Now ... What just happened?
I said earlier that to use GDI+, you just "slip your code in there beside their code to get what you want done" and that's exactly what we did. That's what the "Overrides" keyword is all about. I also said that "other components use GDI+". The main form for your application is a great example. GDI+ is the way Visual Basic actually draws that form on the screen. So what you're doing is overriding the OnPaint subroutine that Visual Basic actually uses to create the form in the first place and adding a bit of your own code to do something you want done.
Another thing I said earlier is that the Graphics object was the most important one. So we created one and then used the PaintEventArgs parameter that is passed to the OnPaint method. (It's named "e". You can use another name but "e" is very traditional and I wouldn't change it.) The PaintEventArgs parameter gives the new Graphics object that you created the information it needs. Then you call the DrawString method with some text, a new font, and a new color for the font.
Just as Visual Basic uses GDI+ to draw the form, it also uses it to draw controls and other things. You can slide your code right in there in a very similar way. To see how this works, add a button to your form (resize it to make it a BIG button that covers most of the form).
Now double click the button to open the code window again. Again, VB starts you out with some default event code for the Click event. You can delete it again because you're not going to use it. (Delete the code we added above, except for the Class header, too.)
Open the drop down window at the top right of the code window. This will show you all of the events supported by the Button component. Scroll down to find the Paint event and click that to add the default code for that event to your program. Notice that a PaintEventArgs parameter is passed to this event too! You can add EXACTLY the same code to this event. Here's the code. (I've added line continuation characters to keep the lines short again too.)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Public Class Form1
Private Sub Button1_Paint( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles Button1.Paint
Dim g As Graphics = e.Graphics
g.DrawString("About Visual Basic" & vbCrLf _
& "and GDI+" & vbCrLf & "A Great Team", _
New Font("Times New Roman", 20), _
Brushes.Firebrick, 0, 0)
End Sub
End Class
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This time, when you run the program, the text is added inside the button. (That's why we made it big.) Click Here to see the result you should see. The point here is that once you get a Graphics object, you can do just about darn near anything with it. There are tons of objects and w-a-a-a-a-y too many to go through in this article. But now you have a starting point and you can go wild with them on your own!
[an error occurred while processing this directive]