VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering

Browse Topics

- Getting started
- Data Types
- Modules
- Operators in VB6
- VB6 Variable
- VB6 Procedures
- VB6 Control Structures
- Loops in VB6
- VB6 Exit Do & With End With
- Arrays in VB6
- User-Defined Data Types
- VB6 Constants
VB6 Built-in Functions
- Date and Time in VB6
- VB6 Controls
- TextBox Control
- ComboBox & OptionButton
- Label & Frame
- PictureBox & ImageBox
- Timer Control
- ListBox & ComboBox
- VB6 ScrollBar
- Control Arrays in VB6
- Files controls in VB6
- VB6 CheckBox
- Forms in VB6
- Menus in VB6
- MDI Form in VB6
- InputBox
- MessageBox
- Mouse events
- Mouse Move
- Error Handling
Error Handling (2)
VB6 Database

You are here: Visual Basic > VB6 (Beginners Tutorial)

Tutorial Main Page | Previous Page | Contents | Next Page

Drawing Lines

• 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:]


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 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
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.


Tutorial Main Page | Previous Page | Contents | Next Page


Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved