Recipe 9.12. Using an Outline Path


Problem

You want to create a complex graphics drawing path that can simplify graphics drawing commands and can be reused repeatedly.

Solution

Sample code folder: Chapter 09\ GraphicsPath

The GraphicsPath object lets you create and store a complex sequence of graphics lines, rectangles, ellipses, and polygons as a single object.

Discussion

The GraphicsPath is part of the Drawing2D namespace, so be sure to add the following Imports statement to the top of your code:

 Imports System.Drawing.Drawing2D 

In this recipe we'll use a GraphicsPath object to draw a checkerboard. The drawing takes place in the form's Paint event handler:

 Private Sub Form1_Paint(ByVal sender As Object, _       ByVal e As System.Windows.Forms.PaintEventArgs) _       Handles Me.Paint 

To begin, the graphics surface for the form is referenced, and a static GraphicsPath reference variable (thePath) is created. The path is created the first time the event handler gets called and is used again on successive calls:

   ' ----- Draw a checkerboard.   Dim across As Integer   Dim down As Integer   Dim canvas As Graphics = e.Graphics   Static thePath As GraphicsPath   ' ----- Draw the checkerboard the first time only.   If (thePath Is Nothing) Then      thePath = New GraphicsPath      For across = 0 To 7         For down = 0 To 7            If (((across + down) Mod 2) = 1) Then               thePath.AddRectangle( _                  New Rectangle(across, down, 1, 1))            End If         Next down      Next across   End If 

The scaling needs to take place every time the Paint event is triggered because as the user changes the size of the form (and the graphics surface), the checkerboard stretches to fit it:

   ' ----- Scale the form for the checkerboard.   Dim scaleX As Single   Dim scaleY As Single   scaleX = CSng(Me.ClientSize.Width / 10)   scaleY = CSng(Me.ClientSize.Height / 10)   canvas.ScaleTransform(scaleX, scaleY)   canvas.TranslateTransform(1, 1) 

Finally, the path is drawn using a blue brush, and its outline is drawn around the edges:

   ' ----- Draw and outline the checkerboard.   canvas.FillPath(Brushes.Blue, thePath)   canvas.DrawRectangle(New Pen(Color.Blue, -1), 0, 0, 8, 8) End Sub 

The form's Resize event needs a command to cause the form to refresh as it is resized. This causes the checkerboard to be redrawn on the fly as the form is stretched or shrunk:

 Private Sub Form1_Resize(ByVal sender As Object, _       ByVal e As System.EventArgs) Handles Me.Resize       ' ----- Redraw the checkerboard.    Me.Refresh() End Sub 

For maximum smoothness of the action, be sure to set the form's DoubleBuffered property to true.

Figure 9-20 shows the checkerboard when the form has been resized to fairly square dimensions.

Figure 9-20. A checkerboard drawn using a single path





Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

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