Proper MouseLeave Detection

I have read many tips about using MouseMove events to create an Explorer-like coolbar look. The problem is that if your button is located close to the main form’s border and you move the mouse too fast, the pointer jumps to the desktop or to another window without firing a Form_MouseMove event, and your button still has the “active” look.

The Command1_MouseMove event allows you to intercept the moment when the mouse pointer moves in or passes over the button, and you can easily assign an “active” picture to the button within this event. But you cannot intercept the moment when the pointer leaves the button using Form_MouseMove or any other Control_MouseMove or LostFocus event. To do this, you need to use two Windows APIs: SetCapture and ReleaseCapture. SetCapture directs all mouse events to the button, so you can trap the mouse at any point on the screen. ReleaseCapture releases mouse events, restoring standard behavior. The only thing you have to do with these APIs is check the pointer’s coordinates to determine when it leaves the button:

 Declare Function ReleaseCapture Lib "user32" () _	As LongDeclare Function SetCapture Lib "user32" (ByVal _	hWnd As Long) As LongPrivate Sub SSCommand1_MouseMove(Button As _	Integer, Shift As Integer, X As Single, Y As _	Single)	Dim ret As Long	Static flagInside As Boolean	If X < 0 Or X > SSCommand1.Width Or _		Y < 0 Or Y > SSCommand1.Height Then		' pointer is out		flagInside = False		ret = ReleaseCapture()		SSCommand1.Picture = _			ImageList1.ListImages("gray").Picture		SSCommand1.BevelWidth = 0	Else		' pointer is in		If flagInside = False Then			flagInside = True			ret = SetCapture(SSCommand1.hWnd)			SSCommand1.Picture = _				ImageList1.ListImages( _				"color").Picture			SSCommand1.BevelWidth = 1		End If	End IfEnd Sub

As you can see, you don’t need to use any other events except SSCommand1_MouseMove.

Use the flagInside variable to prevent the icon from blinking while the pointer passes over the button. While you drag the mouse over the button, the MouseMove event fires repeatedly. You don’t need to assign the “active” picture every time if it has already been assigned.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS