Solved

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

Posted on 2004-04-22
10
732 Views
Last Modified: 2008-01-09
I need someway to fill and then outline a region created by the CreatePolyPolygonRgn...
0
Comment
Question by:mSchmidt
  • 5
  • 4
10 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 10892530
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
0
 

Author Comment

by:mSchmidt
ID: 10893374
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...
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 10896815
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
0
 

Author Comment

by:mSchmidt
ID: 10897749
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
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 10900695
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
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:mSchmidt
ID: 10906525
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
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 10908242
Hi mSchmidt,

I will post it by tomorrow, now is 1.04AM at my local time, cheers
0
 
LVL 7

Expert Comment

by:OHDev2004
ID: 10909163
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

0
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 10910256
And to draw the outline, try use the FrameRgn API, the complete demo is shown as follows:

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 Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor 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 FrameRgn Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long, ByVal hBrush As Long, ByVal nWidth As Long, ByVal nHeight 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), 3, WINDING)
   
    hBrush = CreateSolidBrush(RGB(255, 126, 25))
    FillRgn Me.hdc, hRegion, hBrush
   
    hOutlineBrush = CreateSolidBrush(RGB(255, 0, 0))
    FrameRgn Me.hdc, hRegion, hOutlineBrush, 2, 3
   
    x = SetWindowRgn(Me.hwnd, hRegion, True)
   
    DeleteObject hRegion
End Sub

cheers
0
 

Author Comment

by:mSchmidt
ID: 10916505
Great solution ryancys, love it...
i opened a new question on how to add text, if you have any ideas i would be happy
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20967356.html
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now