Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

Chapter 15 Adding Graphics and Animation Effects 379


'Use a green brush color to create a filled rectangle
Dim BrushColor As New SolidBrush(Color.Green)
GraphicsFun.FillRectangle(BrushColor, 150, 10, 250, 100)

'Create a blue cardinal spline curve with four points
Dim Points() As Point = {New Point(358, 280), _
New Point(300, 320), New Point(275, 155), New Point(350, 180)}
For tension As Single = 0 To 2.5 Step 0.5
GraphicsFun.DrawCurve(Pens.DodgerBlue, Points, tension)
Next
This sample event procedure draws four graphic shapes on your form: a red line, a red
ellipse, a green-filled rectangle, and a blue cardinal spline (a complex curve made up
of five lines). To enable graphics programming, the routine declares a variable named
GraphicsFun in the code and uses the CreateGraphics method to activate or instantiate
the variable. The PenColor variable of type Pen is used to set the drawing color in the
line and ellipse, and the BrushColor variable of type SolidBrush is used to set the fill
color in the rectangle. These examples are obviously just the tip of the graphics library
iceberg—there are many more shapes, colors, and variations that you can create by
using the methods in the System.Drawing.Graphics class.

Tip The complete Draw Shapes program is located in the C:\Vb10sbs\Chap15\Draw
Shapes folder.


  1. Click the Start Debugging button on the Standard toolbar to run the program.


Visual Basic loads the form and executes the form’s Paint event. Your form looks
like this:


  1. Minimize the form, and then restore it again.


The form’s Paint event is executed again, and the graphics shapes are refreshed on
the form.
Free download pdf