00 在对话框上拖动按钮并移动该按钮

Post date: 2012/4/24 上午 06:46:17

  1. // 派生自CButton类,主要过滤WM_LBUTTONDOWN 、WM_LBUTTONUP和WM_MOUSEMOVE消息。
  2. BOOL m_bFlag = FALSE; // 成员变量,用来标示鼠标是否按下, 初始化为FALSE
  3. CPoint m_pt; // 成员变量,用来保存当前坐标值
  4. BOOL CXXButton::PreTranslateMessage(MSG* pMsg)
  5. {
  6. // TODO: Add your specialized code here and/or call the base class
  7. switch(pMsg->message)
  8. {
  9. case WM_LBUTTONDOWN:
  10. {
  11. m_pt = pMsg->pt;
  12. m_bFlag = TRUE;
  13. }
  14. break;
  15. case WM_LBUTTONUP:
  16. {
  17. m_bFlag = FALSE;
  18. }
  19. break;
  20. case WM_MOUSEMOVE:
  21. {
  22. if(m_bFlag)
  23. {
  24. int cx = pMsg->pt.x - m_pt.x;
  25. int cy = pMsg->pt.y - m_pt.y;
  26. CRect rc;
  27. GetWindowRect(&rc);
  28. GetParent()->ScreenToClient(&rc);
  29. int nWidth = rc.Width();
  30. int nHeight = rc.Height();
  31. rc.left += cx;
  32. rc.top += cy;
  33. rc.right = rc.left + nWidth;
  34. rc.bottom = rc.top + nHeight;
  35. MoveWindow(rc);
  36. m_pt = pMsg->pt;
  37. }
  38. }
  39. break;
  40. default:
  41. break;
  42. }
  43. return CButton::PreTranslateMessage(pMsg);
  44. }