How do i use the PtInRegion API Function?...heres my code

andreww
03-07-2002, 03:50 AM
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 DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long




Private Sub Form_Paint()

Dim poly(1 To 3) As COORD, NumCoords As Long, hRgn As Long

NumCoords = 3

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 Me.hdc, poly(1), NumCoords


End Sub



-------------------

ok i got this code from a web page and made it shorter so i could understand it, now i want to detect if a point is inside the region that i just made above...


i tried getting examples off the net but they were to complicated

so could someone write me some code on how to use the point in region command?

ChiefRedBull
03-07-2002, 05:49 AM
Declare Function PtInRegion Lib "gdi32" Alias "PtInRegion" _
(ByVal hRgn As Long, ByVal x As Long, ByVal y As Long) As Long

Parameters:
hrgn
Identifies the region to be examined.

X
Specifies the x-coordinate of the point.

Y
Specifies the y-coordinate of the point.

Return values:
If the specified point is in the region, the return value is nonzero.

If the specified point is not in the region, the return value is zero. That seems fairly self explanatory really.... :D

Squirm
03-07-2002, 10:10 AM
At no point in the code you pasted did you actually create a region. You need to actually use the CreatePolygonRegion API. Your code will only draw shape.

Flyguy
03-07-2002, 03:11 PM
Instead of API I had some function somewhere hidden in an old program:

Function pnpoly(npol As Integer, poly() As Point, X As Integer, Y As Integer) As Boolean
Dim inpoly As Boolean
Dim i As Integer, j As Integer

i = 0
j = npol - 1
inpoly = False
While (i < npol)
If ((((poly(i).Y <= Y) And (Y < poly(j).Y)) Or _
((poly(j).Y <= Y) And (Y < poly(i).Y)))) Then
If (X < (poly(j).X - poly(i).X) * (Y - poly(i).Y) / (poly(j).Y - poly(i).Y) + poly(i).X) Then
inpoly = Not (inpoly)
End If
End If
j = i
i = i + 1
Wend
pnpoly = inpoly
End Function

andreww
03-07-2002, 04:08 PM
ah yeah squirm

ive just tried adding createpolyrgn and it keeps coming up with errors, you wouldn't know what line i have to type in to get it working? ie createpoly etc


ive already tried looking and vb6s help but their example doesnt even work..

Squirm
03-07-2002, 04:15 PM
Considering the API is called CreatePolygonRgn then I would start with that.

CreatePolygonRgn myPointArray, myPointcount, myFillmode

andreww
03-14-2002, 08:27 PM
heres my code , (just add it into a blank form

i cant see what is wrong with this code using the pointinrgn method

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 DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function PtInRegion Lib "gdi32" (ByVal hRgn As Long,
ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As
Long, ByVal hObject As Long) As Long
Dim poly(1 To 3) As COORD, NumCoords As Long, hBrush As Long, hRgn As Long
Dim hOldBrush As Long

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If PtInRegion(hRgn, X, Y) = 0 Then Me.Caption = "out" Else Me.Caption = "in"
End Sub
Private Sub Form_Paint()
Me.ScaleMode = 3
hBrush = CreateSolidBrush(vbRed)
hOldBrush = SelectObject(Me.hdc, hBrush)
NumCoords = 3
Me.ScaleMode = vbPixels
poly(1).X = 0
poly(1).Y = 0
poly(2).X = 200
poly(2).Y = 100
poly(3).X = 150
poly(3).Y = 200
Polygon Me.hdc, poly(1), NumCoords
hRgn = CreatePolygonRgn(poly(1), NumCoords, ALTERNATE)
If hRgn Then FillRgn Me.hdc, hRgn, hBrush
'DeleteObject hRgn
'DeleteObject hBrush
End Sub

Pookie
03-14-2002, 11:21 PM
Maybe I'm not following this thread properly, but in your last reply, you have to put an underscore on the end of the statements to use them on multiple lines:


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 PtInRegion Lib "gdi32" (ByVal hRgn As Long, _
ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As _
Long, ByVal hObject As Long) As Long


Copy and paste that.

andreww
03-14-2002, 11:27 PM
it only on multiple lines as this webpage cant fit the whole line on one line so it puts it onto multiple

Flyguy
03-15-2002, 01:20 AM
Use the & tags.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum