Declare Function PtInRegion Lib "gdi32.dll" (ByVal hRgn As Long, ByVal x As Long, ByVal y As Long) As Long
Platforms: Win 32s, Win 95/98, Win NT
PtInRegion determines if a given point lies within a region. The point is considered to be inside the region if it is within the area bounded by the region. The function returns 0 if the point is not inside the region, or a non-zero value if the point is in the region.
Example:
' Consider a line connecting the upper-right and lower-left corners of the
' screen, and consider the region made of the upper-left side of this line. Determine
' if the mouse cursor lies within this region or not.
Dim swidth As Long, sheight As Long ' width and height of the screen
Dim hRgn As Long ' handle to the triangular region explained above
Dim curpos As POINT_TYPE ' receives location of mouse cursor
Dim vertices(0 To 2) As POINT_TYPE ' vertices of region to create
Dim isinside As Long ' receives 0 if not inside, non-zero otherwise
Dim retval As Long ' generic return value
' Get the screen's width and height. Use this information to create the region.
swidth = GetSystemMetrics(SM_CXSCREEN) ' screen width
sheight = GetSystemMetrics(SM_CYSCREEN) ' screen height
' Load region's vertices into the array and create it.
vertices(0).x = 0: vertices(0).y = 0 ' vertex #1: upper-left corner of screen
vertices(1).x = swidth: vertices(1).y = 0 ' vertex #2: upper-right corner of screen
vertices(2).x = 0: vertices(2).y = sheight ' vertex #3: lower-left corner of screen
hRgn = CreatePolygonRgn(vertices(0), 3, ALTERNATE) ' create the region
' Get the current position of the mouse cursor.
retval = GetCursorPos(curpos)
' Determine if the cursor location lies within the region.
isinside = PtInRegion(hRgn, curpos.x, curpos.y) ' is the point in the region?
If isinside = 0 Then ' not inside
Debug.Print "The cursor is not inside the region."
Else
Debug.Print "The cursor is inside the region."
End If
' Delete the region to free up resources.
retval = DeleteObject(hRgn)
See Also: RectInRegion
Category: Regions
Go back to the alphabetical Function listing.
Go back to the Reference section index.
This page is copyright © 2000 Paul Kuliniewicz. Copyright Information.
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/p/ptinregion.html