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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM