RectInRegion Function

Declare Function RectInRegion Lib "gdi32.dll" (ByVal hRgn As Long, lpRect As RECT) As Long

Platforms: Win 32s, Win 95/98, Win NT

RectInRegion determines if a rectangle lies within a given region. The rectangle is considered to be inside the region if any portion of it -- not necessarily all of it -- lies within the region. The function returns 0 if the rectangle is completely outside the region, or a non-zero value if the rectangle is at least partially within the region.

hRgn
A handle to the region to determine if the rectangle lies within.
lpRect
The rectangle to determine if it lies at least partially within 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 window Form1 at least partially lies within the region..
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 winrect As RECT  ' receives Form1's rectangle
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 rectangle of window Form1, identifying the corners of the window.
retval = GetWindowRect(Form1.hWnd, winrect)
' Determine if the rectangle lies within the region.
isinside = RectInRegion(hRgn, winrect)  ' is the rectangle in the region?
If isinside = 0 Then  ' not inside
  Debug.Print "Form1 is completely outside the region."
Else
  Debug.Print "Form1 lies at least partially inside the region."
End If

' Delete the region to free up resources.
retval = DeleteObject(hRgn)

See Also: PtInRegion
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/r/rectinregion.html