Causes:
I originally wanted to use MFC to make a button that cannot be pressed (mouse moves over the button, the button will move away), use WM_MOUSEMOVE message to find
WM_MOUSEMOVE message is not sent to the parent window when the mouse is over the button
Solution:
Use the SetCapture () function to capture mouse messages
setCapture captures the following mouse events: onmousedown, onmouseup, onclick, ondblclick, onmouseover, and onmouseout
There is exactly the message we need to capture, but this function is a bit overbearing, and no other events can be done during the capture, so it needs to be released in time
Release using the ReleaseCapture () function
note:
The SetCapture () function seems to require the program to have administrator rights
void CBiaoBai1Dlg :: OnMouseMove (UINT nFlags, CPoint point) { // TODO: add the message handler code and / or call the default value CDialogEx :: OnMouseMove (nFlags, point); // Set the mouse capture SetCapture (); / / Get the position of the disagree button (relative to the upper left corner of the display) CRect rect_disagree; m_Disagree_Button.GetWindowRect (& rect_disagree); // Get the position of the consent button CRect rect_agree; m_agree.GetWindowRect (& rect_agree); // Get the position of the considered button CRect rect_think; m_think.GetWindowRect (& rect_think); // Get the width and height of the disagree button int height = rect_disagree.Height (); int width = rect_disagree.Width (); // Convert client coordinates to screen coordinates ClientToScreen (& point); / / Determine whether the mouse is on the consent and consideration buttons // If it is on them, release the mouse capture if (rect_agree.PtInRect (point) || rect_think.PtInRect (point)) { // Release mouse capture ReleaseCapture (); } rect_disagree.NormalizeRect (); BOOL ret = PtInRect (rect_disagree, point); if (ret) { / / Get client size CRect rect; GetClientRect (& rect); // Determine the range of random movement in the client // If it falls on the consent and consideration buttons, it will be random again int re_x; int re_y; CPoint point_youxia; CPoint point_youshang; CPoint point_zuoxia; do { re_x = rand ()% (rect.right-width); re_y = rand ()% (rect.bottom-height); point.SetPoint (re_x, re_y); // Convert client coordinates to the screen coordinate ClientToScreen (& point); point_youxia.SetPoint(point.x + width, point.y + height); point_youshang.SetPoint(point.x + width, point.y); point_zuoxia.SetPoint(point.x, point.y + height); } while (rect_agree.PtInRect(point) || rect_think.PtInRect(point) || rect_agree.PtInRect(point_youxia) || rect_think.PtInRect(point_youxia) || rect_agree.PtInRect(point_youshang) || rect_think.PtInRect(point_youshang) || rect_agree.PtInRect(point_zuoxia) || rect_think.PtInRect(point_zuoxia)); rect.SetRect(re_x, re_y, re_x + width, re_y + height); m_Disagree_Button.MoveWindow(&rect, TRUE); } }