Make Controls And Forms With Your Own Custom Shape

The code below do similar thing to the Shaper control (in the ActiveX section).
You can choose what shape your form will have, your command buttons, text boxes, and every other control that have the hWnd property.

All you have to do is to choose the X and Y coordiantes of the shape's corners, It has to be closed shape, thus the last X and Y coordinates are equal to the first X and Y coordinates.

Example: square coordinates will be:
X=0 Y=0 The upper left corner
X=500  Y=0 The upper right corner
X=500 Y=500  The bottom right corner
X=0 Y=500 The Bottom left corner
X=0 Y=0 The upper left corner again. As  I mentioned - The shape must be closed

The sample below will make your form's shape be Triangle.

Preparations

Add 3 Command Buttons to your form.
Press the first button to see what shape your form will have.
Press the second button to shape it.
Press the third button to restore the form to its orginal shape.

Module Code

Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As POINTAPI, _
    ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, _
    ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
Declare Function Polyline Lib "gdi32" (ByVal hdc As Long, lpPoint As _
    POINTAPI, ByVal nCount As Long) As Long

Public Type POINTAPI
        X As Long
        Y As Long
End Type

Form Code

Dim Result As Long
'replace the '4' below with the number of corners that you have in your shape.
'in this exaple the last point is Points(4).Y

Dim Points(1 To 4) As POINTAPI

Private Sub Command2_Click()
'shape your form with your selected shape. replace the '4' below with the
'number of points you have in your shape (in this exaple the last point is Points(4).Y)

    hRgn = CreatePolygonRgn(Points(1), 4, 1)
'if you want to shape other control, instead of Form1, replace the "From1" below
'with the control's name. for example, to shape Command1, uncomment
'the line below:
'Result = SetWindowRgn(Command1.hWnd, hRgn, True)
   
Result = SetWindowRgn(Form1.hWnd, hRgn, True)
End Sub

Private Sub Command1_Click()
'draw the shape on the form, just to demonstrate how your form's shape will be
    Call Polyline(Form1.hdc, Points(1), 4)
End Sub

Private Sub Command3_Click()
'restore the form to its orginal shape
    Result = SetWindowRgn(Form1.hWnd, 0, True)
End Sub

Private Sub Form_Load()
'set the X and Y coordinates of every corner.
'Points(1).X and Points(1).Y are the X and Y coordinates of the
'first corner, and so on.
'note that to close the shape, the first point (Points(1)) and the last
'point (Points(4)) are the same.
'If you add another points (Points(5).X and so on), remember to update
'the line:    Dim Points(1 To 4) As POINTAPI    to
'Dim Points(1 To 5) As POINTAPI 
'and the line:    hRgn = CreatePolygonRgn(Points(1), 4, 1)   to
'hRgn = CreatePolygonRgn(Points(1), 5, 1)

    Points(1).X = 500
    Points(1).Y = 500
    Points(2).X = 0
    Points(2).Y = 0
    Points(3).X = 1000
    Points(3).Y = 0
    Points(4).X = 500
    Points(4).Y = 500
End Sub

Go Back