MFC 对话框 拖动改变大小,控制位置(大小)跟随变化

修改对话框模板属性 styles-->>Border 属性改为Resizing
 
<p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(70, 70, 70);">   </span><span style="color: rgb(70, 70, 70);">还是以上面最简单的情况为例子:</span></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(70, 70, 70);"> 1  </span><span style="color: rgb(70, 70, 70);">首先要知道对话框大小是否改变,改变了多少,我们应该记录当前对话框的大小。</span></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(70, 70, 70);">    </span><span style="color: rgb(70, 70, 70);">在对话框类中添加成员变量</span><span style="color: rgb(70, 70, 70);"> CRect m_rect  </span><span style="color: rgb(70, 70, 70);">他是用来记录当前对话框的大小。我们在</span><span style="color: rgb(70, 70, 70);">OnInitDialog()</span><span style="color: rgb(70, 70, 70);">函数中获取对话框创建时的大小:</span><span style="color: rgb(70, 70, 70);">GetClientRect(&m_rect);</span></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(70, 70, 70);"> 2 </span><span style="color: rgb(70, 70, 70);">增加消息</span><span style="color: rgb(70, 70, 70);">WM_SIZE</span><span style="color: rgb(70, 70, 70);">的消息响应函数</span><span style="color: rgb(70, 70, 70);"> OnSize():</span><span style="color: rgb(70, 70, 70);">只要对话框大小发生变化,就会调用该函数。</span></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(70, 70, 70);"> 3 </span><span style="color: rgb(70, 70, 70);">在</span><span style="color: rgb(70, 70, 70);">OnSize()</span><span style="color: rgb(70, 70, 70);">函数中增加如下代码:(最简单的为例子,就一个确定按钮和取消按钮)</span></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(70, 70, 70);">来源:</span><span style="color: rgb(70, 70, 70);">(<a target=_blank href="http://blog.sina.com.cn/s/blog_4b5039210100dzkd.html" style="color: rgb(51, 102, 153); text-decoration: none;">http://blog.sina.com.cn/s/blog_4b5039210100dzkd.html</a></span>)- MFC:<span style="color: rgb(70, 70, 70);">如何让对话框中的控件和对话框一起变小变大</span><span style="color: rgb(70, 70, 70);">_</span><span style="color: rgb(70, 70, 70);">萧萧</span><span style="color: rgb(70, 70, 70);">_</span><span style="color: rgb(70, 70, 70);">新浪博客</span></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(70, 70, 70);"> </span></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(70, 70, 70);">  voidCTestDlg::OnSize(UINT nType, int cx, int cy)
{
 CDialog::OnSize(nType, cx, cy);
 
 // TODO: Add your message handler code here
  for (int i=1;i<=2;i++)     </span><span style="color: red;">//</span><span style="color: red;">因为是两个控件,所以这里用了循环</span><span style="color: rgb(70, 70, 70);">
  {</span></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(70, 70, 70);">  CWnd*pWnd; 
  pWnd = GetDlgItem(i);     </span><span style="color: red;">//</span><span style="color: red;">获取</span><span style="color: red;">ID</span><span style="color: red;">为</span><span style="color: red;">i</span><span style="color: red;">的空间的句柄,因为</span><span style="color: red;">“</span><span style="color: red;">确认</span><span style="color: red;">”ID</span><span style="color: red;">为</span><span style="color: red;">1</span><span style="color: red;">,</span><span style="color: red;">“</span><span style="color: red;">取消</span><span style="color: red;">”ID</span><span style="color: red;">为</span><span style="color: red;">2
</span><span style="color: rgb(70, 70, 70);"> if(pWnd)  </span><span style="color: red;">//</span><span style="color: green;">判断是否为空,因为对话框创建时会调用此函数,而当时控件还未创建</span><span style="color: green;">
</span><span style="color: rgb(70, 70, 70);">
  {
   CRect rect;   </span><span style="color: red;">//</span><span style="color: red;">获取控件变化前的大小</span><span style="color: rgb(70, 70, 70);">  </span></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(70, 70, 70);">  pWnd->GetWindowRect(&rect);
   ScreenToClient(&rect</span><span style="color: green;">//</span><span style="color: green;">将控件大小转换为在对话框中的区域坐标</span><span style="color: green;">
  //cx/m_rect.Width()</span><span style="color: green;">为对话框在横向的变化比例</span><span style="color: green;">

</span><span style="color: rgb(70, 70, 70);">  rect.left=rect.left*cx/m_rect.Width();//</span><span style="color: rgb(70, 70, 70);">调整控件大小</span><span style="color: rgb(70, 70, 70);">
   rect.right=rect.right*cx/m_rect.Width();
   rect.top=rect.top*cy/m_rect.Height();
   rect.bottom=rect.bottom*cy/m_rect.Height();
   pWnd->MoveWindow(rect);//</span><span style="color: rgb(70, 70, 70);">设置控件大小</span><span style="color: rgb(70, 70, 70);">
  }</span></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(70, 70, 70);">  }
  GetClientRect(&m_rect);// </span><span style="color: green;">将变化后的对话框大小设为旧大小</span></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(70, 70, 70);"> 
 }</span></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(70, 70, 70);"> </span></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(70, 70, 70);">  </span><span style="color: rgb(70, 70, 70);">然后运行</span><span style="color: rgb(70, 70, 70);">,当对话框最大化的时候,两个按钮也变大了。</span></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(70, 70, 70);"> <a target=_blank href="http://photo.blog.sina.com.cn/showpic.html#blogid=4b5039210100dzkd&url=http://static2.photo.sina.com.cn/orignal/4b50392146da0fc6b13a1&690" target="_blank" style="color: rgb(51, 102, 153); text-decoration: none;"></a></span></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(70, 70, 70);">               </span><span style="color: rgb(70, 70, 70);">(因为全屏图太大,我只截取了右上角的部分)</span></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(70, 70, 70);"> 
</span><span style="color: red;">PS</span><span style="color: red;">:其实这种方法实现起来简单,但是是存在问题的,如果界面有很多控件,那个当你拖拽窗口的角的时候,经过几次变大变小,你会发现其实里面的控件已经失真了,就不是原来的长宽比例了。那么要实现不管窗口怎么变,里面的控件不但大小跟着变,而且大小比例也跟着变,那就不是这个</span><span style="color: red;">OnSize</span><span style="color: red;">函数那么简单了,实现起来有点复杂。等我掌握了这个复杂的方法,一定会拿来分享的。</span></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: red;"> </span></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong><span style="color: black;">二</span></strong></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong><a target=_blank href="http://www.cppblog.com/wsy6634/archive/2009/11/04/100099.html" style="color: rgb(51, 102, 153); text-decoration: none;"><span style="color: rgb(34, 51, 85);">mfc </span><span style="color: rgb(34, 51, 85);">控件大小随窗体改变而改变</span></a></strong></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">  如果对话框或视类的大小调后,控件的大小和位置没有变化,界面看起来会很不爽.</p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">  控件是从CWnd派生的,但不能使用SetWindowPos()或OnSize()或OnSizing()来改变其大小,应在父窗口的WM_SIZE消息中使用MoveWindow()来进行调整。</p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">  VC++之根据对话框大小调整控件大小</p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">  1、在对话框类中加入成员变量CRect m_rect;用于保存对话框大小变化前的大小;</p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">  2、在对话框的OnInitDialog()函数中获取对话框创建时的大小:</p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">  GetClientRect(&m_rect);</p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">  3、在WM_SIZE的响应函数OnSize()中加入以下代码:</p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">  CWnd *pWnd;</p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">  pWnd = GetDlgItem(IDC_LIST);   //获取控件句柄</p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">  if(pWnd)//判断是否为空,因为对话框创建时会调用此函数,而当时控件还未创建</p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">  {
  CRect rect;  //获取控件变化前大小
  pWnd->GetWindowRect(&rect);
  ScreenToClient(&rect);//将控件大小转换为在对话框中的区域坐标
  // cx/m_rect.Width()为对话框在横向的变化比例
  rect.left=rect.left*cx/m_rect.Width();/调整控件大小
  rect.right=rect.right*cx/m_rect.Width();
  rect.top=rect.top*cy/m_rect.Height();
  rect.bottom=rect.bottom*cy/m_rect.Height();
  pWnd->MoveWindow(rect);//设置控件大小
  }
  GetClientRect(&m_rect);//将变化后的对话框大小设为旧大小</p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(70, 70, 70);"> </span></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong><span style="color: rgb(70, 70, 70);">三</span></strong></p><p align="left" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(70, 70, 70);"> </span>如果是里面只有一两个控件倒好办,但控件太多的话这样做是不是重复劳动太多了?
有没有更好的办法啊?</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">视类中加成员函数和成员变量: 
        void   SetControlInfo(WORD   CtrlId); 
protected: 
        int   m_old_cx,m_old_cy; 
        CDWordArray   m_control_info; 
  
构造函数中初始化: 
        m_old_cx   =   m_old_cy   =  0; 
  
加控件信息的函数定义: 
void   CTestFormView::SetControlInfo(WORD   CtrlId) 
{ 
        m_control_info.Add(CtrlId); 
  
} 
  


修改OnSize函数: 
void   CTestFormView::OnSize(UINT   nType,   int   cx,  int   cy) 
{ 
        if(cx==0   ||   cy==0) 
        { 
                cx=800; 
                cy=600; 
        } 
        CFormView::OnSize(nType,   cx,   cy); 
        float   dx_percent   =   (m_old_cx  ==0)?   1   :   (float)((float)cx/(float)m_old_cx); 
        float   dy_percent   =   (m_old_cy  ==0)?   1   :     (float)((float)cy/(float)m_old_cy);
  
        if(m_old_cx) 
        { 
                CRect   WndRect; 
                CWnd   *pWnd; 
                for(int   i  =   0;   i   <   m_control_info.GetSize();   i++) 
                { 
                       pWnd   =   GetDlgItem(m_control_info[i]); 
                       if(!pWnd) 
                       { 
                               TRACE( "Control   ID   -  %d   NOT   FOUND!!\n ",m_control_info[i]); 


                               continue; 
                       } 
  
                       pWnd-> GetWindowRect(&WndRect); 
                       ScreenToClient(&WndRect); 
  
                       WndRect.left   =   (int)(WndRect.left*dx_percent); 
                       WndRect.right   =   (int)(WndRect.right*   dx_percent); 
                       WndRect.bottom   =   (int)(WndRect.bottom*dy_percent); 
                       WndRect.top   =   (int)(WndRect.top*dy_percent); 
  
                       pWnd-> MoveWindow(&WndRect); 
                } 
  
        } 
        m_old_cx   =   cx; 
        m_old_cy   =   cy; 
} 
  
在OnInitialUpdate函数中加入控件ID: 
        SetControlInfo(IDC_BUTTON1); 
            。。。。。。。。。。。。。。。。 


  运行,搞定! 
Dialog也一样!</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> </p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">
</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">上面是转载的内容,补充一些,就是,当在运行上面的程序后,当dlg为仅剩下标题栏时,再放大,就不会显示内部的控件,那么自己设置了窗体的最小值:</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">void CDataBaseConfigDlg::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
{
<span style="white-space: pre;"></span>// TODO: 在此添加消息处理程序代码和/或调用默认值
<span style="white-space: pre;"></span>if(lpMMI->ptMinTrackSize.x <= 800)
<span style="white-space: pre;"></span>lpMMI->ptMinTrackSize.x = 800;
<span style="white-space: pre;"></span>if(lpMMI->ptMinTrackSize.y <= 500)
<span style="white-space: pre;"></span>lpMMI->ptMinTrackSize.y = 500;
<span style="white-space: pre;"></span>CDialogEx::OnGetMinMaxInfo(lpMMI);
}</p>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值