Results 1 to 6 of 6

Thread: Detecting weather the mouse is still over a control.

  1. #1

    Thread Starter
    Fanatic Member RealisticGraphics's Avatar
    Join Date
    Jul 1999
    Location
    Arkansas
    Posts
    655

    Unhappy

    I need a good way to tell if the mouse has left the control it is/was over. I need this soon, so any help would be appreciated.
    www.RealisticGraphics.net

    Running VS.Net Enterprise & VB 6

    Other Languages: JavaScript, VBScript, VBA, HTML, CSS, ASP, SQL, XML

    MSN Messenger: kmsheff

  2. #2
    Guest
    Add the following to a Form with a Timer.
    Code:
    Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Type POINTAPI
        x As Long
        y As Long
    End Type
    Dim LastCtl As Long
    
    
    Private Sub Form_Load()
        Timer1.Interval = 1
    End Sub
    
    Private Sub Timer1_Timer()
    
        Dim Wnd As Long
        Dim PT As POINTAPI
        
        GetCursorPos PT
        Wnd = WindowFromPoint(PT.x, PT.y)
        If Wnd = LastCtl Then Exit Sub
        LastCtl = Wnd
            
        '<--Mouse moved to a different window-->
        
        Print "Mose moved to different window"
        
    End Sub

  3. #3

    Thread Starter
    Fanatic Member RealisticGraphics's Avatar
    Join Date
    Jul 1999
    Location
    Arkansas
    Posts
    655
    Megatron:
    Thanks for the code. But maybe you can help some more.

    What I'd like to do with this code is that when the cursor is over a label the label becomes bold faced, but when it leaves it needs to go back. In the past I placed code in the form_mousemove event to accomplish this but it's not that affective if the controls and close together.
    www.RealisticGraphics.net

    Running VS.Net Enterprise & VB 6

    Other Languages: JavaScript, VBScript, VBA, HTML, CSS, ASP, SQL, XML

    MSN Messenger: kmsheff

  4. #4
    Frenzied Member mlewis's Avatar
    Join Date
    Sep 2000
    Posts
    1,226
    The Microsoft SoftButtons control is a great code example.. check http://msdn.microsoft.com/ and you can find it there somewhere...

    Also, with VB comes a demo activex control w/ source that does something like what you want. Check the samples.

    If not, use the SetCapture API. I can't remember exactly what you do; sorry.
    M. Lewis
    Pi-Q Software
    How many mouse clicks does it take to cook breakfast?

    Blargh! I am dead!

  5. #5
    Guest
    Add the following code to a Form with a Timer.

    This uses a custom function called MouseOverLabel to check if the mouse is over the label specified in the 1st argument.
    Code:
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
    Private Type POINTAPI
        x As Long
        y As Long
    End Type
    
    Function MouseOverLabel(lbl As Label) As Boolean
        Dim PT As POINTAPI
        
        GetCursorPos PT
        ScreenToClient hwnd, PT
    
        If (PT.x > lbl.Left And PT.x < lbl.Left + lbl.Width) And (PT.y > lbl.Top And PT.y < lbl.Top + lbl.Height) Then
            MouseOverLabel = True
        End If
    End Function
    
    Private Sub Form_Load()
        Timer1.Interval = 1
        Me.ScaleMode = vbPixels
    End Sub
    
    Private Sub Timer1_Timer()
        'Check if the mouse is over the following labels
        If MouseOverLabel(Label1) = True Then Label1.FontBold = True Else Label1.FontBold = False
        If MouseOverLabel(Label2) = True Then Label2.FontBold = True Else Label2.FontBold = False
    End Sub

  6. #6
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    MouseLeave Event

    All you will ever need to implement is available at this URL. This uses the API's built in TrackMouseEvent for Win98/NT4 and emulates this for win95. Incorporate the MouseLeave event in your user control and you will have a winner.

    Providing a MouseLeave event for a control. A helper for creating controls which track when the mouse is over them.

    http://vbaccelerator.com/codelib/ssubtmr/msleave.htm
    Cheers
    Paul Lewis

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