QQ登录

只需一步,快速开始

37上位机VC MFC让程序窗口具有磁性效果

[ 复制链接 ]

37上位机VC MFC让程序窗口具有磁性效果

37上位机VC MFC让程序窗口具有磁性效果


37上位机VC MFC让程序窗口具有磁性效果




功能展示

千千静听,很老的音乐播放软件,我们在使用时会发再它的很小窗口可以连在一起,拖动起来彼此间有磁性效果,主程序具有很大的灵活性,那么这种磁性效果是怎么实现的呢?我们当前例程就实现了这功能,效果如图

要点提示
窗口的磁性效果主要用到了两个函数,MapWindowPoints()和MoveWindow();MapWindowPoints函数实现将某个窗口的区域坐标转换为另一窗口的区域坐标,语法如下: void MapWindowPoints( CWnd* pwndTo, LPRECT lpRect ) pwndTo:转换后的区域坐标窗口, lpRect 要转换的区域对象;
实现功能
1.新建基于对话框的应用程序
2.插入一个对话框资源建立与关联一对话框类CChildDlg;在主对话框中添加变量CChildDlg m_ChildDlg; intm_Width;  //窗口宽int m_Height;//窗口高BOOLm_bCreate;//窗口是否已创建BOOL m_bStick; //=两窗口是否粘住CPoint m_Point; //窗口左上角位置;在构造函数中初始化两变量m_bCreate = FALSE;m_bStick  = FALSE;在OnInitDialog()中初始化变量m_ChildDlg.Create(IDD_DIALOG1,this);m_ChildDlg.ShowWindow(SW_SHOW);
CRect Rect; GetWindowRect(Rect);MapWindowPoints(this,Rect); m_Width = Rect.Width();
m_Height = Rect.Height(); m_Point.x = Rect.left;m_Point.y = Rect.top; m_bCreate = TRUE;

窗口的背景可以选择性加载进行美化程序
3.在OnNcLButtonDown()中对m_bStick进行附值
  1. <div style="text-align: center;"><span style="line-height: 1.5;">void CGkbc8Dlg::OnNcLButtonDown(UINT nHitTest, CPoint point) </span></div>{
  2.         // 判断两窗口是否粘合,是则在Onmove函数进一步处理
  3.         CRect Rect,cRect;
  4.         GetWindowRect(Rect);
  5.         MapWindowPoints(this,Rect);
  6.         m_ChildDlg.GetWindowRect(cRect);
  7.         if(Rect.left-cRect.right == 0)
  8.         {
  9.                 m_bStick = TRUE;
  10.         }
  11.         else if(cRect.left-Rect.right == 0)
  12.         {
  13.                 m_bStick = TRUE;
  14.         }
  15.         else if(cRect.top-Rect.bottom == 0)
  16.         {
  17.                 m_bStick = TRUE;
  18.         }
  19.         else if(Rect.top-cRect.bottom == 0)
  20.         {
  21.                 m_bStick = TRUE;
  22.         }
  23.         else
  24.                 m_bStick = FALSE;        
  25.         CDialog::OnNcLButtonDown(nHitTest, point);
  26. }
