上位机VC MFC通过派生实现无法点击的逃跑按钮
如果您认可,可联系功能定制! 如果您着急,充值会员可直接联系发您资料!
[iqiyi][/iqiyi]
例程通过从CButton类派生出自己的类 class CTraceBtn : public CButton
在类中编写OnMouseMove函数实现无法被鼠标点击到的按钮。
点击界面上的捕获开关,可以让按钮点击到与否。
实现过程:
新建对话框工程
从CButton派生类class CTraceBtn : public CButton。
添加成员变量与函数,与鼠标移动函数
- public:
- void EnableCatch(bool Benalbe = true){m_bCatch = Benalbe;}
- int m_nJumpDistance;
- bool m_bCatch;
- void CTraceBtn::OnMouseMove(UINT nFlags, CPoint point)
- {
- if(m_bCatch)
- return;
- CWnd* pParent = GetParent();
- if (!pParent) pParent = GetDesktopWindow();
- CRect ParentRect; // Parent client area (Parent coords)
- pParent->GetClientRect(ParentRect);
- ClientToScreen(&point); // Convert point to parent coords
- pParent->ScreenToClient(&point);
- CRect ButtonRect; // Button Dimensions (Parent coords)
- GetWindowRect(ButtonRect);
- pParent->ScreenToClient(ButtonRect);
- CPoint Center = ButtonRect.CenterPoint(); // Center of button (parent coords)
- CSize CriticalSize(ButtonRect.Width()/6, ButtonRect.Height()/6);
- CRect NewButtonRect = ButtonRect; // New position (parent coords)
- if (point.x - CriticalSize.cx > Center.x) // Mouse is right of center
- {
- if (ButtonRect.left > ParentRect.left + ButtonRect.Width() + m_nJumpDistance)
- NewButtonRect -= CSize(ButtonRect.right - point.x + m_nJumpDistance, 0);
- else
- NewButtonRect += CSize(point.x - ButtonRect.left + m_nJumpDistance, 0);
- }
- else if (point.x + CriticalSize.cx < Center.x) // Mouse is left of center
- {
- if (ButtonRect.right < ParentRect.right - ButtonRect.Width() - m_nJumpDistance)
- NewButtonRect += CSize(point.x - ButtonRect.left + m_nJumpDistance, 0);
- else
- NewButtonRect -= CSize(ButtonRect.right - point.x + m_nJumpDistance, 0);
- }
- if (point.y - CriticalSize.cy > Center.y) // Mouse is below center
- {
- if (ButtonRect.top > ParentRect.top + ButtonRect.Height() + m_nJumpDistance)
- NewButtonRect -= CSize(0, ButtonRect.bottom - point.y + m_nJumpDistance);
- else
- NewButtonRect += CSize(0, point.y - ButtonRect.top + m_nJumpDistance);
- }
- else if (point.y + CriticalSize.cy < Center.y) // Mouse is above center
- {
- if (ButtonRect.bottom < ParentRect.bottom - ButtonRect.Height() - m_nJumpDistance)
- NewButtonRect += CSize(0, point.y - ButtonRect.top + m_nJumpDistance);
- else
- NewButtonRect -= CSize(0, ButtonRect.bottom - point.y + m_nJumpDistance);
- }
- MoveWindow(NewButtonRect);
- RedrawWindow();
-
- CButton::OnMouseMove(nFlags, point);
- }
复制代码
在界面上添加按钮控件,并使其关联变量CTraceBtn m_TraceBtn;
再添加单选框,双击关联点击函数
- void CGkbc8Dlg::OnCheck1()
- {
- static bool bCatch = false;
- bCatch = !bCatch;
- m_TraceBtn.EnableCatch(bCatch);
-
- ((CButton*)GetDlgItem(IDC_CHECK1))->SetCheck(bCatch);
- }
复制代码
|