The following two methods are provided to track whether the mouse is in the window.
1. Use setcapture () and onmousemove () to determine the mouse position
// Set a status to record and identify whether the mouse is in the window
// Bool m_bovercontrol;
Void CXXX: onmousemove (uint nflags, cpoint point)
{
Cstatic: onmousemove (nflags, point );
// If the mouse is still in the window
If (m_bovercontrol)
{
Crect rect;
Getclientrect (rect); // move the cursor away from the customer area, uncapture the mouse, and rewrite the status m_bovercontrol.
If (! Rect. ptinrect (point ))
{
M_bovercontrol = false;
Releasecapture ();
// Other operations such as redrawwindow () when the mouse leaves ();
Return;
}
} // If the mouse enters the window, place the mouse to capture, status parameter
Else
{
M_bovercontrol = true; // other operations when the mouse enters eg: redrawwindow ();
Setcapture ();
}
}
2. Send the wm_mouseleave message when the mouse leaves the window using trackmouseevent ()
Trackmouseevent () can also be used to determine the mouse stop and send the wm_mousehover message.
// Set a status to record and identify whether the mouse is in the window
Bool m_bmousetracking = false;
// Process the mouse-moving message
Void csomewnd: onmousemove (uint nflags, cpoint point)
{
// When wm_mousemove is received and m_bmousetracking is false, the mouse enters the window.
// SET _ trackmouseevent
If (! M_bmousetracking)
{
Trackmouseevent tme;
Tme. cbsize = sizeof (trackmouseevent );
Tme. dwflags = tme_leave; // monitor mouse exit
Tme. hwndtrack = This-> m_hwnd;
If (: _ trackmouseevent (& TME ))
{
M_bmousetracking = true;
// Other operations when the mouse enters
}
}
Cview: onmousemove (nflags, point );
}
Process wm_mouseleave messages
Message Processing Function declaration afx_msg lresult onmouseleave (wparam, lparam );
Message ing on_message (wm_mouseleave, onmouseleave)
Processing Function Definition
Lresult csomewnd: onmouseleave (wparam, lparam)
{
// Other operations when the mouse leaves
// Number of Reset statuses
M_bmousetracking = false;
Return true;
}
========================================================== ========================================================== ==================================
Process the message when the mouse leaves the window (wm_mouseleave)
Wm_mouseleave is a message sent when the mouse leaves the window, but this message is different from the common mouse message. To receive the wm_mouseleave message, you must first call trackmouseevent, and each call to the trackmouseevent window can only receive wm_mouseleave once, that is to say, if you want to obtain the wm_mouseleave message, you must call trackmouseevent once when the mouse enters the window again.
Static bool btrackleave = false;
Void cmywnd: onmousemove (uint nflags, cpoint point)
{
If (! Btrackleave)
{
// When the mouse moves into the window for the first time, a message wm_mouseleave is requested.
Trackmouseevent tme;
Tme. cbsize = sizeof (TME );
Tme. hwndtrack = m_hwnd;
Tme. dwflags = tme_leave;
_ Trackmouseevent (& TME );
Btrackleave = true;
// Add the code for processing the mouse entry here:
....................................
}
}
Manually add message ing and message processing functions
Begin_message_map (cmywnd, cwnd)
// {Afx_msg_map (cmywnd)
...........................
//} Afx_msg_map
On_message (wm_mouseleave, onmouseleave)
End_message_map ()
Lparam cmywnd: onmouseleave (wparam WP, lparam LP)
{
Btrackleave = false;
// Add the code for processing the mouse exit here:
.........................................
Return 0;
}