Intercepting MouseEnter and MouseExit events without subclassing

Intercepting MouseEnter and MouseExit events without subclassing

Visual Basic raises the MouseMove event when the mouse cursor is within a control, so it’s easy to know when the cursor “enters” a control. However, determining when the mouse leaves the control is more difficult.

You can solve this problem by monitoring the first time the control raises the MouseMove event, which corresponds to the MouseEnter event; in this occasion you capture the mouse input with SetCapture API function, so that your control will receive MouseMove messages even if the cursor is outside the control’s window.

Once you have captured the mouse input you can check the coordinates of the cursor and verify if it’s still in the control area. When mouse leaves the control’s window, it’s time for the MouseExit event, and you must release the mouse capture with ReleaseCapture(). Here’s the code that does the trick for the Command1 control, but that can be easily modified for any control:

Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As LongPrivate Declare Function ReleaseCapture Lib "user32" () As LongPrivate Declare Function GetCapture Lib "user32" () As LongPrivate Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, _    Y As Single)    If (X < 0) Or (Y < 0) Or (X > Command1.Width) Or (Y > Command1.Height) Then        ' the MOUSELEAVE pseudo-event        ReleaseCapture        ' in this example revert the caption to normal        Command1.Font.Bold = False            ElseIf GetCapture() <> Command1.hwnd Then        ' the MOUSEENTER pseudo-event        SetCapture Command1.hwnd        ' in this example, make the caption bold        Command1.Font.Bold = True    End IfEnd Sub

You can apply this tip to all controls that have the hWnd property, such as PictureBox, ListBox, etc, for example to easily implement hot-tracking effects. Here is another example that selects the text of a textbox (Text1) when the cursor is over.

Private Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, _    Y As Single)    If (X Or Y) < 0 Or (X > Text1.Width) Or (Y > Text1.Height) Then        ReleaseCapture        Text1.SelLength = 0    ElseIf GetCapture() <> Text1.hWnd Then        SetCapture Text1.hWnd        Text1.SelStart = 0        Text1.SelLength = Len(Text1)        Text1.SetFocus    End IfEnd Sub

UPDATE: The original example for the Command1 button didn’t work well, and the mouseover functionality was disabled after you click on the button. The new version shown above uses the GetCapture API to solve this problem, and gets rid of the bCaptured Static variable, so it’s also simpler than the original code. Big thanks to Sergio Perciballi from UK for letting us know about the mistake.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing