|
-
Sep 17th, 2001, 06:38 AM
#1
Thread Starter
Lively Member
CreatePolygonRgn: region handle is always zero
Hi,
I'm using the CreatePolygonRgn API and passing real world easting and northings. Howvere the region handle returned is always zero, has anyone got any ideas where I'm going wrong???
cheers
john
-
Sep 17th, 2001, 07:07 AM
#2
Fanatic Member
got any code to post John....
Crispin
VB6 ENT SP5
VB.NET
W2K ADV SVR SP3
WWW.BLOCKSOFT.CO.UK
[Microsoft Basic: 1976-2001, RIP]
-
Sep 17th, 2001, 07:07 AM
#3
Fanatic Member
From allapi:
Code:
Private Type COORD
x As Long
y As Long
End Type
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_Paint()
'KPD-Team 1999
'URL: http://www.allapi.net/
'E-Mail: [email protected]
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)
' 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
Private Sub Form_Resize()
Form_Paint
End Sub
hope this helps....
Crispin
VB6 ENT SP5
VB.NET
W2K ADV SVR SP3
WWW.BLOCKSOFT.CO.UK
[Microsoft Basic: 1976-2001, RIP]
-
Sep 17th, 2001, 08:09 AM
#4
Thread Starter
Lively Member
Hi crispin,
what I'm trying to do is find if a point is inside a polygon. I'm loading two text files, one contains the points making the polygon, the other contains a list of points which I need to check to see if they are inside or outside the polygon.
In a module I have the following code:
Option Explicit
Type POINTAPI
x As Double
y As Double
End Type
Public Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI, ByVal nCount As Long) As Long
Public Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Public Declare Function PtInRegion Lib "gdi32" (ByVal hRgn As Long, ByVal x As Long, ByVal y As Long) As Long
Then in my project code:
Dim lXY() As POINTAPI
''''open files etc etc then
For lCount = 0 To lNoPts
Line Input #iFile, cData
varLine = Split(cData, " ")
dPtX(lCount) = Val(varLine(0))
dPtY(lCount) = Val(varLine(1))
lXY(1).x = dPtX(lCount)
lXY(1).y = dPtY(lCount)
lRegion = CreatePolygonRgn(lXY(1), lNoPts, 1)
Next
that's it, so I'm not actually trying to draw the polygon or do anything where I detect a mouse click.
Do I have to convert from real world co-ords to pixels or something??, if so how do I do it?, or is this a red herring??
any ideas??
cheers
john
-
Sep 17th, 2001, 09:21 PM
#5
Fanatic Member
You're attempting to create a polygon inside a loop repeatedly, and most likely that's what causing this. You only need to create the polygon once. Plus you're losing the original region (if any), the next time you get to the CreatePolygonRgn() call, and that will be eating up GDI memory. If you want to do point testing with regions, you should fill up your polygon points first, then create the region. You might want to use purely mathmatical ways to test though rather than regions (and after going back to search I see that you were the one that Kedaman and I were talking about for the math ways).
Last edited by Kaverin; Sep 17th, 2001 at 09:25 PM.
I'm baaaack...
VB5 Professional Edition, VC++ 6
Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se
I feel special because I finally figured out how to loop midis: Post link
I'm a fanatic too 
-
Sep 18th, 2001, 08:07 AM
#6
Thread Starter
Lively Member
thanks for the help Kaverin, I have managed to do it now using a maths way and this seems to be working fine.
thanks again
john
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|