上位机MFC实现磁性窗口源代码
当前代码实现带有磁性功能的窗口。
效果如上图,
可以点击显示/隐藏按钮,打开隐藏子窗体。
在停靠窗体周边拖动子窗体,可以看到子窗体有磁铁效果,
会吸附在停靠窗体上。
子窗体移动时,显示磁铁效果的关键代码
- // 定义两个域值
- #define THRESHOLD 1
- #define GAP 30
- void CDockDlg::Dock(UINT nDirection,CWnd *pWndMain)
- {
- // 得到主窗体矩形区域
- CRect rectMain;
- pWndMain->GetWindowRect(&rectMain);
- // 进行停靠操作
- m_nDirection = nDirection;
- if (nDirection == LEAVE)
- m_nDirection = NO_DIRECTION;
- // 不再停靠
- else if (!IsDocking(rectMain,m_rectWnd,nDirection))
- Dock(LEAVE,pWndMain);
- int nSep;
- switch (m_nDirection)
- {
- case LEFT:
- // 从左方停靠
- nSep = rectMain.left - m_rectWnd.right;
- if (abs(nSep) > THRESHOLD && abs(nSep) <= GAP)
- {
- m_rectWnd.OffsetRect(nSep,0);
- Dock(LEFT,pWndMain);
- }
- else if (abs(nSep) > GAP)
- Dock(LEAVE,pWndMain);
- break;
- case RIGHT:
- // 从右边停靠
- nSep = rectMain.right - m_rectWnd.left;
- if (abs(nSep) > THRESHOLD && abs(nSep) <= GAP)
- {
- m_rectWnd.OffsetRect(nSep,0);
- Dock(RIGHT,pWndMain);
- }
- else if (abs(nSep) > GAP)
- Dock(LEAVE,pWndMain);
- break;
- case TOP:
- // 从上边停靠
- nSep = rectMain.top - m_rectWnd.bottom;
- if (abs(nSep) > THRESHOLD && abs(nSep) <= GAP)
- {
- m_rectWnd.OffsetRect(0,nSep);
- Dock(TOP,pWndMain);
- }
- else if (abs(nSep) > GAP)
- Dock(LEAVE,pWndMain);
- break;
- case BOTTOM:
- // 从下面停靠
- nSep = rectMain.bottom - m_rectWnd.top;
- if (abs(nSep) > THRESHOLD && abs(nSep) <= GAP)
- {
- m_rectWnd.OffsetRect(0,nSep);
- Dock(BOTTOM,pWndMain);
- }
- else if (abs(nSep) > GAP)
- Dock(LEAVE,pWndMain);
- break;
- case NO_DIRECTION:
- // 子窗体未停靠
- break;
- case LEAVE:
- // 不再是停靠状态
- m_nDirection = NO_DIRECTION;
- }
- }
复制代码 源代码下载地址:
上位机VC MFC程序开发精典实例大全源码与视频讲解配套下载408例 经历1年的编程与录制点击进入查看
如果您认可,可联系功能定制! 如果您着急,充值会员可直接联系发您资料!
|