Results 1 to 7 of 7

Thread: Detecting when mouse cursor is over control

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Roodepoort, South Africa
    Posts
    472

    Detecting when mouse cursor is over control

    I'm trying to make a custom control based on a PictureBox control.

    Want to add something like a toolbar that must be hidden until the mouse is moved over the PictureBox when the toolbar must be made visible or slided into view. When the mouse is moved of the PictureBox the toolbar must be made invisible or slided back out of view. (Any ideas how to do this will also be appreciated.)

    How do I detect when the mouse moves onto the PictureBox and off the PictureBox again?

    Found the following on the board:
    Code:
    Option Explicit
    
    Private Declare Function Mouse_Enter Lib "user32" Alias "SetCapture" (ByVal hwnd As Long) As Long
    Private Declare Function Mouse_Leave Lib "user32" Alias "ReleaseCapture" () As Long
    
    Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        
        Static CtrMov As Boolean
        With Command1
            If (X < 0) Or (Y < 0) Or (X > .Width) Or (Y > .Height) Then
                Mouse_Leave
                CtrMov = False
                Debug.Print "Leaving"
            Else
                Mouse_Enter .hwnd
                If CtrMov = False Then
                    CtrMov = True
                    Debug.Print "Entering"
                End If
            End If
        End With
    End Sub
    but it only detects the mouse going onto the control. When the mouse moves off the control it's not detected.

  2. #2
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Detecting when mouse cursor is over control

    This is how I have done it in the past
    Code:
    Private Declare Function ReleaseCapture Lib "user32" () As Long
    Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
    
    Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Static blnOver As Boolean
    
        With Picture1
            If (X < 0) Or (Y < 0) Or (X > .Width) Or (Y > .Height) Then
                ReleaseCapture
                blnOver = False
                Debug.Print "Mouse Out"
            Else
                If Not blnOver Then
                    SetCapture .hwnd
                    blnOver = True
                    Debug.Print "Mouse Over"
                End If
            End If
        End With
    End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Roodepoort, South Africa
    Posts
    472

    Re: Detecting when mouse cursor is over control

    Tried this example but it also didn't work at first. Eventually figured out that my problem was the .Width and .Height properties. The picturebox scalemode is set to pixels. So changed the .Width to .ScaleWidth and the .Height to .ScaleHeight and now it works. Sort of...

    My zoom buttons are, for now, located in the picturebox at the top left. When moving the mouse in and out of the UserControl the 'mouse in' and 'mouse out' are picked up without a problem. But when I click one of my zoom buttons the 'mouse in' and 'mouse out' are not picked up anymore! Nothing. Any ideas?

    Also, Where would the current code that's in the MouseMove event go in the above example? I have code in the MouseMove event that is used to pan the picture around in the picturebox.

  4. #4
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Detecting when mouse cursor is over control

    add a timer and a picturebox (default names)
    Timer1
    Picture1

    then add this to the form code

    Code:
    Option Explicit
    
    Private Const ToolBar_Border_Closed_Width As Single = 15 'Twips = 1 Pixel
    Private Const ToolBar_Border_Opened_Width As Single = 1560 ' Twips = 104 Pixels
    Private Const ToolBar_Speed As Single = 1  '0.1 to 10 ... Best between 1 - 5 (for slower 0.1 to 0.9... for faster 5 - 10)
    Private Const ToolBar_Color As Long = vbBlack
    
    Private OpenToolBar As Boolean
    
    Private Sub Form_Load()
     Picture1.Move Me.Width - 240 - ToolBar_Border_Closed_Width, 0, ToolBar_Border_Closed_Width, Me.Height ' Border width = 240 twips (16 pixels)
     Picture1.BackColor = ToolBar_Color
     Picture1.BorderStyle = 0
     Timer1.Interval = 1
     Timer1.Enabled = False
    End Sub
    
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
     OpenToolBar = False
     Timer1.Enabled = True
    End Sub
    
    Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If X < Picture1.Left Then
      OpenToolBar = True
      Timer1.Enabled = True
     End If
    End Sub
    
    
    Private Sub Timer1_Timer()
    If OpenToolBar Then
    
          If Picture1.Width >= ToolBar_Border_Opened_Width Then
             Timer1.Enabled = False
             OpenToolBar = False
          Else
             Picture1.Width = Picture1.Width + (15 * ToolBar_Speed)
             Picture1.Left = Picture1.Left - (15 * ToolBar_Speed)
          End If
          
    Else
    
          If Picture1.Width <= ToolBar_Border_Closed_Width Then
             Timer1.Enabled = False
             OpenToolBar = True
          Else
             Picture1.Width = Picture1.Width - (15 * ToolBar_Speed)
             Picture1.Left = Picture1.Left + (15 * ToolBar_Speed)
          End If
          
    End If
    End Sub
    try something like this... i did it so the tool bar is on the right

  5. #5
    New Member
    Join Date
    Oct 2013
    Posts
    5

    Re: Detecting when mouse cursor is over control

    Couldn't he just use the Mouse Hover and Mouse Leave handles?

  6. #6
    New Member
    Join Date
    Oct 2013
    Posts
    5

    Re: Detecting when mouse cursor is over control

    Sorry for the double post, new to the forums could not find the edit button!

    You can use this for what you want.

    Code:
    Code:
    Public Class Form1
        Dim hovering As String = "off"
    Code:
        
    Private Sub PictureBox1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseHover
            hovering = "on"
        End Sub
    
        Private Sub PictureBox1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave
            hovering = "off"
        End Sub
    I then used an enabled timer with the interval set to 1 to change a labels text.

    Code:
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If hovering = "on" Then
                Label1.Text = "on"
            Else
                Label1.Text = "off"
            End If
        End Sub

  7. #7
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Detecting when mouse cursor is over control

    Wrong forum. This is VB6 not .NET

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