|
|
Title | Draw a polygon filled with a picture |
Keywords | region, path, polygon, picture |
Categories | Graphics, API |
|
|
At design time, the PictureBox's Picture property is set to display a picture.
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. Any pieces of the loaded picture that fall outside the region are clipped off automatically.
|
|
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
End Sub
|
|
For more information on graphics programming in Visual Basic, see my book Visual Basic Graphics Programming.
|
|
|
|
|
|