MFC-拖动无边框窗体_默认值




hpp      //定义全局变量 BOOL m_bMoving; CPoint m_ptMouse;   cpp  CMFCApplication11Dlg::CMFCApplication11Dlg(CWnd* pParent /*=NULL*/)     : CDialogEx(CMFCApplication11Dlg::IDD, pParent) {     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);      //在此处初始化     m_ptMouse.x = m_ptMouse.y = 0;     m_bMoving = FALSE; }    /*     鼠标左键按下消息 */ void CMFCApplication11Dlg::OnLButtonDown(UINT nFlags, CPoint point) {     // TODO:  在此添加消息处理程序代码和/或调用默认值      m_bMoving = TRUE;     SetCapture();//捕获鼠标     m_ptMouse = point;       CDialogEx::OnLButtonDown(nFlags, point); }   /*     鼠标移动消息 */ void CMFCApplication11Dlg::OnMouseMove(UINT nFlags, CPoint point) {     // TODO:  在此添加消息处理程序代码和/或调用默认值      if (m_bMoving)     {         //计算鼠标移动的距离         int x, y;         x = point.x - m_ptMouse.x;         y = point.y - m_ptMouse.y;          if (abs(x) >= 2 || abs(y) >= 2)//控制移动大于两个像素在移动,避免连续移动的闪烁感         {             //获取窗口原来的位置             RECT rc = { 0 };             GetWindowRect(&rc);              OffsetRect(&rc, x, y);//偏移矩形的位置             MoveWindow(&rc, TRUE);//移动窗口         }      }      CDialogEx::OnMouseMove(nFlags, point); }   /*     鼠标抬起消息 */ void CMFCApplication11Dlg::OnLButtonUp(UINT nFlags, CPoint point) {     // TODO:  在此添加消息处理程序代码和/或调用默认值      if (m_bMoving)     {         m_bMoving = FALSE;         ReleaseCapture();//释放鼠标的捕获     }      CDialogEx::OnLButtonUp(nFlags, point); }   /*     窗口激活消息 */ void CMFCApplication11Dlg::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized) {     CDialogEx::OnActivate(nState, pWndOther, bMinimized);      // TODO:  在此处添加消息处理程序代码      //窗口失去焦点,由激活状态,变为不激活状态     if (nState == WA_INACTIVE && m_bMoving)     {         m_bMoving = FALSE;         ReleaseCapture();//释放鼠标的捕获     } }