Link to home
Start Free TrialLog in
Avatar of mSchmidt
mSchmidt

asked on

Need to draw outline and fill a region, created using CreatePolyPolygonRgn

I need someway to fill and then outline a region created by the CreatePolyPolygonRgn...
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Private Type COORD
    x As Long
    y As Long
End Type
Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long

Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As Any, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Private Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As Any, ByVal nCount As Long) As Long
Private Declare Function FillRgn Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long, ByVal hBrush As Long) As Long
Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Const ALTERNATE = 1 ' ALTERNATE and WINDING are
Const WINDING = 2 ' constants for FillMode.
Const BLACKBRUSH = 4 ' Constant for brush type.

Private Sub Form_Load()
    Me.AutoRedraw = True
    'KPD-Team 1999
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    Dim poly(1 To 3) As COORD, NumCoords As Long, hBrush As Long, hRgn As Long
    Me.Cls
    ' Number of vertices in polygon.
    NumCoords = 3
    ' Set scalemode to pixels to set up points of triangle.
    Me.ScaleMode = vbPixels
    ' Assign values to points.
    poly(1).x = Form1.ScaleWidth / 2
    poly(1).y = Form1.ScaleHeight / 2
    poly(2).x = Form1.ScaleWidth / 4
    poly(2).y = 3 * Form1.ScaleHeight / 4
    poly(3).x = 3 * Form1.ScaleWidth / 4
    poly(3).y = 3 * Form1.ScaleHeight / 4
    ' Polygon function creates unfilled polygon on screen.
    ' Remark FillRgn statement to see results.
    Polygon Me.hdc, poly(1), NumCoords
   
    ' Gets stock black brush.
    'hBrush = GetStockObject(BLACKBRUSH)
    hBrush = CreateSolidBrush(RGB(255, 126, 25))

    ' Creates region to fill with color.
    hRgn = CreatePolygonRgn(poly(1), NumCoords, ALTERNATE)
    ' If the creation of the region was successful then color.
    If hRgn Then FillRgn Me.hdc, hRgn, hBrush
    DeleteObject hRgn
End Sub
Avatar of mSchmidt
mSchmidt

ASKER

Could you get that working with PolyPolygon instead of only Polygons ?...
well increase the points if its possible... i cant seem to get it working
i tried this code

    Me.AutoRedraw = True
    Dim vertex(0 To 6) As POINTAPI ' holds vertices of each polygon
    Dim numvertices(0 To 1) As Long  ' holds how many vertices belong to each polygon
    Dim hRgn As Long  ' handle to the multi-polygonal region
    Dim retval As Long  ' return value

    ' Load the vertices of the triangle into the vertex array.
    vertex(0).x = 25: vertex(0).y = 25  ' 1st point: (25,25)
    vertex(1).x = 50: vertex(1).y = 50  ' 2nd point: (50,50)
    vertex(2).x = 25: vertex(2).y = 50  ' 3rd point: (25,50)
    numvertices(0) = 3  ' three vertices for the triangle

    ' Load the vertices of the diamond into the vertex array.
    vertex(3).x = 150: vertex(3).y = 150  ' 1st point: (150,150)
    vertex(4).x = 200: vertex(4).y = 200  ' 2nd point: (200,200)
    vertex(5).x = 150: vertex(5).y = 250  ' 3rd point: (150,250)
    vertex(6).x = 100: vertex(6).y = 200  ' 4th point: (100,200)
    numvertices(1) = 4  ' four vertices for the triangle
    hBrush = CreateSolidBrush(RGB(255, 126, 25))
    ' Create the multi-polygonal region and get a handle to it.
    hRgn = CreatePolyPolygonRgn(vertex(0), numvertices(0), 2, ALTERNATE)

    ' If the creation of the region was successful then color.

    If hRgn Then FillRgn Me.hdc, hRgn, hBrush

    DeleteObject hRgn

It also need to fill and outline... the other one worked GREAT...
It works, maybe you forget to put the Command buttons ? (Command1, Command2, Command3)

Try this modified version:

