Replacing Painting Functions

Replacing Painting Functions

Remember how, in Visual Basic 6, you could draw a circle on a form using the Form.Circle method? Remember also how you could make the line stay on the form by setting the Form.AutoDraw property to True? Well, things have changed a little in Visual Basic .NET. The graphics methods Line, Circle, Print, PaintPicture, and Cls have been removed from Form and PictureBox. Instead, Windows Forms has access to the .NET GDI+ object library, which has a much richer set of graphics functions. While all this is very exciting, you ve probably guessed that the graphics methods you re used to are not supported in Windows Forms.

For example, suppose the Click event of a Button contains the following Visual Basic 6 code, which draws a line and a circle on the current form:

Me.Line (0, 200)-(2000, 200) Me.Circle (300, 300), 100

The two methods cannot automatically be upgraded to Visual Basic .NET, so after upgrading, these statements become the following:

'UPGRADE_ISSUE: Form method Form1.Line was not upgraded Me.Line(0, 200) To (2000, 200) 'UPGRADE_ISSUE: Form method Form1.Circle was not upgraded Me.Circle(300, 300, 100)

The line and circle methods cause compile errors in Visual Basic .NET because they are not available as methods of the Form object. Where have the graphics methods gone? you may ask. They have been moved to the GDI+ graphics object. To use a GDI+ graphics object, you create a graphics object, draw onto the graphics object, and then dispose of the graphics object. For example, the following code paints a line and a circle on the form. Try it out by putting it in a Button_Click event in a Visual Basic .NET project.

Dim g As Graphics = Me.CreateGraphics g.DrawLine(Pens.Black, 0, 20, 100, 20) g.DrawEllipse(Pens.Black, 24, 24, 30, 30) g.Dispose()

Figure 15-5 shows the effect of running this code.

Figure 15-5

Drawing a line and a circle in Visual Basic .NET.

This code draws a line of length 100 and a circle with a diameter of 24. Notice that graphics coordinates are in pixels. In Visual Basic 6, the graphics unit was twips by default. Also notice that, as in Visual Basic 6, the origin is the top left corner. In this example, we put the graphics code in a Click event to ensure that the form is visible when the code is run. There is no AutoRedraw property in Visual Basic .NET. If the window is hidden, minimized, or moved off the screen, the line and circle you worked so hard to draw are erased. Luckily, Windows Forms gives you a way to redraw your objects every time the form is repainted. You do this using the Form.Paint event. This event is called every time a portion of the form s background has to be repainted. The following code shows how to paint a line and a circle using the Paint event. Notice that the system passes a graphics object to the event, so you don t need to create one yourself.

Private Sub frmLineAndCircle_Paint(ByVal sender As Object, _ ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint    e.Graphics.DrawLine(Pens.Black, 0, 20, 100, 20)    e.Graphics.DrawEllipse(Pens.Black, 24, 24, 30, 30) End Sub

In most applications, Form.Paint is the best place to put graphics code, since the graphics are redrawn each time the form is repainted.

Now let s look at something else you can do in the Paint event using GDI+. The following Paint event makes the form background fade from black to white:

Private Sub frmGradient_Paint(ByVal sender As Object, _ ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint    Dim rectForm As New Rectangle(New Point(0, 0), Me.ClientSize)    Dim l As New Drawing2D.LinearGradientBrush(rectForm, Color.Black, _       Color.White, Drawing.Drawing2D.LinearGradientMode.Horizontal)    e.Graphics.FillRectangle(l, rectForm) End Sub

Figure 15-6 shows the form with the gradient background. Notice that this form also has a TextBox and a PictureBox on it, and that these controls haven t been painted with the gradient. This brings us to an important point: the graphics object passed to the Form.Paint event paints only the form background. Each control on the form also has a Paint event, which you can use to draw on that particular control.

Figure 15-6

Black to white background on a form.

In addition to circles, lines, and gradient backgrounds, the graphics object also has all the methods that were available in Visual Basic 6. To erase the background, use the Clear method; to paint a picture, use the PaintImage method; and to print text onto a form, use the DrawString method. These methods are incredibly flexible. For example, the following code draws the string VB Rocks onto the background of a form, using the contents of a PictureBox as a brush:

Dim bitmapBrush As New Drawing.TextureBrush(PictureBox1.Image) Dim f As New Font("Arial", 60, FontStyle.Bold, GraphicsUnit.Pixel) e.Graphics.DrawString("VB Rocks", f, bitmapBrush, 20, 20)

As you can see, the GDI+ methods are quite powerful.

Learn More About GDI+

What we ve shown here just scratches the surface. GDI+ is a fully featured set of graphics classes. You can draw paths, rectangles, icons, and pictures. You can rotate and transform these objects, and you can use alpha blending to fade one color into another. You will find GDI+ in the System.Drawing namespace. To learn more, search for Introduction to GDI+ and Using GDI+ Managed Classes in the Visual Basic .NET Help system.



Upgrading Microsoft Visual Basic 6.0to Microsoft Visual Basic  .NET
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET w/accompanying CD-ROM
ISBN: 073561587X
EAN: 2147483647
Year: 2001
Pages: 179

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net