|
|
Title | Draw a polygon filled with text |
Keywords | text, region, path, polygon |
Categories | Graphics, API |
|
|
When the program starts, the Form_Load event handler uses some trigonometry to calculate the points needed to draw an eight-pointed star and stores them in the pts array of POINTAPI structures. It uses the CreatePolygonRgn API function to make a region defined by those points. It then calls SetWindowRgn to confine the PictureBox to that region.
Next the program draws text all over the PictureBox. Any pieces that fall outside the region are clipped off automatically.
Finally, the program calls the Polygon API function to draw the polygon around the edge of the region.
|
|
Private Sub Form_Load()
Const PI = 3.14159265
Const NUM_POINTS = 8
Const NUM_PTS = 2 * NUM_POINTS
Const TXT = "Star "
Dim pts(1 To NUM_PTS) As POINTAPI
Dim i As Integer
Dim cx As Single
Dim cy As Single
Dim theta As Single
Dim dtheta As Single
Dim rx1 As Single
Dim rx2 As Single
Dim ry1 As Single
Dim ry2 As Single
Dim hRgn As Long
Dim clr As Single
Dim dclr As Single
Dim offset As Single
' API functions work in pixels.
picStar.ScaleMode = vbPixels
picStar.AutoRedraw = True
picStar.BorderStyle = vbBSNone
' Set the points' coordinates.
cx = picStar.ScaleWidth / 2
cy = picStar.ScaleHeight / 2
rx1 = picStar.ScaleWidth * 0.49
rx2 = picStar.ScaleWidth * 0.25
ry1 = picStar.ScaleHeight * 0.49
ry2 = picStar.ScaleHeight * 0.25
dtheta = 2 * PI / NUM_PTS
theta = 0
i = 1
Do While i < NUM_PTS
pts(i).X = cx + rx1 * Cos(theta)
pts(i).Y = cy + ry1 * Sin(theta)
theta = theta + dtheta
i = i + 1
pts(i).X = cx + rx2 * Cos(theta)
pts(i).Y = cy + ry2 * Sin(theta)
theta = theta + dtheta
i = i + 1
Loop
' Confine the PictureBox to this polygon.
hRgn = CreatePolygonRgn(pts(1), NUM_PTS, WINDING)
SetWindowRgn picStar.hWnd, hRgn, True
' Draw a bunch of text in the picture.
picStar.ForeColor = RGB(0, 128, 255)
picStar.Font.Bold = True
picStar.BackColor = vbWhite
picStar.CurrentX = 0
picStar.CurrentY = 0
Do While picStar.CurrentY <= picStar.ScaleHeight
Do While picStar.CurrentX <= picStar.ScaleWidth
picStar.Print TXT;
Loop
picStar.Print
picStar.CurrentX = offset
offset = offset - 7
Loop
' Draw the polygon's border.
picStar.DrawWidth = 3
picStar.ForeColor = vbBlue
Polygon picStar.hdc, pts(1), NUM_PTS
End Sub
|
|
For more information on graphics programming in Visual Basic, see my book Visual Basic Graphics Programming.
|
|
|
|
|
|