工控编程吧
标题:
上位机VC MFC通过派生实现无法点击的逃跑按钮
[打印本页]
作者:
qq263946146
时间:
2017-11-14 17:48
标题:
上位机VC MFC通过派生实现无法点击的逃跑按钮
(, 下载次数: 7)
上传
点击文件名下载附件
(, 下载次数: 0)
上传
点击文件名下载附件
[weixinlianxi]1[/weixinlianxi]
[iqiyi][iqiyi]http://player.video.qiyi.com/5996515e56001ae021359fe23179e9aa/0/0/w_19ruy7irfh.swf-albumId=9915157809-tvId=9915157809-isPurchase=0-cnId=12[/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);
}
复制代码
欢迎光临 工控编程吧 (https://www.gkbc8.com/)
Powered by Discuz! X3.4