1. Home
  2. Computing & Technology
  3. Visual Basic

A GDI+ Introduction for Visual Basic 6 Programmers
A Starting Example - Adding GDI+ Text to a Component

By , About.com Guide

This first example is a bit more difficult than the next one. I'm doing it in this order to be more complete in describing how to use GDI+. But just follow the steps and make it work. We'll go over what happened after that.

1 - Start Visual Basic 2005 and start a new Windows Application. This will give you a blank form.

2 - Double click the form to open a program code window.

3 - Visual Basic automatically adds code to process the Load event for the form.

Private Sub Form1_Load( ... etc.

End Sub

Delete that. We're going to override the "behind the scenes" code for the form itself instead.

4 - Add this code inside the Class block. The whole thing is shown below. (I've used a lot of line continuations to keep the lines short for this article. You can keep them or delete them.)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Public Class Form1
   Protected Overrides Sub OnPaint( _
      ByVal e As System.Windows.Forms.PaintEventArgs)
      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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

5 - Run your program! You should see the screen shown when you Click Here.

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 to give a new Graphics object that you create the information it needs. (The PaintEventArgs parameter is named "e". You can use another name but "e" is very traditional and I wouldn't change it.) 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 again 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 the button 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.

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. A GDI+ Introduction for Visual Basic 6 Programmers

©2009 About.com, a part of The New York Times Company.

All rights reserved.