Solved

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

Posted on 2004-04-22
Medium Priority
784 Views
I need someway to fill and then outline a region created by the CreatePolyPolygonRgn...
0
Question by:mSchmidt
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 4

LVL 53

Expert Comment

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.

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

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 53

Expert Comment

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

Command2_Click
End Sub

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

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 53

Expert Comment

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

Author Comment

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 53

Expert Comment

ID: 10908242
Hi mSchmidt,

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

LVL 7

Expert Comment

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()
End Sub

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 53

Accepted Solution

Ryan Chong earned 2000 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

Command2_Click
End Sub

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

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library aâ€¦
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone â€¦
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that aâ€¦
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â€¦
###### Suggested Courses
Course of the Month10 days, 19 hours left to enroll