• Another DLL graphic function is Polyline. It is used
to connect a series of connected line segments. This is useful for plotting information
or just free hand drawing. Polyline uses the DrawWidth and DrawStyle properties.
• The Declare statement for Polyline is:
Private Declare Function Polyline Lib "gdi32" Alias
"Polyline" (ByVal hdc As Long, lpPoint As POINTAPI, ByVal nCount As
Long) As Long
Note it has three arguments: hdc is the hDC handle of the region
(Form or Picture Box-again, make sure ScaleMode is Pixels) being drawn to, lpPoint
is the first point in an array of points defining the endpoints of the line segments
- it is of a special user-defined type POINTAPI (we will talk about this next),
and nCount is the number of points defining the line segments.
• As mentioned, Polyline employs a special user-defined
variable (a data structure) of type POINTAPI. This definition is made in the general
declarations area and looks like:
Private Type POINTAPI
X As Long
Y As Long
End Type
Any variable defined to be of type POINTAPI will have two coordinates,
an X value and a Y value. As an example, say we define variable A to be of type
POINTAPI using:]
Dim A As POINTAPI
A will have an X value referred to using the dot notation A.X
and a Y value referred to as A.Y. Such notation makes using the Polyline function
simpler. We will use this variable type to define the array of line segment endpoints.
• So, to draw a sequence of line segments in a picture
box, first decide on the (X, Y) coordinates of each segment endpoint. Then, decide
on line color and line pattern and set the corresponding properties for the picture
box. Then, using Polyline to draw the segments is simple. And, as usual, the process
is best illustrated using an example.
Quick Example
4 - Drawing Lines
1. Start a new application. Add a command button. Set the form’s
ScaleMode property to Pixels:
2. Set up the General Declarations area to include the user-defined
variable (POINTAPI) and the Declare statement for Polyline. Also define a variable
for the line endpoints:
Option Explicit
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function Polyline Lib "gdi32" (ByVal hdc As Long, lpPoint
As POINTAPI, ByVal nCount As Long) As Long
Dim V(20) As POINTAPI
Dim Index As Integer
3. Establish the Form_MouseDown event (saves the points):
Private Sub Form_MouseDown(Button As Integer, Shift As Integer,
X As Single, Y As Single)
If Index = 0 Then Form1.Cls
Index = Index + 1
V(Index).X = X
V(Index).Y = Y
End Sub
4. Establish the Command1_Click event (draws the segments):
Private Sub Command1_Click()
Dim RtnValue As Integer
Form1.Cls
RtnValue = Polyline(Form1.hdc, V(1), Index)
Index = 0
End Sub
5. Run the application. Click on the form at different points,
then click the command button to connect the ‘clicked’ points. Try
different colors and line styles.