复制代码
4.在OnMove()实现磁性效果
  1. void CGkbc8Dlg::OnMove(int x, int y)
  2. {
  3.         CDialog::OnMove(x, y);
  4.         
  5.         if(m_bCreate == TRUE)
  6.         {
  7.                 CRect Rect,cRect;
  8.                 GetWindowRect(Rect);
  9.                 MapWindowPoints(this,Rect);
  10.         
  11.                 m_ChildDlg.GetWindowRect(cRect);
  12.                 if(Rect.left-cRect.right<20 && Rect.left-cRect.right>0 && (
  13.                         Rect.top>cRect.top-m_Height && Rect.bottom<cRect.bottom+m_Height))
  14.                         Rect.left = cRect.right;
  15.                 else if(cRect.left-Rect.right<20 && cRect.left-Rect.right>0 && (
  16.                         Rect.top>cRect.top-m_Height && Rect.bottom<cRect.bottom+m_Height))
  17.                         Rect.left = cRect.left - m_Width;
  18.                 else if(cRect.top-Rect.bottom<20 && cRect.top-Rect.bottom>0 && (
  19.                         Rect.left>cRect.left-m_Width && Rect.right<cRect.right+m_Width))
  20.                         Rect.top = cRect.top - m_Height;
  21.                 else if(Rect.top-cRect.bottom<20 && Rect.top-cRect.bottom>0 && (
  22.                         Rect.left>cRect.left-m_Width && Rect.right<cRect.right+m_Width))
  23.                         Rect.top = cRect.bottom;
  24.                 MoveWindow(Rect.left,Rect.top,m_Width,m_Height);
  25.                 if(m_bStick)
  26.                 {
  27.                         m_ChildDlg.MoveWindow(cRect.left+(Rect.left-m_Point.x),
  28.                                 cRect.top+(Rect.top-m_Point.y),cRect.Width(),cRect.Height());
  29.                 }
  30.                 m_Point.x = Rect.left;
  31.                 m_Point.y = Rect.top;
  32.         }
  33. }
复制代码
5.以上仅实现父窗口对子窗口的磁性效果,如果要实现子窗口也具有磁性效果,还得对子对话框类进行简单处理。在子对话框CChildDlg中添加变量int m_Width;

int m_Height;BOOL m_bCreate;
在构造函数中初始化m_bCreate=FALSE;在OnInitDialog()中初始化Crect rect; GetWindowRect(rect);m_Width =rect.Width();m_Height = rect.Height();m_bCreate = TRUE;


6.在OnMove()里实现子窗口的磁性效果
  1. void CChildDlg::OnMove(int x, int y)
  2. {
  3.         CDialog::OnMove(x, y);
  4.         
  5.         // TODO: Add your message handler code here
  6.         if(m_bCreate == TRUE)
  7.         {
  8.                 CRect Rect,cRect;
  9.                 GetWindowRect(cRect);
  10.                 this->GetParent()->GetWindowRect(Rect);
  11.                 if(cRect.left-Rect.right<20 && cRect.left-Rect.right>0 && (
  12.                         cRect.top>Rect.top-m_Height && cRect.bottom<Rect.bottom+m_Height))
  13.                         cRect.left = Rect.right;
  14.                 else if(Rect.left-cRect.right<20 && Rect.left-cRect.right>0 && (
  15.                         cRect.top>Rect.top-m_Height && cRect.bottom<Rect.bottom+m_Height))
  16.                         cRect.left = Rect.left - m_Width;
  17.                 else if(Rect.top-cRect.bottom<20 && Rect.top-cRect.bottom>0 && (
  18.                         cRect.left>Rect.left-m_Width && cRect.right<Rect.right+m_Width))
  19.                         cRect.top = Rect.top - m_Height;
  20.                 else if(cRect.top-Rect.bottom<20 && cRect.top-Rect.bottom>0 && (
  21.                         cRect.left>Rect.left-m_Width && cRect.right<Rect.right+m_Width))
  22.                         cRect.top = Rect.bottom;
  23.                 MoveWindow(cRect.left,cRect.top,m_Width,m_Height);
  24.         }
  25. }
复制代码
下面我们来实现磁性窗口的过程演示
源码及视频下载
(仅在电脑可见)

请点击此处下载

请先注册会员后在进行下载

已注册会员,请先登录后下载

文件名称:37.上位机VC MFC让程序窗口具有磁性效果.rar 
文件大小:43.47 KB  售价:10金币
下载权限: 不限 以上或 VIP会员   [购买捐助会员]   [充值积分]   有问题联系我

  

您的支持是我们创作的动力!  

  

您可花点闲钱积分自助任意充值

  

成为VIP会员 全站资源任意下载永久更新!

回复

使用道具 举报

点击查看
快速回复 返回列表 客服中心 搜索