VC++控件自适应屏幕的方法

1.首先在初始化函数中,FormView在OnInitialUpdate(),Dialog在OnInitDialog()中初始化控件的大小。

view plaincopy to clipboardprint?
01.//开始初始化控件大小  
02. m_IsInitialed = false;  
03. 
04. CRect m_ClientRect;  
05. this->GetClientRect(&m_ClientRect);  
06. CSize m_Forsize;  
07. m_Forsize = GetTotalSize();//在资源编辑器中定好大小后,程序运行时大小(不管最大化和最小化,该大小均为同一个值),客户区大于或等于显示的大小  
08. double m_x = (double)m_ClientRect.Width() / m_Forsize.cx;//宽度方向发大倍数  
09. double m_y = (double)m_ClientRect.Height() / m_Forsize.cy;//高度方向发大倍数  
10. 
11. //调整控件的大小  
12. CWnd *pWnd = NULL;   
13. pWnd = GetWindow(GW_CHILD);  
14. while(pWnd)//判断是否为空,因为对话框创建时会调用此函数,而当时控件还未创建  
15. {  
16.  CRect rect;   //获取控件变化前大小  
17.  pWnd->GetWindowRect(&rect);  
18.  ScreenToClient(&rect);//将控件大小转换为在对话框中的区域坐标  
19.  m_ControlRect.insert(pair<int, CRect>(pWnd->GetDlgCtrlID(), rect));//保存控件的初始大小,以便在OnSize函数中继续使用  
20.  int width = rect.Width();  
21.  int height = rect.Height();  
22. 
23.  WCHAR szBuf[256];  
24.  GetClassName(pWnd->m_hWnd,szBuf,256);           
25.  if( _tcsicmp(szBuf,_T("Edit")) == 0)     
26.  {   
27.   //Edit只是位置变化,大小没有变  
28.   rect.top = m_y * rect.top;  
29.   rect.left = m_x * rect.left;  
30.   rect.bottom = rect.top + height;  
31.   rect.right = rect.left + width;  
32.  }  
33.  else 
34.  {  
35.   //其它控件位置和大小均变化  
36.   rect.top = m_y * rect.top;  
37.   rect.left = m_x * rect.left;  
38.   rect.bottom = m_y * rect.bottom;  
39.   rect.right = m_x * rect.right;  
40.  }  
41. 
42.  pWnd->MoveWindow(&rect);//设置控件大小  
43.  pWnd = pWnd->GetWindow(GW_HWNDNEXT);  
44. }  
45.   
46. //控件初始化结束  
47. m_IsInitialed = true; 
//开始初始化控件大小
 m_IsInitialed = false;

 CRect m_ClientRect;
 this->GetClientRect(&m_ClientRect);
 CSize m_Forsize;
 m_Forsize = GetTotalSize();//在资源编辑器中定好大小后,程序运行时大小(不管最大化和最小化,该大小均为同一个值),客户区大于或等于显示的大小
 double m_x = (double)m_ClientRect.Width() / m_Forsize.cx;//宽度方向发大倍数
 double m_y = (double)m_ClientRect.Height() / m_Forsize.cy;//高度方向发大倍数

 //调整控件的大小
 CWnd *pWnd = NULL;
 pWnd = GetWindow(GW_CHILD);
 while(pWnd)//判断是否为空,因为对话框创建时会调用此函数,而当时控件还未创建
 {
  CRect rect;   //获取控件变化前大小
  pWnd->GetWindowRect(&rect);
  ScreenToClient(&rect);//将控件大小转换为在对话框中的区域坐标
  m_ControlRect.insert(pair<int, CRect>(pWnd->GetDlgCtrlID(), rect));//保存控件的初始大小,以便在OnSize函数中继续使用
  int width = rect.Width();
  int height = rect.Height();

  WCHAR szBuf[256];
  GetClassName(pWnd->m_hWnd,szBuf,256);        
  if( _tcsicmp(szBuf,_T("Edit")) == 0)  
  {
   //Edit只是位置变化,大小没有变
   rect.top = m_y * rect.top;
   rect.left = m_x * rect.left;
   rect.bottom = rect.top + height;
   rect.right = rect.left + width;
  }
  else
  {
   //其它控件位置和大小均变化
   rect.top = m_y * rect.top;
   rect.left = m_x * rect.left;
   rect.bottom = m_y * rect.bottom;
   rect.right = m_x * rect.right;
  }

  pWnd->MoveWindow(&rect);//设置控件大小
  pWnd = pWnd->GetWindow(GW_HWNDNEXT);
 }
 
 //控件初始化结束
 m_IsInitialed = true;
 

