Hi all,
Anyone know how to create click spots on background image (like mapping in HTML) please help. I am stuck on my project.
Thanks in advance.
Printable View
Hi all,
Anyone know how to create click spots on background image (like mapping in HTML) please help. I am stuck on my project.
Thanks in advance.
use transparent labels
or use image boxes that contain nothing
You want to have rectangular areas that can be clicked on?
Well here's a good sample for you:
Code:Private Declare Function PtInRect Lib "user32" (lpRect As RECT, pt As POINTAPI) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type POINTAPI
X As Long
Y As Long
End Type
Private clickspots() As RECT
Private Sub Form_Load()
ReDim clickspots(1)
With clickspots(0)
.Left = 50
.Right = 100
.Top = 50
.Bottom = 100
End With
With clickspots(1)
.Left = 250
.Right = 200
.Top = 50
.Bottom = 100
End With
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim pt As POINTAPI, n&
pt.X = X
pt.Y = Y
For n = 0 To UBound(clickspots)
If PtInRect(clickspots(n), pt) Then
Debug.Print "clicked at area " & n
End If
Next n
End Sub
Thanks for your help!
Kedaman, where would I place the top part which is the Private funtions?!! Can you show me because it show me the error when I move it to module.
Thanks for your help again.
Don't make this more difficult than it should be.
If you want "click spots" (commonly known as hotspots)
then just create (as someone previously mentioned) a label
that is either transparent or a text frame with nothing in it. This should require no code whatsoever.
I do this in my applications when i'm planting my easter
eggs :P
Just place everything that Kedaman posted in your form code. If you want to put the declarations in a module, make them Public.
However, I tried the code and it bombs out VB with an application error.
This line:
If PtInRect(clickspots(n), pt) Then
generates an Application Error that memory cannot be read.
Any suggestions ?
Thanks.
I'm a slow learner here, please be patient with me:)
I create a label but it's overlapped my background image?
I would like to know do i miss to do something?!!
no problem, everyone has to start somewhere :)
anyways, in design mode, right click on the label and click
on 'Send to Back'
this moves it behind the picture or whatever else you want on top.
hmm ptinrect really crashed, i just picked up that api because it was missing, now i found there was another module with the same function, well was years ago so how could i know
replace the api with this insteadCode:Private Function ptinrect(rect As rect, pt As POINTAPI) As Boolean
ptinrect = pt.X > rect.Left And pt.X < rect.Right And pt.Y > rect.Top And pt.Y < rect.Bottom
End Function
Thank you so much!! I got it but I use the imagebox(none) instead of label:)
Noble, I have to do one more step. Can I swap the image whenever I point the mouse to that location? (only change the image of that location)
I create another same background image but in diff color, I want it cut the same section of the second image and replace to the first background when i move the mouse to the location? any ideas how to do it?!!
that sounds tricky
first of all, do you want it to change when you move the
mouse over it or when you actually click on it?
if you want it to change when you click on it, you can
have two pictures boxes, each one displays a different
picture
when you click on one, it sets it's visible property to
false and the visible property of the other image to true
and vice versa for the other picture.
If you want it to change when you simply move the mouse
over it, you're gonna have to experiment with the API
as i'm not sure the best way to do that.
I know there is an API that
tells you the mouse's position on the screen but i don't
know if that's the best way to go about it.
I hope this helps.
Thanks Nobel
Someone told me to use buffer to hold the images and select cases. It is too complicated for me.:)
in th top piece of code you don't need to redefine RECT, there is already a rect type:
Type Rect
x1 as long
x2 as long
y1 as long
y2 as long
end type
That type already exists, as for getting mouse position, theres always the VB event Mouse_Event which passes the mouse co-ords as it moves over the object which you can code in, its not perfect thou if you move the mouse over it quickly.
Where do you find that RECT Nirces? It's at least not in the typelib that comes with my version of vb.