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
 
 
 
 
 
 
TitleMake a rotating polygonal shaped form
Keywordsform, shape, polygon, rotate, spin, SetWindowRgn, region, shaped form
CategoriesTips and Tricks, Graphics
 

This program uses a Timer. When the Timer event fires, the program calls subroutine ShapeForm passing it a new offset angle.

ShapeForm makes an array of POINTAPI structures listing the points that define the form's star shape. It adds the offset angle value to the angle it uses to make the star's points. Repeating the process for different offset angles makes the star rotate.

The routine then calls the CreatePolygonRgn API function to make a region defined by the points. Finally it calls SetWindowRgn to restrict the form to the polygonal region.

 
Private Sub Timer1_Timer()
Const PI = 3.14159265

Static offset As Single

    ShapeForm offset
    offset = offset + PI / 40
End Sub

Private Sub ShapeForm(ByVal offset As Single)
Const ALTERNATE = 1
Const NUM_POINTS = 6
Const PI = 3.14159265

Dim rgn As Long
Dim wid As Single
Dim hgt As Single
Dim theta As Single
Dim dtheta As Single
Dim i As Integer
Dim cx As Single
Dim cy As Single
Dim points() As POINTAPI

    If WindowState = vbMinimized Then Exit Sub
    
    ' Create the points for the polygon.
    ' This example uses sines and cosines to make
    ' a hexagon rotated by the offset angle.
    wid = ScaleX(Width, vbTwips, vbPixels)
    hgt = ScaleY(Height, vbTwips, vbPixels)
    cx = wid / 2
    cy = hgt / 2
    wid = wid * 0.4
    hgt = hgt * 0.4
    dtheta = 2 * PI / NUM_POINTS
    theta = PI / 2
    ReDim points(1 To NUM_POINTS)
    For i = 1 To NUM_POINTS
        With points(i)
            .x = cx + wid * Cos(theta + offset)
            .y = cy + hgt * Sin(theta + offset)
            theta = theta + dtheta
        End With
    Next i

    ' Create the region.
    rgn = CreatePolygonRgn(points(1), _
        NUM_POINTS, ALTERNATE)

    ' Restrict the window to the region.
    SetWindowRgn hWnd, rgn, True
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated