Results 1 to 7 of 7

Thread: VB - Simulate MouseEnter and MouseLeave events...

  1. #1

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    VB - Simulate MouseEnter and MouseLeave events...

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
    4. Private Declare Function ReleaseCapture Lib "user32" () As Long
    5. Private Declare Function GetCapture Lib "user32" () As Long
    6.  
    7. Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    8.    
    9.     With Command1
    10.         If (X < 0) Or (Y < 0) Or (X > .Width) Or (Y > .Height) Then 'MouseLeave
    11.             Call ReleaseCapture
    12.         ElseIf GetCapture() <> .hwnd Then 'MouseEnter
    13.             Call SetCapture(.hwnd)
    14.         Else
    15.             'Normal MouseMove
    16.         End If
    17.     End With
    18.  
    19. End Sub

    Replace Command1 with the name of the control. The control must have an hWnd property, and support the MouseMove event for this to work.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  2. #2

  3. #3

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Sorry, no.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  4. #4

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    I use it mainly for painting certain effects on usercontrols when the mouse is over it. Like toolbar buttons. See attached.

    Attached Files Attached Files
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Another way to do this. It uses a timer, does not need the mousemove event and also needs the hwnd property :

    VB Code:
    1. Option Explicit
    2.  
    3. Private Type RECT
    4.     Left As Long
    5.     Top As Long
    6.     Right As Long
    7.     Bottom As Long
    8. End Type
    9.  
    10. Private Type POINTAPI
    11.     X As Long
    12.     Y As Long
    13. End Type
    14.  
    15.  
    16. Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    17. Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    18.  
    19. Private Sub Form_Load()
    20.     Timer1.Interval = 1
    21.     Timer1.Enabled = True
    22. End Sub
    23.  
    24. Private Sub Timer1_Timer()
    25.     Dim Rec As RECT, Point As POINTAPI
    26.    
    27.     GetWindowRect Command1.hwnd, Rec
    28.    
    29.     GetCursorPos Point
    30.  
    31.     If Point.X >= Rec.Left And Point.X <= Rec.Right And Point.Y >= Rec.Top And Point.Y <= Rec.Bottom Then
    32.         Me.Caption = "Over Button"
    33.     Else
    34.         Me.Caption = "Not Over Button"
    35.     End If
    36. End Sub


    Has someone helped you? Then you can Rate their helpful post.

  6. #6
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Like the other version, it needs a timer and the hwnd :

    VB Code:
    1. Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    2. Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    3.  
    4. Private Type POINTAPI
    5.     X As Long
    6.     Y As Long
    7. End Type
    8.  
    9. Private Sub CheckMouse(Control As Control)
    10. Dim mhWnd As Long
    11. Dim Pt As POINTAPI
    12.  
    13. GetCursorPos Pt
    14.  
    15. mhWnd = WindowFromPoint(Pt.X, Pt.Y)
    16. If mhWnd = Control.hwnd Then
    17.     Debug.Print "Mouse Is Over " & Control.Name
    18. Else
    19.     Debug.Print "Mouse Isn't Over " & Control.Name
    20. End If
    21. End Sub
    22.  
    23. Private Sub Timer1_Timer()
    24. CheckMouse Command1
    25. End Sub
    Last edited by manavo11; Aug 4th, 2003 at 08:44 PM.


    Has someone helped you? Then you can Rate their helpful post.

  7. #7
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Another way, just a timer and no hwnd or mousemove event :

    VB Code:
    1. Option Explicit
    2.  
    3. Private Type POINTAPI
    4.     X As Long
    5.     Y As Long
    6. End Type
    7.  
    8. Private Declare Function GetCursorPos Lib "user32" _
    9. (lpPoint As POINTAPI) As Long
    10.  
    11. Private Declare Function ScreenToClient Lib "user32" _
    12. (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
    13.  
    14. Dim Pt As POINTAPI
    15.  
    16. Private Sub CheckMouse(Control As Control)
    17.  
    18. Dim SM As Single
    19.  
    20. GetCursorPos Pt
    21. ScreenToClient Me.hwnd, Pt
    22.  
    23. SM = Me.ScaleMode
    24. Me.ScaleMode = vbPixels
    25.  
    26. If (Pt.X > Control.Left And Pt.X < Control.Left + Control.Width) And (Pt.Y > Control.Top And Pt.Y < Control.Top + Control.Height) Then
    27.     Debug.Print "Over"
    28. Else
    29.     Debug.Print "Not Over"
    30. End If
    31.  
    32. Me.ScaleMode = SM
    33.  
    34. End Sub
    35.  
    36. Private Sub Timer1_Timer()
    37. CheckMouse Label1
    38. End Sub


    Has someone helped you? Then you can Rate their helpful post.

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