Freetutes.com

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 Polygons

• We could try to use the Polyline function to draw closed regions, or polygons. One drawback to this method is that drawing filled regions is not possible. The DLL function Polygon allows us to draw any closed region defined by a set of (x, y) coordinate pairs.

• Let’s look at the Declare statement for Polygon (from the API Text Viewer):

Private Declare Function Polygon Lib "gdi32" Alias "Polygon" (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) being drawn to, lpPoint is the first point in an array of points defining the vertices of the polygon - it is of type POINTAPI, and nCount is the number of points defining the enclosed region.

• So, to draw a polygon in a picture box, first decide on the (X, Y) coordinates of each vertex in the polygon. Then, decide on line color, line pattern, fill color and fill pattern and set the corresponding properties for the picture box. Then, using Polygon to draw the shape is simple.

Quick Example 5 - Drawing Polygons

  1. Start a new application and establish a form with the following controls: a picture box (ScaleMode set to Pixels), a control array of five option buttons, and a command button:

  2. Set up the General Declarations area to include the user-defined variable (POINTAPI) and the Declare statement for Polygon:}

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

  3. Establish the Command1_Click event:

    Private Sub Command1_Click()
    Dim I As Integer
    For I = 0 To 4
    If Option1(I).Value = True Then
    Exit For
    End If
    Next I
    Picture1.Cls
    Call Draw_Shape(Picture1, I)
    End Sub

  4. Set up a general procedure to draw a particular shape number (PNum) in a general control (PBox). This procedure can draw one of five shapes (0-Square, 1-Rectangle, 2-Triangle, 3-Hexagon, 4-Octagon). For each shape, it establishes some margin area (DeltaX and DeltaY) and then defines the vertices of the shape using the V array (a POINTAPI type variable).

    Private Sub Draw_Shape(PBox As Control, PNum As Integer)

    Dim V(1 To 8) As POINTAPI, Rtn As Long
    Dim DeltaX As Integer, DeltaY As Integer

    Select Case PNum

    Case 0

    'Square
    DeltaX = 0.05 * PBox.ScaleWidth
    DeltaY = 0.05 * PBox.ScaleHeight
    V(1).X = DeltaX: V(1).Y = DeltaY
    V(2).X = PBox.ScaleWidth - DeltaX: V(2).Y = V(1).Y
    V(3).X = V(2).X: V(3).Y = PBox.ScaleHeight - DeltaY
    V(4).X = V(1).X: V(4).Y = V(3).Y
    Rtn = Polygon(PBox.hdc, V(1), 4)

    Case 1

    'Rectangle
    DeltaX = 0.3 * PBox.ScaleWidth
    DeltaY = 0.05 * PBox.ScaleHeight
    V(1).X = DeltaX: V(1).Y = DeltaY
    V(2).X = PBox.ScaleWidth - DeltaX: V(2).Y = V(1).Y
    V(3).X = V(2).X: V(3).Y = PBox.ScaleHeight - DeltaY
    V(4).X = V(1).X: V(4).Y = V(3).Y
    Rtn = Polygon(PBox.hdc, V(1), 4)

    Case 2

    ' Triangle
    DeltaX = 0.05 * PBox.ScaleWidth
    DeltaY = 0.05 * PBox.ScaleHeight
    V(1).X = DeltaX: V(1).Y = PBox.ScaleHeight - DeltaY
    V(2).X = 0.5 * PBox.ScaleWidth: V(2).Y = DeltaY
    V(3).X = PBox.ScaleWidth - DeltaX: V(3).Y = V(1).Y
    Rtn = Polygon(PBox.hdc, V(1), 3)

    Case 3

    'Hexagon
    DeltaX = 0.05 * PBox.ScaleWidth
    DeltaY = 0.05 * PBox.ScaleHeight
    V(1).X = DeltaX: V(1).Y = 0.5 * PBox.ScaleHeight
    V(2).X = 0.25 * PBox.ScaleWidth: V(2).Y = DeltaY
    V(3).X = 0.75 * PBox.ScaleWidth: V(3).Y = V(2).Y
    V(4).X = PBox.ScaleWidth - DeltaX: V(4).Y = V(1).Y
    V(5).X = V(3).X: V(5).Y = PBox.ScaleHeight - DeltaY
    V(6).X = V(2).X: V(6).Y = V(5).Y
    Rtn = Polygon(PBox.hdc, V(1), 6)

    Case 4

    'Octagon
    DeltaX = 0.05 * PBox.ScaleWidth
    DeltaY = 0.05 * PBox.ScaleHeight
    V(1).X = DeltaX: V(1).Y = 0.3 * PBox.ScaleHeight
    V(2).X = 0.3 * PBox.ScaleWidth: V(2).Y = DeltaY
    V(3).X = 0.7 * PBox.ScaleWidth: V(3).Y = V(2).Y
    V(4).X = PBox.ScaleWidth - DeltaX: V(4).Y = V(1).Y
    V(5).X = V(4).X: V(5).Y = 0.7 * PBox.ScaleHeight
    V(6).X = V(3).X: V(6).Y = PBox.ScaleHeight - DeltaY
    V(7).X = V(2).X: V(7).Y = V(6).Y
    V(8).X = V(1).X: V(8).Y = V(5).Y
    Rtn = Polygon(PBox.hdc, V(1), 8)
    End Select
    End Sub

  5. Run the application. Select a shape and click the command button to draw it. Play with the picture box properties to obtain different colors and fill patterns.

  6. To see the importance of proper variable declarations when using DLL’s and the API, make the two components (X and Y) in the POINTAPI variable of type Integer rather than Long. Rerun the program and see the strange results.


  

Tutorial Main Page | Previous Page | Contents | Next Page

   

Home | About Us | Privacy Policy | Contact Us

Copyright © Freetutes.com | All Rights Reserved