When the size of the dialog box changes, the size of the controls on the dialog box will look ugly if they do not change. Here's how to make the controls on the dialog box automatically adjust as the size of the dialog box changes.
The first clear is that Windows has a WM_SIZE message response function, which is called when the size of the dialog box changes, so we can record the size of the original dialog box, as well as the size of the current dialog box (this does not have to calculate, the system has given), and then adjust the corresponding proportional to the control.
For example, the original dialog box size is a, now B, the original control size is C, then the control size should be c*b/a; (note A cannot be 0).
See Example: ( boldface word for added code !!) )
Assuming that the dialog class is a CMyDlg class, first set a variable
CRect M_rect; to save the original dialog box size
Then you get the size of the dialog box when the dialog box is created.
BOOL Cmydlg::oninitdialog ()
{
.........................
SetIcon (M_hicon, TRUE); Set Big icon
SetIcon (M_hicon, FALSE); Set Small Icon
Todo:add Extra initialization here
GetClientRect (&m_rect); get the size of the dialog box
return TRUE; Return TRUE unless you set the focus to a control
}
Then manually add a function to achieve the specific change
void Cmydlg::changesize (UINT nID, int x, int y)/NID for the control id,x,y the current length and width of the dialog box, respectively
{
CWnd *pwnd;
Pwnd=getdlgitem (NID);
if (pwnd!=null)// is blank, because the OnSize function is called when the window is created, but the controls are not created yet, pWnd is empty
{
CRect Rec;
Pwnd->getwindowrect (&REC); get the size of the control before it changes
ScreenToClient (&REC); Place the control size in the area coordinates of the dialog box
Rec.left=rec.left*x/m_rect. Width (); adjust the new location of the space proportionally
Rec.top=rec.top*y/m_rect. Height ();
Rec.bottom=rec.bottom*y/m_rect. Height ();
Rec.right=rec.right*x/m_rect. Width ();
Pwnd->movewindow (REC); Scaling Controls
}
}
Finally add the Windows message response function OnSize:
void Cmydlg::onsize (UINT nType, int cx, int cy)//Cx,cy is the length and width of the current dialog box
{
Cdialog::onsize (NType, CX, CY);
Todo:add your message Handler code here
if (ntype!=size_minimized)// judgment window is not minimized, because after the window is minimized, the length and width of the window will become 0, the current change will appear divided by 0 error operation
{
Changesize (Idc_list1,cx,cy); make adjustments to each control in turn
Changesize (Idc_list2,cx,cy);
Changesize (Idc_button1,cx,cy);
Changesize (Idc_button2,cx,cy);
GetClientRect (&m_rect); Finally, to update the dialog box size, as the next change in the old coordinates
}
}
Mission Accomplished!
How MFC makes the size of a control automatically adjust with the size of the dialog box