2.如果界面在运行时大小可以改变,则在OnSize函数中加入如下代码

view plaincopy to clipboardprint?
01.// TODO: 在此处添加消息处理程序代码  
02.    CFormView::ShowScrollBar(SB_BOTH, false);//设置没有滚动条,视情况而定。  
03.         //在界面不是最小化并且已经初始化完毕  
04.    if (!IsIconic() && m_IsInitialed)  
05.    {  
06.        CSize m_Forsize;  
07.        m_Forsize = GetTotalSize();  
08.        double m_x = (double)cx / m_Forsize.cx;  
09.        double m_y = (double)cy / m_Forsize.cy;  
10. 
11.                //读取控件的初始大小  
12.        map<int, CRect>::iterator pos = m_ControlRect.begin();  
13.        for (; pos != m_ControlRect.end(); ++pos)  
14.        {  
15.            CRect rect = pos->second;  
16.            int width = rect.Width();  
17.            int height = rect.Height();  
18. 
19.            WCHAR szBuf[256];  
20.            GetClassName(GetDlgItem(pos->first)->m_hWnd,szBuf,256);                     
21.            if( _tcsicmp(szBuf,_T("Edit")) == 0)     
22.            {   
23.                rect.top = m_y * rect.top;  
24.                rect.left = m_x * rect.left;  
25.                rect.bottom = rect.top + height;  
26.                rect.right = rect.left + width;  
27.            }  
28.            else 
29.            {  
30.                rect.top = m_y * rect.top;  
31.                rect.left = m_x * rect.left;  
32.                rect.bottom = m_y * rect.bottom;  
33.                rect.right = m_x * rect.right;  
34.            }  
35.            GetDlgItem(pos->first)->MoveWindow(rect);  
36.        }  
37.    } 
// TODO: 在此处添加消息处理程序代码
 CFormView::ShowScrollBar(SB_BOTH, false);//设置没有滚动条,视情况而定。
         //在界面不是最小化并且已经初始化完毕
 if (!IsIconic() && m_IsInitialed)
 {
  CSize m_Forsize;
  m_Forsize = GetTotalSize();
  double m_x = (double)cx / m_Forsize.cx;
  double m_y = (double)cy / m_Forsize.cy;

                //读取控件的初始大小
  map<int, CRect>::iterator pos = m_ControlRect.begin();
  for (; pos != m_ControlRect.end(); ++pos)
  {
   CRect rect = pos->second;
   int width = rect.Width();
   int height = rect.Height();

   WCHAR szBuf[256];
   GetClassName(GetDlgItem(pos->first)->m_hWnd,szBuf,256);         
   if( _tcsicmp(szBuf,_T("Edit")) == 0)  
   {
    rect.top = m_y * rect.top;
    rect.left = m_x * rect.left;
    rect.bottom = rect.top + height;
    rect.right = rect.left + width;
   }
   else
   {
    rect.top = m_y * rect.top;
    rect.left = m_x * rect.left;
    rect.bottom = m_y * rect.bottom;
    rect.right = m_x * rect.right;
   }
   GetDlgItem(pos->first)->MoveWindow(rect);
  }
 }

或在OnShowWindow()函数中加入也可以(特别是在对话框作为tabpage时)

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ybw20041910/archive/2010/06/19/5679730.aspx

posted on 2010-11-29 11:06 wrh 阅读(1596) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2010年11月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

统计

常用链接

留言簿(19)

随笔档案

文章档案

收藏夹

搜索

最新评论

阅读排行榜

评论排行榜