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

The Form_Load event handler calls subroutine ShapeForm to shape the form. This routine makes an array of POINTAPI structures listing the points that define the form's shape. You could explicitly set the values in this array to give the form any polygonal shape.

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.

 
' Restrict the form to a star-shaped polygon.
Private Sub ShapeForm()
Const ALTERNATE = 1
Const NUM_POINTS = 20
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 w(0 To 1) As Single
Dim h(0 To 1) As Single
Dim cx As Single
Dim cy As Single
Dim points() As POINTAPI

    ' Don't bother if we're minimized.
    If WindowState = vbMinimized Then Exit Sub

    ' Create the points for the polygon.
    ' This example uses sines and cosines to make
    ' a many pointed star. You could enter points
    ' explicitly to create a polygon of any shape.
    wid = ScaleX(Width, vbTwips, vbPixels)
    hgt = ScaleY(Height, vbTwips, vbPixels)
    cx = wid / 2
    cy = hgt / 2
    w(0) = wid * 0.2
    w(1) = wid * 0.5
    h(0) = hgt * 0.2
    h(1) = hgt * 0.5
    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 + w(i Mod 2) * Cos(theta)
            .y = cy + h(i Mod 2) * Sin(theta)
            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
 
Note that the program makes the region big enough to include part of the title bar. That lets the user click and drag to move the form. If you make the region smaller so it includes none of the title bar, the user will be unable to move the form.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated