Paths


Paths are recorded sequences of deferred line drawing commands. The idea is that you can draw an irregular, invisible shape using various commands and then perform an operation on the whole shape at once. Let’s try it:

‘ Start recording path
BeginPath hDC
’ Do some draw operations
ForeColor = vbYellow
FillColor = vbRed
FillStyle = vbSolid
Line (ScaleWidth * 0.2, ScaleHeight * 0.3)- _
(ScaleWidth * 0.8, ScaleHeight * 0.7)
Circle (ScaleWidth * 0.5, ScaleHeight * 0.5), _
ScaleHeight * 0.3
’ Stop recording path
EndPath hDC
’ Do something to finished path
StrokeAndFillPath hDC
’ Update device context
Refresh

If you step through this code, nothing happens when you step over the Line and Circle statements. The figures aren’t drawn until you reach the StrokeAnd­FillPath procedure, and they usually won’t be visible until you call the Refresh method. Both figures are filled (except in the intersection) with the current FillColor and FillStyle, even though the Line statement doesn’t have the F argument that specifies filling.


We’re mixing Visual Basic statements and Windows API statements, but that’s OK because the Visual Basic statements are calling API functions under the surface. The Line statement probably calls Rectangle, Circle probably calls Ellipse, and they’re both working on the same device context that was passed to the path procedures. The API documentation has a long list of line drawing functions that you can use in paths, but the only Visual Basic statements that work are Line, Circle, and Print.


Print? Since when did Print get to be a line drawing statement? Well, since Windows got TrueType fonts. Try this:

‘ Set a large, TrueType font
With Font
.Name = “Lucida Console”
.Bold = True
.Italic = True
.Size = 48
End With
ForeColor = vbGreen
FillColor = vbMagenta
FillStyle = vbDiagonalCross
’ Make Print statement into a path, and then fill
BeginPath hDC
Print “Hello”
EndPath hDC
StrokeAndFillPath hDC
Refresh

This code draws outlined green text filled with magenta cross-hatches. I was going to include a screen shot, but it was just too hideous.


Besides drawing and filling a path, you can also draw it (StrokePath), fill it (FillPath), and turn it into a region (PathToRegion). I don’t have much to say about regions, except that paths make them easier to manage. In fact, I’m not going to discuss path-related functions such as FlattenPath, WidenPath, Set­MiterLimit, CloseFigure, or ExtCreatePen. You can explore these on your own.


You must set AutoRedraw to True to use paths. I also found that some path features behaved differently under Windows 95 than under Windows NT.