Private Declare Function CreateEllipticRgn Lib "gdi32" _
     (ByVal X1 As Long, ByVal Y1 As Long, _
     ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" _
     (ByVal hObject As Long) As Long
Private Declare Function SetWindowRgn Lib "user32" _
     (ByVal hWnd As Long, ByVal hRgn As Long, _
     ByVal bRedraw As Long) As Long
Private hRegion As Long
Private Declare Function CreateRectRgnIndirect Lib "gdi32" _
     (lpRect As RECT) As Long

Private Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function CreateEllipticRgnIndirect Lib "gdi32" _
     (lpRect As RECT) As Long
Private Declare Function CreatePolyPolygonRgn Lib "gdi32" (lpPoint As POINTAPI, lpPolyCounts As Long, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Private Const WINDING = 2
Private Const ALTERNATE = 1
Private Type RECT
  Left As Long
  Top As Long
  Right As Long
  Bottom As Long
End Type
Private Type POINTAPI
  x As Long
  y As Long
End Type

Private Sub Command3_Click()
    Dim x As Long
    Me.ScaleMode = vbPixels
    hRegion = CreateEllipticRgn(0, 0, Me.ScaleWidth, Me.ScaleHeight)
    x = SetWindowRgn(Me.hWnd, hRegion, True)
End Sub

Private Sub Form_Load()
    Command2_Click
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Dim x As Long
    Me.Visible = False ' must be gone before region is deleted!
    x = DeleteObject(hRegion)
End Sub

Private Sub Form_Click()
    Unload Me ' need a clean way out! (this is just for testing)
End Sub

'To make non-round forms use the other API calls to create a region of
'whatever shape is desired. Some of the other region calls are:

'Example 1: Create a diamond form:
Private Sub Command1_Click()
    Dim x As Long
    Dim udtPoints(4) As POINTAPI
    Me.ScaleMode = vbPixels
    udtPoints(0).x = Me.ScaleWidth \ 2: udtPoints(0).y = 0
    udtPoints(1).x = 0: udtPoints(1).y = Me.ScaleHeight \ 2
    udtPoints(2).x = Me.ScaleWidth \ 2: udtPoints(2).y = Me.ScaleHeight
    udtPoints(3).x = Me.ScaleWidth: udtPoints(3).y = Me.ScaleHeight \ 2
    hRegion = CreatePolygonRgn(udtPoints(0), 4, WINDING)
    x = SetWindowRgn(Me.hWnd, hRegion, True)
End Sub
   
'Example 2: Create a double-diamond form:
Private Sub Command2_Click()
    Dim x As Long
    Dim udtPolygons(0 To 15) As POINTAPI
    Dim lngPoints(0 To 2) As Long
    Me.ScaleMode = vbPixels
    Me.AutoRedraw = True
   
    ' define diamond on left-hand side
    udtPolygons(0).x = Me.ScaleWidth \ 4: udtPolygons(0).y = 0
    udtPolygons(1).x = 0: udtPolygons(1).y = Me.ScaleHeight \ 2
    udtPolygons(2).x = Me.ScaleWidth \ 4: udtPolygons(2).y = Me.ScaleHeight
    udtPolygons(3).x = Me.ScaleWidth \ 2: udtPolygons(3).y = Me.ScaleHeight \ 2
    udtPolygons(4).x = udtPolygons(0).x: udtPolygons(4).y = udtPolygons(0).y
    lngPoints(0) = 5 ' 5 points in first polygon
    ' define diamond on the right
    udtPolygons(5).x = Me.ScaleWidth * 3 \ 4: udtPolygons(5).y = 0
    udtPolygons(6).x = Me.ScaleWidth \ 2: udtPolygons(6).y = Me.ScaleHeight \ 2
    udtPolygons(7).x = Me.ScaleWidth * 3 \ 4: udtPolygons(7).y = Me.ScaleHeight
    udtPolygons(8).x = Me.ScaleWidth: udtPolygons(8).y = Me.ScaleHeight \ 2
    udtPolygons(9).x = udtPolygons(5).x: udtPolygons(9).y = udtPolygons(5).y
    lngPoints(1) = 5 ' 5 points in second polygon
   
    udtPolygons(10).x = 300: udtPolygons(10).y = 120
    udtPolygons(11).x = 350: udtPolygons(11).y = 130
    udtPolygons(12).x = 450: udtPolygons(12).y = 163
    udtPolygons(13).x = 289: udtPolygons(13).y = 199
    udtPolygons(14).x = 223: udtPolygons(14).y = 170
    udtPolygons(15).x = 78: udtPolygons(15).y = 279
    lngPoints(2) = 6 ' 6 points in 3rd polygon
   
    ' create region from them
    'hRegion = CreatePolyPolygonRgn(udtPolygons(0), lngPoints(0), 2, WINDING)
    hRegion = CreatePolyPolygonRgn(udtPolygons(0), lngPoints(0), 3, WINDING)
    x = SetWindowRgn(Me.hWnd, hRegion, True)
End Sub
Need to draw them not make the form like it, i need to fill them and outline them

Thanks, increased the point for this one
then you may need to use FillRgn to fill the region with the colors, see my first post example?

and err... sorry, what is mean by drawing the outline ? Is that mean to draw the sides of the polygon with a Line?

regards
Could you please create an example i have been trying for ages and i simple cant get it to fill the region..

And yes i need a line around the PolyPolygon
Hi mSchmidt,

I will post it by tomorrow, now is 1.04AM at my local time, cheers
Try this out :)

Private Type COORD
    x As Long
    y As Long
End Type
Private Type POINTAPI
  x As Long
  y As Long
End Type
Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long

Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As Any, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Private Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As Any, ByVal nCount As Long) As Long
Private Declare Function FillRgn Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long, ByVal hBrush As Long) As Long
Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Const ALTERNATE = 1 ' ALTERNATE and WINDING are
Const WINDING = 2 ' constants for FillMode.
Const BLACKBRUSH = 4 ' Constant for brush type.
Private Declare Function CreatePolyPolygonRgn Lib "gdi32" (lpPoint As POINTAPI, lpPolyCounts As Long, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long

Private Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function CreateRoundRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long
Private Declare Function CreateEllipticRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
Private Const RGN_OR = 2
Private Const RGN_XOR = 3


Private Sub Form_Click()
Unload Me
End Sub

Private Sub Form_Load()
    Me.AutoRedraw = True
    Dim vertex(0 To 6) As POINTAPI  ' holds vertices of each polygon
    Dim numvertices(0 To 1) As Long  ' holds how many vertices belong to each polygon
    Dim hRgn As Long  ' handle to the multi-polygonal region
    Dim retval As Long  ' return value
    Dim x As Integer
   
    Me.ScaleMode = vbPixels
    Me.ForeColor = RGB(0, 0, 99) ' toset the outline colour
   
    vertex(0).x = 25: vertex(0).y = 25  ' 1st point: (25,25)
    vertex(1).x = 50: vertex(1).y = 50  ' 2nd point: (50,50)
    vertex(2).x = 25: vertex(2).y = 50  ' 3rd point: (25,50)
    numvertices(0) = 3  ' three vertices for the triangle

    vertex(3).x = 150: vertex(3).y = 150  ' 1st point: (150,150)
    vertex(4).x = 200: vertex(4).y = 200  ' 2nd point: (200,200)
    vertex(5).x = 150: vertex(5).y = 250  ' 3rd point: (150,250)
    vertex(6).x = 100: vertex(6).y = 200  ' 4th point: (100,200)
    numvertices(1) = 4  ' four vertices for the triangle
    hBrush = CreateSolidBrush(RGB(255, 126, 25))
    hRgn = CreatePolyPolygonRgn(vertex(0), numvertices(0), 2, ALTERNATE)
   
    If hRgn Then FillRgn Me.hdc, hRgn, hBrush
    DeleteObject hRgn

    For x = 0 To numvertices(0) - 1
    If x <> numvertices(0) - 1 Then
    Me.Line (vertex(x).x, vertex(x).y)-(vertex(x + 1).x, vertex(x + 1).y)
    Else
    Me.Line (vertex(x).x, vertex(x).y)-(vertex(0).x, vertex(0).y)
    End If
    Next x
   
    For x = 3 To (numvertices(1) + 2)
    If x <> numvertices(1) + 2 Then
    Me.Line (vertex(x).x, vertex(x).y)-(vertex(x + 1).x, vertex(x + 1).y)
    Else
    Me.Line (vertex(x).x, vertex(x).y)-(vertex(3).x, vertex(3).y)
    End If
    Next x
   


End Sub


Private Sub pCreateSkin()
    Dim lRgnTmp   As Long
    Dim lSkinRgn  As Long
    Dim lWidth As Long
    Dim lHeight As Long
    Left = (Screen.Width / 2) - (ScaleWidth / 2)
    Top = (Screen.Height / 2) - (ScaleHeight / 2)
    lWidth = (ScaleWidth) / Screen.TwipsPerPixelX
    lHeight = (ScaleHeight) / Screen.TwipsPerPixelY
   lSkinRgn = CreateRoundRectRgn(86, 83, 659, 481, 14, 14)
   lRgnTmp = CreateEllipticRgn(8, 7, 166, 165)
    CombineRgn lSkinRgn, lSkinRgn, lRgnTmp, RGN_OR
    Call DeleteObject(lRgnTmp)
    Call SetWindowRgn(hWnd, lSkinRgn, True)
End Sub

ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great solution ryancys, love it...
i opened a new question on how to add text, if you have any ideas i would be happy
https://www.experts-exchange.com/questions/20967356/Adding-text-to-a-PolyPolygon-region-limited-by-it-on-a-picture-box.html