I have a program where you move the mouse on the form to work it. I want to be able to detect when i'm not on the form such as the when the mouse is on the blue bar on the top or on the taskbar or around the window. How can i do this?
Printable View
I have a program where you move the mouse on the form to work it. I want to be able to detect when i'm not on the form such as the when the mouse is on the blue bar on the top or on the taskbar or around the window. How can i do this?
Is this what you mean?VB Code:
Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function ReleaseCapture Lib "user32" () As Long Private Declare Function GetCapture Lib "user32" () As Long Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) If (X < 0) Or (Y < 0) Or (X > Form1.Width) Or (Y > Form1.Height) Then ReleaseCapture Label1.Caption = "Mouse is no longer over the form" ElseIf GetCapture() <> Form1.hwnd Then SetCapture Form1.hwnd Label1.Caption = "Mouse is over the form" End If End Sub
that's what i was going to suggest - but every control on the form needs to be clicked twice before it responds as the first click is sent to the Form (you can detect it with the mouse_up / mouse_down events)
Subclass the form and trap WM_NCMOUSEMOVE and/or WM_NCMOUSELEAVE messages.
VB Code:
'[b]In Form1[/b] Option Explicit Private Sub Form_Load() ' Redirect messages to our new handler in the module and ' save the pointer to the old handler pOldWindPoc = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf WndProc) End Sub Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) 'Me.Caption = "Mouse is on client area" End Sub Private Sub Form_Unload(Cancel As Integer) ' Restore the previously saved message handler SetWindowLong Me.hwnd, GWL_WNDPROC, pOldWindPoc End SubVB Code:
'[b]In a module[/b] Option Explicit Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _ (ByVal lpPrevWndFunc As Long, _ ByVal hwnd As Long, _ ByVal msg As Long, _ ByVal wParam As Long, _ ByVal lParam As Long) As Long Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _ (ByVal hwnd As Long, _ ByVal nIndex As Long, _ ByVal dwNewLong As Long) As Long ' A pointer to the old window procedure Public pOldWindPoc As Long Private Const WM_NCMOUSELEAVE As Long = &H2A2 Private Const WM_NCMOUSEMOVE As Long = &HA0 Public Const GWL_WNDPROC& = (-4) ' Our new window procedure Public Function WndProc(ByVal hwnd As Long, _ ByVal uMsg As Long, _ ByVal wParam As Long, _ ByVal lParam As Long) As Long Select Case uMsg Case WM_NCMOUSELEAVE: Form1.Caption = "WM_NCMOUSELEAVE" ' [b]Here you can use GetCursorPos to check[/b] ' [b]if the mouse is over the form[/b] Case WM_NCMOUSEMOVE: Form1.Caption = "WM_NCMOUSEMOVE" End Select WndProc = CallWindowProc(pOldWindPoc, hwnd, uMsg, wParam, lParam) End Function