Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleUse the Polygon API function to quickly draw a polygon
Keywordspolygon, draw
CategoriesGraphics
 

When the form receives a Resize event, it calculates the points needed to draw a 13-pointed star filling the form. It saves the coordinates of the points in an array of POINTAPI structures and uses the Polygon API function to draw the polygon. Note that this function recognizes Visual Basic's normal drawing properties: FillStyle, FillColor, ForeColor, DrawWidth, etc.

 
' Draw a star.
Private Sub Form_Resize()
Const NUM_STAR_POINTS = 13
Const NUM_POINTS = 2 * NUM_STAR_POINTS
Dim pts(1 To NUM_POINTS) As POINTAPI
Dim rx1 As Single
Dim rx2 As Single
Dim ry1 As Single
Dim ry2 As Single
Dim theta As Single
Dim dtheta As Single
Dim cx As Single
Dim cy As Single
Dim i As Integer

    Cls
    rx2 = ScaleWidth * 0.5
    ry2 = ScaleHeight * 0.5
    rx1 = rx2 * 0.5
    ry1 = ry2 * 0.5
    cx = ScaleWidth / 2
    cy = ScaleHeight / 2

    ' Calculate points.
    dtheta = 2 * 3.14159265 / NUM_POINTS
    theta = 0
    For i = 1 To NUM_POINTS Step 2
        pts(i).x = cx + rx1 * Cos(theta)
        pts(i).y = cy + ry1 * Sin(theta)
        theta = theta + dtheta
        pts(i + 1).x = cx + rx2 * Cos(theta)
        pts(i + 1).y = cy + ry2 * Sin(theta)
        theta = theta + dtheta
    Next i

    FillStyle = vbFSSolid
    FillColor = vbGreen
    ForeColor = vbBlue
    DrawWidth = 2
    Polygon hdc, pts(1), NUM_POINTS
End Sub
 
For more information on graphics programming in Visual Basic, see my book Visual Basic Graphics Programming.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated