[COLOR=blue]
Option Explicit
Private Points(1 To 5) As POINTAPI
Private RegionDictionary As Dictionary 'Requires that Microsoft Scripting Runtime library is added as Reference
Private Declare Function FrameRgn Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long, ByVal hBrush As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long
Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Private Declare Function PtInRegion Lib "gdi32.dll" (ByVal hRgn As Long, ByVal X As Long, ByVal Y As Long) As Long
Private Const BLACK_BRUSH = 4
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Sub Form_Initialize()
Set RegionDictionary = New Dictionary
End Sub
Private Sub Form_Load()
Dim lp As Long
Dim myrgn As Long
Me.ScaleMode = vbPixels ' avoids us having to muck about with converting coordinate spaces
Me.Show
DoEvents ' Make sure form is visible so we can see region outlines
' Set up 5 irregular(ish) shapes, and save them in a Dictionary
For lp = 0 To 225 Step 55
Points(1).X = lp
Points(1).Y = lp
Points(2).X = lp
Points(2).Y = lp + 50
Points(3).X = lp + 25
Points(3).Y = lp + 25
Points(4).X = lp + 50
Points(4).Y = lp + 50
Points(5).X = lp + 50
Points(5).Y = lp
myrgn = CreatePolygonRgn(Points(1), 5, 1)
FrameRgn Form1.hdc, myrgn, GetStockObject(BLACK_BRUSH), 1, 1
RegionDictionary.Add "Region " & (lp / 55) + 1, myrgn
Next
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim lp As Long
' loop through all regions to see if we get a hit
For lp = 0 To RegionDictionary.Count - 1
If PtInRegion(RegionDictionary.Items(lp), X, Y) Then
MsgBox "We are in: " & RegionDictionary.Keys(lp)
End If
Next
End Sub
Private Sub Form_Paint()
Dim lp As Long
' loop through all regions to see if we get a hit
For lp = 0 To RegionDictionary.Count - 1
FrameRgn Form1.hdc, RegionDictionary.Items(lp), GetStockObject(BLACK_BRUSH), 1, 1
Next
End Sub
[/color]