Practical experience: hover and leave on the control of use

problem

In recent work, we encountered such a demand: The system can make up different responses depending on whether the mouse control. Whether here on the control, in fact, has a special name: Hover and Leave. The so-called Hover, refers to hover in the rectangular area specified period of time the window is located. The so-called Leave, refers to a rectangular area where the mouse leaves the window.

To use news

In Windows, each using WM_MOUSEHOVER and WM_MOUSELEAVE to represent the two events. Let's take a look at the MSDN description of the two messages:

WM_MOUSEHOVER:

Posted to a window when the cursor hovers over the client area of the window for the period of time specified in a prior call to TrackMouseEvent.

WM_MOUSELEAVE:

Posted to a window when the cursor leaves the client area of the window specified in a prior call to TrackMouseEvent.

Described herein, reference to an important function TrackMouseEvent. We look at this function.

TrackMouseEvent:

Posts messages when the mouse pointer leaves a window or hovers over a window for a specified amount of time.

The main effect of this function is that when the mouse cursor hovers over a specified period of time a window or leaving a window, send or WM_MOUSELEAVE WM_MOUSEHOVER message out.

Therefore, in order to trigger WM_MOUSEHOVER / WM_MOUSELEAVE messages, you must call TrackMouseEvent at the right point in time. What time is the appropriate point in time?

Move the mouse at any time according to the user's manipulation, so the best time to call TrackMouseEvent of: WM_MOUSEMOVE event handler.

Treatment of WM_MOUSEMOVE

Let's add a call to TrackMouseEvent in WM_MOUSEMOVE event handler, here we create a custom class CMyButton, which inherits from CMFCButton:

16868175-d89a3eb73337cb0a

Code explanation:

1) m_bTrackingMouse used to indicate whether mouse tracking, behind this variable will be used to.

Event message 2) dwFlags specify the interest here is set to TME_LEAVE | TME_HOVER, express application and would like to receive WM_MOUSEHOVER WM_MOUSELEAVE messages.

3) hwndTrack: Specifies the hover or leave the target window, the action will only be made in this window will only be detected by the system.

4) dwHoverTime: specified period of time, unit: ms. When the mouse hovers over a specified window, undergoing a period of time, the system will trigger WM_MOUSEHOVER messages.

5) Note that this needs to be added ON_WM_MOUSEMOVE () to create a message map.

6) used here to _TrackMouseEvent, rather than TrackMouseEvent following reasons:

The _TrackMouseEvent function calls TrackMouseEvent if it exists, otherwise _TrackMouseEvent emulates TrackMouseEvent.

Receiving a leave message or hover

Next, we were on WM_MOUSEHOVER and WM_MOUSELEAVE message is processed:

16868175-08a2dc03a6f7822d

Code explanation:

1) were used ON_WM_MOUSEHOVER and ON_WM_MOUSELEAVE two macros establish the message map.

2) When the message is received hover possible to know where the mouse is positioned within the rectangular window, and the mouse tracking is no longer required. This time m_bTrackingMouse set to false, it will disable call follow-up TrackMouseEvent prevent unnecessary mouse tracking system.

3) When the mouse leaves the message is received, the mouse has left rectangle represents a case where the window m_bTrackingMouse set to true, to re-enable mouse tracking.

Precautions

1) Some controls, such as CStatic control, the default will not trigger WM_MOUSEHOVER and WM_MOUSELEAVE message Notify needs to be set to true.

to sum up

Through the actual code that demonstrates the generation and flow trigger mouseover and leave a message. Through this practice, we can draw from this added function code in controls to achieve effects such as changing the appearance of controls, such as when the mouse hovers.

Guess you like

Origin blog.csdn.net/weixin_34162228/article/details/90850656