Results 1 to 6 of 6

Thread: CreatePolygonRgn: region handle is always zero

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    Brighton, England
    Posts
    112

    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

  2. #2
    Fanatic Member crispin's Avatar
    Join Date
    Aug 2000
    Location
    2 clicks west of a Quirkafleeg...Cornwall, England
    Posts
    754
    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]

  3. #3
    Fanatic Member crispin's Avatar
    Join Date
    Aug 2000
    Location
    2 clicks west of a Quirkafleeg...Cornwall, England
    Posts
    754
    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]

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    Brighton, England
    Posts
    112
    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




  5. #5
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    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

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    Brighton, England
    Posts
    112
    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
  •  



Click Here to Expand Forum to Full Width