Polygons
This short tutorial teaches you how draw polygons using the windows API. The windows API are the functions that windows uses to run so you should be careful using them as they can crash windows.
First we have to declare the API at the top of our code.
Private Type POINTAPI X As Long Y As Long End Type Private Declare Function Polygon Lib "gdi32" _ (ByVal hdc As Long, lpPoint As POINTAPI, ByVal nCount As Long) As Long
'Private Declare Function' We already know what the private means. Declare function tells visual basic that we are using this function.
'Polygon' is the name of the function.
'Lib "gdi32"' Tells Visual Basic which library this API is in. The API are arranged into libraries with each library having several related functions inside it.
'_' This tells visual basic that there should not be a line break here. It has just been put in so that it fits well on a single line.
Next we have the variables that we input into the function.
'ByVal hdc As Long' This is the hdc reference of wherever yu want the polygon to be drawn. This is a reference that windows give to an objects drawing space in the memory. To get the hdc of the object we are using we simply type 'Object.hdc'. Not all objects have a hdc value, the main ones are forms and pictureboxes.
'lpPoint As POINTAPI' We are using the POINTAPI type here. The POINTAPI just has an X and Y coordinate. The lpPoint is the first element of the array which will contain the points of the polygon. This is ByRef because the funtion needs access to the other elements of the array.
'ByVal nCount As Long' This is the number of vertices that our polygon has. You must make sure that the array you have sent is large enough. If the array is too big then the end of it will just be ignored.
'As Long' The funtion returns a value to say whether it succeeded or not. I usually ignore this return value.
Setting Other Properties
Although we can only send so much information to the function it does get information from the objects hdc reference. Before we call the function we can set our objects colours etc. I will show this in the example below.
Copy and paste this code into a blank project. Run it and click on the form.
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function Polygon Lib "gdi32" _
(ByVal hDC As Long, lpPoint As POINTAPI, ByVal nCount As Long) As Long
Dim Poly(2) As POINTAPI
Private Sub Form_Click()
Randomize 'If you dont include this then the project will produce the same random sequence each time.
For i = 0 To 2 'loop through the whole array
Poly(i).X = Int(Rnd * 200) 'pick some random coordinates
Poly(i).Y = Int(Rnd * 200)
Next
Me.FillStyle = 0 'set the fill style this is defaulted to 1 transparent
Me.FillColor = RGB(160 + Rnd * 80, 30, 30) 'set the colour to be filled. I have made it a bit random
Me.DrawStyle = 5 'the style of the border. You can find out the numbers by looking at the objects properties
Me.ForeColor = RGB(200, 200, 50) 'this sets the color of the border
Me.DrawWidth = 1 'the sets the width of the border
Polygon Me.hDC, Poly(0), 3 'call the polygon function
End Sub
© Jonathan Waller 2005; QuantumState Visual Basic



