Results 1 to 8 of 8

Thread: mouse over control

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    *.*
    Posts
    85

    mouse over control

    The following code is a way to use API to tell when a control no longer has the mouse over it. Any ideas how to find when a control gets the mouse over it using API?

    Code:
    Public Const TME_CANCEL = &H80000000
    Public Const TME_HOVER = &H1&
    Public Const TME_LEAVE = &H2&
    Public Const TME_NONCLIENT = &H10&
    Public Const TME_QUERY = &H40000000
    Public Const WM_MOUSELEAVE = &H2A3&
    
    Public Type TRACKMOUSEEVENTTYPE
        cbSize As Long
        dwFlags As Long
        hwndTrack As Long
        dwHoverTime As Long
    End Type
    
    Public Declare Function TrackMouseEvent Lib "user32" (lpEventTrack As TRACKMOUSEEVENTTYPE) As Long
    Public Declare Function SetCursorPos Lib "user32.dll" (ByVal x As Long, ByVal y As Long) As Long
    
    Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    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 Const GWL_WNDPROC = (-4)
    Public PrevProc As Long
    
    Public Sub HookForm(F As Control)
    
    PrevProc = SetWindowLong(F.hWnd, GWL_WNDPROC, AddressOf WindowProc)
    
    End Sub
    Public Sub UnHookForm(F As Control)
    
    SetWindowLong F.hWnd, GWL_WNDPROC, PrevProc
    
    End Sub
    Public Function WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    If uMsg = WM_MOUSELEAVE Then
        'if we receive a WM_MOUSELEAVE message, show it
        Form1.Print "The mouse left the button!"
    End If
    
    WindowProc = CallWindowProc(PrevProc, hWnd, uMsg, wParam, lParam)
    
    End Function
    --------------------------------------------------------------------------------
    into a form
    visual basic code:--------------------------------------------------------------------------------Private Sub command1_Click()
    
    Dim ET As TRACKMOUSEEVENTTYPE
    'initialize structure
    ET.cbSize = Len(ET)
    ET.hwndTrack = Command1.hWnd
    ET.dwFlags = TME_LEAVE
    'start the tracking
    TrackMouseEvent ET
    'show a message to the user
    Me.Print "Move the mouse cursor outside the button" + vbCrLf + "to generate a WM_MOUSELEAVE event"
    
    End Sub
    Private Sub Form_Load()
    
    MsgBox "WARNING: This sample uses subclassing." + vbCrLf + "To end this program, always use the X button of the form." + vbCrLf + "Do not use VB's Stop button and do not use the 'End' keyword in your VB code." + vbCrLf + vbCrLf + "For more information about subclassing, check out" + vbCrLf + "our subclassing tutorial at http://www.allapi.net/", vbExclamation
    'set the graphics mode to persistent
    Me.AutoRedraw = True
    Me.Print "Click the button to begin"
    'start subclassing this form
    HookForm Command1
        
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
    
    'stop subclassing this control
    UnHookForm Command1
    
    End Sub
    Everytime

    "I'm not normally a religious man, but if you're up there, save me, Superman!" Homer Simpson

    Visit my site

  2. #2
    Megatron
    Guest
    Add the following code to a Form with a Timer. Set it's Interval to 1.
    VB Code:
    1. Private Type POINTAPI
    2.     x As Long
    3.     y As Long
    4. End Type
    5. Private Type RECT
    6.     Left As Long
    7.     Top As Long
    8.     Right As Long
    9.     Bottom As Long
    10. End Type
    11. Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    12. Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    13.  
    14. Private Sub Timer1_Timer()
    15.  
    16.     Dim PT As POINTAPI
    17.     Dim RC As RECT
    18.     GetWindowRect hwnd_of_window, RC
    19.     GetCursorPos PT
    20.        
    21.     If (PT.x > RC.Left And PT.x < RC.Right) And (PT.y > RC.Top And PT.y < RC.Bottom) Then
    22.         'Mouse is over it
    23.     End If
    24.    
    25. End Sub

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    *.*
    Posts
    85
    thanks

    is there a bad overhead on cpu usage (or whatever) with having such a timer?
    Everytime

    "I'm not normally a religious man, but if you're up there, save me, Superman!" Homer Simpson

    Visit my site

  4. #4
    Megatron
    Guest
    It's not the best method, but with today's computers, you probably won't notice the different. If you want though, you could set the Interval to a higher number so that the code doesn't execute as much.

    Do you need this to work for all windows? Or just windows that are in your project?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    *.*
    Posts
    85
    it's just for windows in the project so it shouldn't be too stressful. Thanks for the idea
    Everytime

    "I'm not normally a religious man, but if you're up there, save me, Superman!" Homer Simpson

    Visit my site

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Here are a couple of examples for detecting when a mouse is over a control, when it has left the control, and doing certain things to the control depending on where the mouse is, without using a timer.
    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. 'From VB2TheMax.Com
    6. 'MouseEnter And MouseExit Code
    7.  
    8. 'NOTE:   To use this code to change the background color of a command
    9. 'button, the command button style MUST be set to graphical
    10.  
    11. Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    12. If (X < 0) Or (Y < 0) Or (X > Command1.Width) Or (Y > Command1.Height) Then ' the MOUSELEAVE pseudo-event
    13.        ReleaseCapture ' in this example revert the caption to normal
    14.        Command1.Font.Bold = False
    15. ElseIf GetCapture() <> Command1.hwnd Then ' the MOUSEENTER pseudo-event
    16.        SetCapture Command1.hwnd ' in this example, make the caption bold
    17.        Command1.Font.Bold = True
    18. End If
    19. End Sub
    20. 'You can apply this tip to all controls that have the hWnd property, such as PictureBox,
    21. 'ListBox, etc, for example to easily implement hot-tracking effects. Here is another example
    22. 'that selects the text of a textbox (Text1) when the cursor is over.
    23.  
    24. Private Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, _
    25.     Y As Single)
    26.     If (X Or Y) < 0 Or (X > Text1.Width) Or (Y > Text1.Height) Then
    27.         ReleaseCapture
    28.         Text1.SelLength = 0
    29.     ElseIf GetCapture() <> Text1.hWnd Then
    30.         SetCapture Text1.hWnd
    31.         Text1.SelStart = 0
    32.         Text1.SelLength = Len(Text1)
    33.         Text1.SetFocus
    34.     End If
    35. End Sub
    36.  
    37. 'To change the backcolor of a command button
    38. Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    39.             If (X < 0) Or (Y < 0) Or (X > cmdClose.Width) Or (Y > cmdClose.Height) Then ' the MOUSELEAVE pseudo-event
    40.                  ReleaseCapture ' the mouse is no longer over the button, change backcolor to gray
    41.                        cmdClose.BackColor = &HC0C0C0
    42.             ElseIf GetCapture() <> cmdClose.hwnd Then ' the MOUSEENTER pseudo-event
    43.                 SetCapture cmdClose.hwnd ' the mouse is over the button, change backcolor to cyan
    44.                        cmdClose.BackColor = vbCyan
    45.             End If
    46. End Sub

  7. #7
    Megatron
    Guest
    That's for MouseEnter, and MouseLeave, not MouseOver.

    Everytime: Personally, I don't see anything wrong with using a Timer. In order to check a "MouseOver" event, you'll inevitably need to use some type of loop, so might as well use VB's Timer.

  8. #8
    Fanatic Member tim_l_012's Avatar
    Join Date
    Mar 2001
    Location
    Next to a Coffee Cup.
    Posts
    641
    is there any point in using an API timer?
    /: Tim :\____________________
    \: VB, HTML, ASP, VBScript, QBASIC, JavaScript :/

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