MFC中左键移动窗口实现方法

一、

窗体的OnLButtonDown函数中加入如下代码:

  PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,   point.y));

SendMessage(WM_SYSCOMMAND,0xF012,0);  

 

 

二、一般思路

首先在对话框类中添加以下消息处理函数:  
  OnLButtonDown   左键按下  
  OnMouseMove       鼠标移动  
  OnLButtonUp       左键放开  
   
  然后在对话框类中添加两个变量用来表示是否移动窗口及按下鼠标后那个点(相对于窗口的)  
  BOOL   m_bIsMove;                 //是否处于移动状态  
  CPoint   m_CenterPt;           //移动点中心  
   
  再次初始化上面的变量,在对话框类的构造函数中:  
  m_bIsMove   =   FALSE;  
   
  最后处理三个消息函数如下:  
  void   CMoveWindowDemoDlg::OnLButtonDown(UINT   nFlags,   CPoint   point)    
  {  
  //   TODO:   Add   your   message   handler   code   here   and/or   call   default  
  m_bIsMove   =   TRUE;  
  m_CenterPt   =   point;  
   
  CDialog::OnLButtonDown(nFlags,   point);  
  }  
   
  void   CMoveWindowDemoDlg::OnMouseMove(UINT   nFlags,   CPoint   point)    
  {  
  //   TODO:   Add   your   message   handler   code   here   and/or   call   default  
  if   (m_bIsMove)  
  {  
  //获取移动偏移量  
  int   nX   =   point.x   -   m_CenterPt.x;  
  int   nY   =   point.y   -   m_CenterPt.y;  
   
  //正在处于移动状态  
  CRect   rect;  
  GetWindowRect(&rect);  
  rect.OffsetRect(nX,nY);  
  MoveWindow(rect);  
  }  
   
  CDialog::OnMouseMove(nFlags,   point);  
  }  
   
  void   CMoveWindowDemoDlg::OnLButtonUp(UINT   nFlags,   CPoint   point)    
  {  
  //   TODO:   Add   your   message   handler   code   here   and/or   call   default  
  m_bIsMove   =   FALSE;  
   
  CDialog::OnLButtonUp(nFlags,   point);  
  }  

简化:

  //   设对话框类变量   POINT   prePoint;  
   
  //   鼠标移动函数:  
   
  void   CDragMainWindowDlg::OnMouseMove(UINT   nFlags,   CPoint   point)    
  {  
  if(nFlags   ==   MK_LBUTTON)  
  {      
      RECT   rect;  
      GetWindowRect(&rect);  
      POINT   p   =   point;  
      ClientToScreen(&p);  
       
      MoveWindow(rect.left+p.x-prePoint.x,rect.top+p.y-prePoint.y,rect.right-rect.left,rect.bottom-rect.top);  
      prePoint   =   p;  
  }    
  CDialog::OnMouseMove(nFlags,   point);  
  }  
   
  //   配合   鼠标按下函数:  
   
  void   CDragMainWindowDlg::OnLButtonDown(UINT   nFlags,   CPoint   point)    
  {  
  RECT   rect;  
  GetWindowRect(&rect);  
  POINT   p   =   point;  
  ClientToScreen(&p);    
  prePoint   =   p;  
   
  CDialog::OnLButtonDown(nFlags,   point);  
  }  

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
MFC实现左键按下时图片发生移动,可以通过以下步骤实现: 1. 在窗口添加成员变量,用于保存图片的位置和大小信息。 2. 重写窗口类的OnPaint函数,将图片绘制在窗口上。 3. 重写窗口类的OnLButtonDown函数,当用户按下鼠标左键时,记录下鼠标当前位置和图片的起始位置。 4. 重写窗口类的OnMouseMove函数,当用户移动鼠标时,计算出图片应该移动的位置,并调用InvalidateRect函数刷新窗口。 5. 重写窗口类的OnLButtonUp函数,当用户松开鼠标左键时,将保存的位置信息清空。 具体代码实现可以参考以下代码: ``` // 声明成员变量 CRect m_rectImage; // 图片位置和大小信息 CPoint m_ptMouse; // 鼠标位置信息 // 重写OnPaint函数 void CMyWnd::OnPaint() { CPaintDC dc(this); // 绘制图片 CImage image; image.Load(_T("image.bmp")); image.Draw(dc.GetSafeHdc(), m_rectImage); } // 重写OnLButtonDown函数 void CMyWnd::OnLButtonDown(UINT nFlags, CPoint point) { // 记录鼠标位置和图片起始位置 m_ptMouse = point; m_rectImage.OffsetRect(-m_rectImage.left, -m_rectImage.top); } // 重写OnMouseMove函数 void CMyWnd::OnMouseMove(UINT nFlags, CPoint point) { if (nFlags & MK_LBUTTON) // 判断鼠标左键是否按下 { // 计算图片应该移动的位置 int dx = point.x - m_ptMouse.x; int dy = point.y - m_ptMouse.y; m_rectImage.OffsetRect(dx, dy); // 刷新窗口 InvalidateRect(NULL, FALSE); } } // 重写OnLButtonUp函数 void CMyWnd::OnLButtonUp(UINT nFlags, CPoint point) { // 清空保存的位置信息 m_ptMouse = CPoint(0, 0); m_rectImage.SetRectEmpty(); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值