Visual Basic Tip: 02/26/96


Draw Filled Objects.

Using the Polygon and SetPolyFillMode API's you can create
complex filled shapes in Visual Basic.  Create a form with a
picture box named  'P'.  Then copy the code for P_Paint to your
form.  Also create a module (ex: module1.bas).  Then copy the
declares and the DrawPolygon subroutine to this module.  It is
easy to modify this code to create other complex filled shapes.


'form containing a picturebox named 'p'
  Sub P_Paint()
   p.ScaleMode = 3
   p.CurrentY = p.ScaleHeight / 2
   p.CurrentX = p.ScaleWidth / 2
   DrawPolygon p, 45, QBColor(12)
  End Sub


'declares (general *.BAS module)
  Option Explicit

  Type POINTAPI
    X As Integer
    Y As Integer
  End Type

  'PolyFill Modes
  Global Const ALTERNATE = 1
  Global Const WINDING = 2

  Declare Sub drPOLYGON Lib "GDI" Alias "Polygon" (ByVal hDC As Integer, lpPoints As POINTAPI, ByVal nCount As Integer)
  Declare Sub SetPolyFillMode Lib "GDI" (ByVal hDC As Integer, ByVal nPolyFillMode As Integer)

  Dim Ply(10) As POINTAPI


'drawing subroutine (general *.BAS module)
  Sub DrawPolygon(p As Control, Size As Integer, FColor As Long)
   Dim nPoints As Integer, i As Integer
   Dim SoldMode, SoldTOP, SoldLeft, SoldWidth, SoldHeight
  
   'save current attributes
   SoldMode = p.ScaleMode
   SoldTOP = p.ScaleTop
   SoldLeft = p.ScaleLeft
   SoldWidth = p.ScaleWidth
   SoldHeight = p.ScaleHeight

   'setup scaleMode, fill style, ...
   p.ScaleMode = 3         ' PolyGon needs everything in pixels
   p.FillStyle = 0         ' Sold Fill
   p.FillColor = FColor    ' Fill Color
   p.DrawWidth = 1         ' Border width in pixels

   'build coordinates for shape
   Ply(1).X = Size / 2
   Ply(1).Y = 0
   Ply(2).X = 4 * Size / 5
   Ply(2).Y = Size
   Ply(3).X = 0
   Ply(3).Y = Size / 3
   Ply(4).X = Size
   Ply(4).Y = Ply(3).Y
   Ply(5).X = Size - Ply(2).X
   Ply(5).Y = Size
   nPoints = 5
  
   'adjust Points to center shape at current point
   For i = 1 To nPoints
     Ply(i).X = Ply(i).X + p.CurrentX - Size / 2
     Ply(i).Y = Ply(i).Y + p.CurrentY - Size / 2
   Next
   'set PolyFillMode to fill whole shape
   SetPolyFillMode p.hDC, WINDING
   'draw PolyGon
   drPOLYGON p.hDC, Ply(1), nPoints

  'restore old attributes
   p.ScaleMode = SoldMode
   p.ScaleTop = SoldTOP
   p.ScaleLeft = SoldLeft
   p.ScaleHeight = SoldHeight
   p.ScaleWidth = SoldWidth
  End Sub

Return to Phil's Web Page

Created by: Philip