We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 16,619 times

Related Categories

Mouse Out Event

One event that is noticably lacking from VB controls is a MouseOut event. The code below lets you create one. Please note that you need to change the hWnd that is checked in the tmrMouse_Timer() event to the control you want to monitor hovering over.

Add a timer to the form, called tmrMouse. Set its interval to 100. Also, add a command button called cmdHover.

'// VB Web Code Example
'// www.vbweb.co.uk
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Type POINTAPI
        X As Long
        Y As Long
End Type
Private bHovering As Boolean
Private Sub MouseOut()
    cmdHover.Caption = "Move Mouse Here"
    bHovering = False
End Sub

Private Sub cmdHover_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If bHovering = False Then
        '// this event occurs a lot.
        '// only change the caption when we need to
        bHovering = True
        cmdHover.Caption = "Hovering..."
    End If
End Sub

Private Sub tmrMouse_Timer()
    Dim lpPos As POINTAPI
    Dim lhWnd As Long
    '// get the current pos
    GetCursorPos lpPos
    '// see if the mouse is currently over our form
    lhWnd = WindowFromPoint(lpPos.X, lpPos.Y)
    '// if it isn't over the button, then call the MouseOut procedure
    If lhWnd <> cmdHover.hWnd And bHovering = True Then MouseOut
End Sub

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Working brain has no limits..

    Posted by eliminater on 12 Aug 2004

    hai, james.
    i am happy to see that u have a solution to this mouseout event.
    unfortunatly even i am using same logic since years. any way i will be glad to see if new techniques are developed in thi...