Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hotspots triggering a click event (inVB!)

Status
Not open for further replies.

briggsy79

Programmer
Feb 23, 2001
68
0
0
SE
Please help me,
I would like ot divide a map i have into different areas
(hospots) using another program, i.e. Photoshop, Dreamweaver, whatever
will do it. And then each of those areas when clicked on will trigger a
click event. I have tried seperating the picture into different squared
areas but that does not work if i want to use angled areas, and is
exrtremly time consuming for irregular shapes.
The image can be in a picture box, a form, or a 3rd party control
(anything it needs!).

Please offer any advice you may have on the subject.

Thank you very much


-David
 
I would put the cordination of border lines in a group of arrays, then use the (x,y) from mouse click to tell which part of the map the user is on and give response accordingly.

Jason
 
It is probably a lot of hard work, but consider using Windows' GDI regions. Here is a rough example. You'll need a form with the scalemode set to pixels:
[tt]
Option Explicit
Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Private Declare Function Polyline Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI, ByVal nCount As Long) As Long

Private Declare Function BeginPath Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function EndPath Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function FillPath Lib "gdi32" (ByVal hdc 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 Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Const ALTERNATE = 1
Private Const WINDING = 2


' Just one region handle
' In a map with several hotspots you would want a collection of these
Public hrgn As Long

' Used to define vertices of hotspot
Private mypoints() As POINTAPI



Private Sub Form_Load()
ReDim mypoints(5)

' Define vertices
mypoints(0).X = 5
mypoints(0).Y = 5
mypoints(1).X = 50
mypoints(1).Y = 25
mypoints(2).X = 100
mypoints(2).Y = 5
mypoints(3).X = 100
mypoints(3).Y = 100
mypoints(4).X = 5
mypoints(4).Y = 100
mypoints(5).X = 5
mypoints(5).Y = 5

' Specify path to be filled
BeginPath Form1.hdc
Polyline Form1.hdc, mypoints(0), 6
EndPath Form1.hdc

' Fill it
Form1.FillColor = RGB(255, 0, 0)
FillPath Form1.hdc

' Create a GDI region that matches our filled shape
hrgn = CreatePolygonRgn(mypoints(0), 6, ALTERNATE)
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

' Is mouse over our GDI region (which matches the filled shape)?
If PtInRegion(hrgn, X, Y) = 1 Then
Form1.FillColor = RGB(0, 255, 0) ' Yes, so fill in green
Else
Form1.FillColor = RGB(255, 0, 0) ' No, so fill in red
End If

BeginPath Form1.hdc
Polyline Form1.hdc, mypoints(0), 6
EndPath Form1.hdc
FillPath Form1.hdc

FillPath Form1.hdc

Form1.Refresh

End Sub
 
for the border line thing you can draw out the lay out of the hotspots in a paint program using a single pixel brush and a different color for each hotspot.

have a border type

Private Type Borders
X1 As Integer
X2 As Integer
HotSpotID as Integer
End Type

Private Type Borders2
Bounds() as Borders
End Type

Dim Borders(ScreenHeight) as Borders2



then scan the hotspot map row by row until you get to the first non zero point, Redimension Borders(YRow).Bounds(Ubound+1), and put the value of this point in HotSpotID and the x cooridinate in X1, then the next non zero point is X2. And restart this process for the rest of the line...

then to get the region that the mouse is over, all you have to do is take the Y coordinate of the mouse, take the Upper Bound of Borders(Y).Bounds, and scan through each set until the X cooridinate lies between X1 and X2 and then the HotSpotID will tell you where the mouse is.

that's pretty efficient.

an easier but not so efficient way of doing it would be to just draw a picture which represents where the hot spots are using a different color for each region(each region being filled with that color) and then just use the mouse (X,Y) coordinates as a direct pointer into this picture which will give you the hotspot region immediately. But you would probably want to make a VB program to scan it in and save this data to a file which is cleaner then setting up and array to point to the bmpBits of a picturebox.


 
Many of these solutions seem good, but i would imagine i would need to spend days working on the code to first understand it, and then to get it to work. A good easy to use 3rd party control seems like the quicker way to go, i will have a look at the deltabit control.

Thanks for all the suggestions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top