Results 1 to 4 of 4

Thread: mouse move

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    129

    mouse move

    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?
    Resizing
    System Balloons

    If your question is answered, please mark the thread resolved by pulling down the Thread Tools menu and clicking the Mark Thread Resolved button.

    Remember to rate a post if you find it helpful

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: mouse move

    Is this what you mean?
    VB Code:
    1. Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
    2. Private Declare Function ReleaseCapture Lib "user32" () As Long
    3. Private Declare Function GetCapture Lib "user32" () As Long
    4.  
    5. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    6. If (X < 0) Or (Y < 0) Or (X > Form1.Width) Or (Y > Form1.Height) Then
    7.        ReleaseCapture
    8.        Label1.Caption = "Mouse is no longer over the form"
    9. ElseIf GetCapture() <> Form1.hwnd Then
    10.        SetCapture Form1.hwnd
    11.        Label1.Caption = "Mouse is over the form"
    12. End If
    13. End Sub

  3. #3
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: mouse move

    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)

  4. #4
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: mouse move

    Subclass the form and trap WM_NCMOUSEMOVE and/or WM_NCMOUSELEAVE messages.
    VB Code:
    1. '[b]In Form1[/b]
    2. Option Explicit
    3.  
    4. Private Sub Form_Load()
    5.     ' Redirect messages to our new handler in the module and
    6.     ' save the pointer to the old handler
    7.     pOldWindPoc = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf WndProc)
    8. End Sub
    9.  
    10. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    11.     'Me.Caption = "Mouse is on client area"
    12. End Sub
    13.  
    14. Private Sub Form_Unload(Cancel As Integer)
    15.     ' Restore the previously saved message handler
    16.     SetWindowLong Me.hwnd, GWL_WNDPROC, pOldWindPoc
    17. End Sub
    VB Code:
    1. '[b]In a module[/b]
    2. Option Explicit
    3.  
    4. Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
    5.                           (ByVal lpPrevWndFunc As Long, _
    6.                            ByVal hwnd As Long, _
    7.                            ByVal msg As Long, _
    8.                            ByVal wParam As Long, _
    9.                            ByVal lParam As Long) As Long
    10. Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
    11.                           (ByVal hwnd As Long, _
    12.                            ByVal nIndex As Long, _
    13.                            ByVal dwNewLong As Long) As Long
    14.  
    15. ' A pointer to the old window procedure
    16. Public pOldWindPoc As Long
    17.  
    18. Private Const WM_NCMOUSELEAVE As Long = &H2A2
    19. Private Const WM_NCMOUSEMOVE As Long = &HA0
    20.  
    21. Public Const GWL_WNDPROC& = (-4)
    22.  
    23. ' Our new window procedure
    24. Public Function WndProc(ByVal hwnd As Long, _
    25.        ByVal uMsg As Long, _
    26.        ByVal wParam As Long, _
    27.        ByVal lParam As Long) As Long
    28.    
    29.     Select Case uMsg
    30.         Case WM_NCMOUSELEAVE:
    31.             Form1.Caption = "WM_NCMOUSELEAVE"
    32.             ' [b]Here you can use GetCursorPos to check[/b]
    33.             ' [b]if the mouse is over the form[/b]
    34.         Case WM_NCMOUSEMOVE:
    35.             Form1.Caption = "WM_NCMOUSEMOVE"
    36.     End Select
    37.    
    38.     WndProc = CallWindowProc(pOldWindPoc, hwnd, uMsg, wParam, lParam)
    39.    
    40. End Function
    Last edited by iPrank; May 30th, 2006 at 11:36 AM.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width