VC/MFC拖动窗口任意位置移动窗口

除了拖动标题栏移动窗口以外,我们也可以拖动窗口任意位置(除控件)位置而使对话框移动。

这里只讲述基于对话框的程序

[cpp] view plain copy
 
  1. class c**dlg:public CDialog  
  2. {  
  3. //Construction  
  4. public:  
  5.     Crect startRect;     //窗口的初始位置所在的矩形  
  6.     bool isMouseDown;    //鼠标是否按下  
  7.     CPoint startPoint;   //鼠标按下的位置  
  8.     .......  

其次添加OnLButtonDown消息响应函数

[cpp] view plain copy
 
  1. void c**dlg::OnLButtonDown(UINT nFlags, CPoint point)  
  2. {  
  3.     isMouseDown=true;  
  4.     startPoint = point;  
  5.     this->GetWindowRect(startRect);  
  6.     CDialog::OnLButtonDown(nFlags, point);  
  7. }  

添加OnMouseMove消息响应函数

[cpp] view plain copy
 
  1. void c**dlg::ONMouseMove(UINT nFlags, CPoint point)  
  2. {  
  3.     if(isMouseDown == true)  
  4.     {  
  5.         int Dx = point.x - startPoint.x;  
  6.         int Dy = point.y - startPoint.y;  
  7.         startRect.left += Dx;  
  8.         startRect.right += Dx;  
  9.         startRect.top +=Dy;  
  10.         startRect.bottom +=Dy;             //获取新的位置  
  11.         this->MoveWindow(&startRect);     //将窗口移到新的位置  
  12.     }  
  13.     CDialog::OnMouseMove(nFlags, point);  
  14. }  

当释放鼠标时不再拖动窗口,所以要添加OnLButtonUp消息响应函数

[cpp] view plain copy
 
  1. void c**dlg::OnLButtonUp(UINT nFlags, CPoint point)  
  2. {  
  3.     isMouseDown = false;  
  4.     //CDialog::OnLButtonUp(nFlags,Point);  
  5. }  

 

//转:http://blog.csdn.net/luanwujian/article/details/9059861

posted on 2017-12-28 11:40  lydstory  阅读(3208)  评论(0编辑  收藏  举报

导航