VC++控件自适应屏幕的方法
2012-05-18 11:02
176 查看
1.首先在初始化函数中,FormView在OnInitialUpdate(),Dialog在OnInitDialog()中初始化控件的大小。
2.如果界面在运行时大小可以改变,则在OnSize函数中加入如下代码
或在OnShowWindow()函数中加入也可以(特别是在对话框作为tabpage时)
/article/11267966.html
//开始初始化控件大小 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函数中加入如下代码
// 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时)
/article/11267966.html
相关文章推荐
- VC++控件自适应屏幕的方法
- VC通用控件自适应屏幕类
- VC通用控件自适应屏幕类
- Android 关于处理手机屏幕自适应时,用到的主要方法
- 获取屏幕高宽,在代码中设置控件大小的方法
- VC控件DateTimePicker使用方法及其相关
- VC MFC 常用控件使用方法-- CTreeCtrl
- 【Android】屏幕自适应4种方法
- VC++ 使用MSComm控件检测串口是否存在的方法
- 如何消除vc屏幕闪烁的方法
- VC窗体透明而控件不透明以及Static文本背景透明方法
- VC中几个控件处理同一消息方法
- vc得到屏幕的当前分辨率方法
- VC控件DateTimePicker使用方法
- VC CListCtrl控件的上移和下移的通用方法
- VC++/MFC常用控件 基本使用方法
- [cocos2d-x]屏幕自适应解决的方法
- VC多线程中控制界面控件的几种方法
- VC窗体透明而控件不透明以及Static文本背景透明方法
- 解决 android 高低版本 webView 里内容 自适应屏幕的终极方法