I have a lot of information inquiries and found that the scaling example of the dialog box controls easy to find, but not the font scaling existing code, after my second zoom control on the basis of I developed a font scaling, is completely tested problem, only downside is that in the calculation of the time scale is used for integer division, after repeatedly scaling cumulative error there, want to have friends to further improve!
the first method:
1. Calculate the size of oninitdlg out to maximize the size of the current dialog, pay attention to use a float value, otherwise large error.
CRect rectum;
::GetWindowRect(m_hWnd,rect);
ScreenToClient(rect);
m_nDlgWidth = rect.right - rect.left;
m_nDlgHeight = rect.bottom - rect.top;
// Calculate the resolution
m_nWidth = GetSystemMetrics(SM_CXSCREEN);
m_nHeight = GetSystemMetrics(SM_CYSCREEN);
// Calculation magnification
m_Multiple_width = float(m_nWidth)/float(m_nDlgWidth);
m_Mutiple_heith = float(m_nHeight)/float(m_nDlgHeight);
change_flag = TRUE; // bool member variable that is shaped for onsize is determined whether execution has been performed oninitdlg
2. give you a dialog box to add onsize message:
void EnviromentConfigDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
if (change_flag) // If it is determined oninitdlg has been called completed.
{
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);
// restore magnification, and stores (to ensure that the reduction time can be restored to its original size)
m_Multiple_width = float(1)/ m_Multiple_width
m_Mutiple_heith = float(1)/m_Mutiple_heith
}
}
3. Refresh Control: calculating control ratio according to the size of the zoom, and the new rectangular movewindow
void EnviromentConfigDlg::ReSize(int nID)
{
CRect rectum;
GetDlgItem(nID)->GetWindowRect(Rect);
ScreenToClient(Rect);
// calculated control the upper left corner
CPoint OldTLPoint,TLPoint;
OldTLPoint = Rect.TopLeft();
TLPoint.x = long(OldTLPoint.x *m_Multiple_width);
TLPoint.y = long(OldTLPoint.y * m_Mutiple_heith );
// Calculate the lower right corner of the control point
CPoint OldBRPoint,BRPoint;
OldBRPoint = Rect.BottomRight();
BRPoint.x = long(OldBRPoint.x *m_Multiple_width);
BRPoint.y = long(OldBRPoint.y * m_Mutiple_heith );
// move the controls to a new rectangle
Rect.SetRect(TLPoint,BRPoint);
GetDlgItem(nID)->MoveWindow(Rect,TRUE);
}
The second method:
1, are defined in the class header file dialog box:
LOGFONT m_LogFont;
CFont *m_pFont;
2, performed in OnInitDialog:
this->GetFont()->GetLogFont(&m_LogFont);
m_pFont = new CFont;
3, the following code is added OnSize message function:
Control of amplification:
static BOOL IsFirst = TRUE; // Since the program initialization function should call OnSize, this time in the dialog box controls, fonts and other resources have not been created yet, if this time these resources will appear in WINXP operating system program crashes, so setting this variable to control
float fsp[2];
POINT Newp;
// Get the current size of the dialog
CRect straight;
GetClientRect (and correct);
// get the size of the client area
Newp.x=recta.right-recta.left;
Newp.y = recta.bottom-recta.top;
fsp[0]=(float)Newp.x/Old.x;
fsp[1]=(float)Newp.y/Old.y;
if (IsFirst && (nType == SIZE_MAXIMIZED))
{
IsFirst = FALSE;
m_XMaxRatio = fsp[0];
m_YMaxRatio = fsp[1];
m_XRestoreRatio = float (1) / FSP [0];
m_YRestoreRatio = float(1)/fsp[1];
}
else if(FALSE == IsFirst)
{
if (SIZE_MAXIMIZED == nType)
{
fsp[0] = m_XMaxRatio;
fsp[1] = m_YMaxRatio;
}
else if(SIZE_RESTORED == nType)
{
FSP [0] = m_XRestoreRatio;
fsp[1] = m_YRestoreRatio;
}
}
CRect rectum;
int woc;
CPoint OldTLPoint, TLPoint; // upper left corner
CPoint OldBRPoint, BRPoint; // lower right corner
HWND hwndChild=::GetWindow(m_hWnd,GW_CHILD);
// List all the controls
while(hwndChild)
{
woc=::GetDlgCtrlID(hwndChild);//取得ID
GetDlgItem(woc)->GetWindowRect(Rect);
ScreenToClient(Rect);
OldTLPoint = Rect.TopLeft();
TLPoint.x = long(OldTLPoint.x*fsp[0]);
TLPoint.y = long(OldTLPoint.y*fsp[1]);
OldBRPoint = Rect.BottomRight();
BRPoint.x = long(OldBRPoint.x *fsp[0]);
BRPoint.y = long(OldBRPoint.y *fsp[1]);
Rect.SetRect(TLPoint,BRPoint);
GetDlgItem(woc)->MoveWindow(Rect,TRUE);
hwndChild=::GetWindow(hwndChild, GW_HWNDNEXT);
}
Old=Newp;
Font scaling:
if (!IsFirst)
{
m_pFont->DeleteObject();
LOGFONT log;
log = m_LogFont;
log.lfWidth=long(log.lfWidth*fsp[0]);
log.lfHeight=long(log.lfHeight*fsp[1]);
if (FALSE == m_pFont->CreateFontIndirect(&log))
{
CString errInfo;
errInfo.Format(_T("%d"),GetLastError());
MessageBox(errInfo);
return;
}
m_LogFont.lfHeight = log.lfHeight;
m_LogFont.lfWeight = log.lfWidth;
}
hwndChild=::GetWindow(m_hWnd,GW_CHILD);
// List all the controls
while(hwndChild)
{
CString str;
woc=::GetDlgCtrlID(hwndChild);//取得ID
GetDlgItem(woc)->GetWindowText(str);
if(!IsFirst)
GetDlgItem (woc) -> SetFont (m_pFont); // set the control font
GetDlgItem(woc)->SetWindowText(str);
hwndChild=::GetWindow(hwndChild, GW_HWNDNEXT);
}
RedrawWindow (); // erase an image overlap generated during amplification