I need a good way to tell if the mouse has left the control it is/was over. I need this soon, so any help would be appreciated.
Printable View
I need a good way to tell if the mouse has left the control it is/was over. I need this soon, so any help would be appreciated.
Add the following to a Form with a Timer.
Code:Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Dim LastCtl As Long
Private Sub Form_Load()
Timer1.Interval = 1
End Sub
Private Sub Timer1_Timer()
Dim Wnd As Long
Dim PT As POINTAPI
GetCursorPos PT
Wnd = WindowFromPoint(PT.x, PT.y)
If Wnd = LastCtl Then Exit Sub
LastCtl = Wnd
'<--Mouse moved to a different window-->
Print "Mose moved to different window"
End Sub
Megatron:
Thanks for the code. But maybe you can help some more.
What I'd like to do with this code is that when the cursor is over a label the label becomes bold faced, but when it leaves it needs to go back. In the past I placed code in the form_mousemove event to accomplish this but it's not that affective if the controls and close together.
The Microsoft SoftButtons control is a great code example.. check http://msdn.microsoft.com/ and you can find it there somewhere...
Also, with VB comes a demo activex control w/ source that does something like what you want. Check the samples.
If not, use the SetCapture API. I can't remember exactly what you do; sorry.
Add the following code to a Form with a Timer.
This uses a custom function called MouseOverLabel to check if the mouse is over the label specified in the 1st argument.
Code:Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Function MouseOverLabel(lbl As Label) As Boolean
Dim PT As POINTAPI
GetCursorPos PT
ScreenToClient hwnd, PT
If (PT.x > lbl.Left And PT.x < lbl.Left + lbl.Width) And (PT.y > lbl.Top And PT.y < lbl.Top + lbl.Height) Then
MouseOverLabel = True
End If
End Function
Private Sub Form_Load()
Timer1.Interval = 1
Me.ScaleMode = vbPixels
End Sub
Private Sub Timer1_Timer()
'Check if the mouse is over the following labels
If MouseOverLabel(Label1) = True Then Label1.FontBold = True Else Label1.FontBold = False
If MouseOverLabel(Label2) = True Then Label2.FontBold = True Else Label2.FontBold = False
End Sub
All you will ever need to implement is available at this URL. This uses the API's built in TrackMouseEvent for Win98/NT4 and emulates this for win95. Incorporate the MouseLeave event in your user control and you will have a winner.
CheersQuote:
Providing a MouseLeave event for a control. A helper for creating controls which track when the mouse is over them.
http://vbaccelerator.com/codelib/ssubtmr/msleave.htm