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   lydstory  阅读(3257)  评论(0编辑  收藏  举报

编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示