CodeItBetter Programming Another VB Programming Blog

How to Make Controls and Forms with your own Custom Shape

Posted on January 5, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'Forms - How to Make Controls and Forms with your own Custom Shape

Option Explicit
 
'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.

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

'Add 3 Command Buttons to your form. The first one is to see what shape your form will have.
'The second one to shape it and the third one to restore the form to its orginal shape.

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
 
'Add the following code to your form:

Dim Result As Long
'replace the '4' below with the number of corners that you have in your shape.
'in this example 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 example 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 "Form1" below with the control's name.
    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
Filed under: Forms Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.