对应场景:在窗口打开之前放大比例已经确定。
直接在OnInitDialog中实现,直接贴代码。
参数说明:
m_fRatio :放大比例,最小1最大3
注意事项:
因为对话框内的字体大小不会自己变需要我们自己实现,而不同字体放大的比例又不通,故这里需要根据自己情况修改lgFont.lfHeight = -rcChildOld.Height()*m_fRatio*0.58;
中的系数0.58。
BOOL CMyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
CenterWindow( AfxGetMainWnd() );
if(m_fRatio > 1 && m_fRatio <=3)
{
CFont* pOldFont;
pOldFont = GetFont();
LOGFONT lgFont;
memset(&lgFont,0,sizeof LOGFONT);
CRect rectOld;
CRect rectNew;
CRect rcTmp;
GetWindowRect(&rectOld);//屏幕坐标系
rcTmp = rectOld;
rectOld.InflateRect(rectOld.Width()*(m_fRatio-1)/2,rectOld.Height()*(m_fRatio-1)/2);
rectNew = rectOld;
rectOld = rcTmp;
CRect rcClient;
GetClientRect(rcClient);
CRect rcChildOld;
CRect rcChildNew;
CWnd* pWnd = GetWindow(GW_CHILD);//获取子窗体
std::vector<CRect> vecRect;
vecRect.clear();
while(pWnd) //1.计算位置
{
pWnd->GetWindowRect(rcChildOld);//子窗体的区域
/*if (pWnd->GetDlgCtrlID() == IDC_COMBO_USERS)
{
CComboBox* pCB = (CComboBox*)pWnd;
pCB->GetDroppedControlRect(&rcChildOld);
}*///这里需注意下拉列表框 要获取的是下拉后需要的大小
vecRect.push_back(rcChildOld);
pWnd = pWnd->GetNextWindow();//取下一个子窗体
}
GetFont()->GetLogFont(&lgFont);
lgFont.lfHeight = -rcChildOld.Height()*m_fRatio*0.58;
if (m_newFont.m_hObject != NULL)
{
m_newFont.DeleteObject();//删除旧的
}
m_newFont.CreateFontIndirect(&lgFont);
MoveWindow(rectNew);//2.父窗口先移动 切记
pWnd = GetWindow(GW_CHILD);
std::vector<CRect>::iterator iter = vecRect.begin();
while(pWnd)
{
CPoint ptLeftTop;
ptLeftTop.x = (iter->left - rectOld.left)*m_fRatio ;//相对偏移量
ptLeftTop.y = (iter->top - (rectOld.Height() - rcClient.Height()) - rectOld.top )*m_fRatio ;//相对偏移量
pWnd->SetFont(&m_newFont);
pWnd->MoveWindow(ptLeftTop.x,ptLeftTop.y,iter->Width()*m_fRatio,iter->Height()*m_fRatio,TRUE);
pWnd = pWnd->GetNextWindow();//取下一个子窗体
iter++;
}
}
return TRUE;
}