MFC 对话框最大化时控件也随比例最大化或者还原

1. 在oninitdlg中 计算出当前对话框的大小与最大化后大小,注意要用float值,不然误差很大.

CRect rect;
::GetWindowRect(m_hWnd,rect);
ScreenToClient(rect);
m_nDlgWidth = rect.right - rect.left;
m_nDlgHeight = rect.bottom - rect.top;

//计算分辨率
m_nWidth = GetSystemMetrics(SM_CXSCREEN);
m_nHeight = GetSystemMetrics(SM_CYSCREEN);

//计算放大倍数
m_Multiple_width = float(m_nWidth)/float(m_nDlgWidth);
m_Mutiple_heith = float(m_nHeight)/float(m_nDlgHeight);
change_flag=TRUE;//这个是成员变量bool形,用来判断onsize执行时oninitdlg是否已经执行了

2. 给你对话框添加 onsize消息:

void EnviromentConfigDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
if (change_flag)//如果确定oninitdlg已经调用完毕.
{
   ReSize(IDC_STATIC1);
   ReSize(IDC_TREE_ALARM);
   ReSize(IDC_STATIC2);
   ReSize(IDC_TREE_CAMERA);
   ReSize(IDC_STATIC3);
   ReSize(IDC_LIST_TYPE);
   ReSize(IDOK);
   ReSize(IDC_STATIC4);
   ReSize(IDC_LIST_INFO);

//恢复放大倍数,并保存 (确保还原时候能够还原到原来的大小)

m_Multiple_width = float(1)/ m_Multiple_width ;
   m_Mutiple_heith = float(1)/m_Mutiple_heith ;
}
}

3.刷新控件:根据比例计算控件缩放的大小,然后movewindow 到新矩形上

void EnviromentConfigDlg::ReSize(int nID)
{
CRect Rect;
GetDlgItem(nID)->GetWindowRect(Rect);
ScreenToClient(Rect);
//计算控件左上角点
CPoint OldTLPoint,TLPoint;
OldTLPoint = Rect.TopLeft();
TLPoint.x = long(OldTLPoint.x *m_Multiple_width);
TLPoint.y = long(OldTLPoint.y * m_Mutiple_heith );

//计算控件右下角点
CPoint OldBRPoint,BRPoint;
OldBRPoint = Rect.BottomRight();
BRPoint.x = long(OldBRPoint.x *m_Multiple_width);
BRPoint.y = long(OldBRPoint.y * m_Mutiple_heith );
//移动控件到新矩形

Rect.SetRect(TLPoint,BRPoint);
GetDlgItem(nID)->MoveWindow(Rect,TRUE);
}

这样,你就可以随心所遇的放大与缩小你的对话框了,控件也跟着比例缩小放大

 

本文章来自于【http://blog.csdn.net/dongzhongshu/article/details/5688416】(未验证)

你可能感兴趣的:(mfc,控件,调节大小)