|
-
Feb 23rd, 2000, 04:54 AM
#1
Thread Starter
Frenzied Member
I finally came up with a solution to a problem that someone posted, but I can't find them now. He wanted to have a control (an image, I think) change whenever the mouse was over it, then change back when the mouse left it. Here is code that creates a "MouseOver" event for any control on your form.
Paste the following into a standard module:
Code:
Option Explicit
Type POINTAPI
X As Long
Y As Long
End Type
Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Function GetControlRect(ControlIn As Control, FormIn As Form) As RECT
GetControlRect.Left = (ControlIn.Left + FormIn.Left + 50) / _
Screen.TwipsPerPixelX
GetControlRect.Right = (ControlIn.Left + FormIn.Left + _
ControlIn.Width + 50) / Screen.TwipsPerPixelX
GetControlRect.Top = (ControlIn.Top + FormIn.Top + 325) / _
Screen.TwipsPerPixelY
GetControlRect.Bottom = (ControlIn.Top + FormIn.Top + _
325 + ControlIn.Height) / Screen.TwipsPerPixelY
End Function
Public Function MouseOver(ControlIn As Control, FormIn As Form) As Boolean
Dim CursorPos As POINTAPI
Dim ControlRect As RECT
GetCursorPos CursorPos
ControlRect = GetControlRect(ControlIn, FormIn)
If CursorPos.X > ControlRect.Left And _
CursorPos.X < ControlRect.Right And _
CursorPos.Y > ControlRect.Top And _
CursorPos.Y < ControlRect.Bottom Then
MouseOver = True
Else
MouseOver = False
End If
End Function
Usage (Probably best used in a timer):
If MouseOver(ControlName, FormName) then
'Highlight control here
Else
'Unhighlight control here
End If
Example:
Place a timer, a picturebox, and two labels on a form (make sure the timer's Interval property is set to 10 or less for a quick response time). Then place the following code in the form's code window:
Code:
Option Explicit
Private Sub Timer1_Timer()
If MouseOver(Label1, Me) Then
Label2 = "You're over the label"
ElseIf MouseOver(Picture1, Me) Then
Label2 = "You're over the picture"
Else
Label2 = "You aren't over any controls"
End If
End Sub
Hope that helps!
~seaweed
Edited by seaweed on 02-23-2000 at 04:56 PM
-
Dec 10th, 2001, 02:02 PM
#2
PowerPoster
Originally posted by seaweed
I finally came up with a solution to a problem that someone posted, but I can't find them now. He wanted to have a control (an image, I think) change whenever the mouse was over it, then change back when the mouse left it. Here is code that creates a "MouseOver" event for any control on your form.
Paste the following into a standard module:
Code:
Option Explicit
Type POINTAPI
X As Long
Y As Long
End Type
Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Function GetControlRect(ControlIn As Control, FormIn As Form) As RECT
GetControlRect.Left = (ControlIn.Left + FormIn.Left + 50) / _
Screen.TwipsPerPixelX
GetControlRect.Right = (ControlIn.Left + FormIn.Left + _
ControlIn.Width + 50) / Screen.TwipsPerPixelX
GetControlRect.Top = (ControlIn.Top + FormIn.Top + 325) / _
Screen.TwipsPerPixelY
GetControlRect.Bottom = (ControlIn.Top + FormIn.Top + _
325 + ControlIn.Height) / Screen.TwipsPerPixelY
End Function
Public Function MouseOver(ControlIn As Control, FormIn As Form) As Boolean
Dim CursorPos As POINTAPI
Dim ControlRect As RECT
GetCursorPos CursorPos
ControlRect = GetControlRect(ControlIn, FormIn)
If CursorPos.X > ControlRect.Left And _
CursorPos.X < ControlRect.Right And _
CursorPos.Y > ControlRect.Top And _
CursorPos.Y < ControlRect.Bottom Then
MouseOver = True
Else
MouseOver = False
End If
End Function
Usage (Probably best used in a timer):
If MouseOver(ControlName, FormName) then
'Highlight control here
Else
'Unhighlight control here
End If
Example:
Place a timer, a picturebox, and two labels on a form (make sure the timer's Interval property is set to 10 or less for a quick response time). Then place the following code in the form's code window:
Code:
Option Explicit
Private Sub Timer1_Timer()
If MouseOver(Label1, Me) Then
Label2 = "You're over the label"
ElseIf MouseOver(Picture1, Me) Then
Label2 = "You're over the picture"
Else
Label2 = "You aren't over any controls"
End If
End Sub
Hope that helps!
~seaweed
Edited by seaweed on 02-23-2000 at 04:56 PM
I tried to use your code for a lable control but all it did was always return a false return for the MouseOver function..
-
Dec 10th, 2001, 02:13 PM
#3
Need-a-life Member
That's not the way to do it.
http://forums.vb-world.net/showthrea...ght=setcapture
This is the best way I know to create a "rollover image" effect. The "how" you load the new image is up to you, there are several different way to do it: Loadpicture, a picture array, an ImageList, whatever. But the essence of the effect is shown in this code:
VB Code:
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
With Picture1
If (X < 0) Or (Y < 0) Or (X > .Width) Or (Y > .Height) Then
ReleaseCapture
.Picture = RollOver(0).Picture
Else
SetCapture .hwnd
.Picture = RollOver(1).Picture
End If
End With
End Sub
The PictureBox RollOver is an array, but you can use whateve method you like to load one or other image.
Emiliano F. Martín
If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
-
Dec 10th, 2001, 02:16 PM
#4
PowerPoster
thank you very much. Can u tell me how to change the mouse's icon when I do this effect?
thanks
-
Dec 10th, 2001, 02:20 PM
#5
Need-a-life Member
VB Code:
Screen.MouseIcon = vbCustom
Screen.MousePointer = Pictture1.Picture
Emiliano F. Martín
If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
-
Dec 10th, 2001, 02:29 PM
#6
also try the WindowFromPointer API, it returns the handle of the control that the mouse pointer is over it. You can make a timer and check to see if the mouse is over the picturebox (if the function returns the same hWnd as the picturebox's). I'm not sure if Picture_mousemove works always
-
Dec 10th, 2001, 02:30 PM
#7
PowerPoster
if I use this in my code
Code:
Label3.ForeColor = vbBlue
Me.MouseIcon = LoadPicture("C:\Program Files\Microsoft Visual Studio\Common\Graphics\Cursors\H_POINT.CUR")
Me.MousePointer = vbCustom
when I distribute how will the APP know where to find the path to the icon or is there a better way?
-
Dec 10th, 2001, 02:35 PM
#8
Need-a-life Member
Originally posted by jesus4u
if I use this in my code
Code:
Label3.ForeColor = vbBlue
Me.MouseIcon = LoadPicture("C:\Program Files\Microsoft Visual Studio\Common\Graphics\Cursors\H_POINT.CUR")
Me.MousePointer = vbCustom
when I distribute how will the APP know where to find the path to the icon or is there a better way?
It would look in the path you're saying in the code. So, if the user doens't get the path, or the cursor in that folder, you're app will raise an error.
Emiliano F. Martín
If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
-
Dec 10th, 2001, 02:36 PM
#9
PowerPoster
Originally posted by Mc Brain
It would look in the path you're saying in the code. So, if the user doens't get the path, or the cursor in that folder, you're app will raise an error.
Would it be better to place the icon in the same folder as the app then?
-
Dec 10th, 2001, 02:38 PM
#10
-
Dec 10th, 2001, 02:39 PM
#11
PowerPoster
is there any simpler way than that?
-
Dec 10th, 2001, 02:43 PM
#12
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|