As a beginner of MFC, inevitably encounter such a problem, the window can be changed size, and the size of the control is also changed, then how to do it.
1. The properties of the corresponding window maximize box and minimize box are set to true, allowing the window to be maximized and minimized, and the border property set to resizing so that the window size can be changed.
2. In the message there is a wm_size, add function onsize, so that you can adjust the size of the control within the function, in particular, many code to take advantage of the current control ratio and window size ratio and then multiply the current window size to change the size of the control, but because of the accuracy of the problem, the adjustment of many times will be problematic, You can then use a struct to save the ratio of each control to the original window.
Create the structure first:
typedef struct Rect
{
Public:
int Id;
Double scale[4];
Rect ()
{
Id=-2;
scale[0]=0;
scale[1]=0;
scale[2]=0;
scale[3]=0;
}
Rect (const rect& c)
{
*this=c;
}
}control;
Where ID is used to hold the ID of the control, but initialized here-2 means there is no control inside, so you should be aware that there is no macro definition ID-2 in your control, this is an imperfect point.
Then add the variable in the member variable of the class
CRect m_rect;//get the initial window size
Control m_control[10];//up to 10 controls, where code portability is less powerful, but the author is less capable, directly represented by numbers, and not defined by macros
Then you can add a method to the OnSize.
Controls the size of the control, making it change with the size of the window
void Cchangedbtooldlg::onsize (UINT nType, int cx, int cy)
{
Cdialog::onsize (NType, CX, CY);
Gets the size of the control and its original size for easy scaling
CWnd *pwnd;
int WOC;
List all controls
HWND Hwndchild=::getwindow (M_hwnd,gw_child);
Determines whether the minimum state is not redrawn
if (!cx| |! Cy
{
Return
}
Else
{
while (Hwndchild)
{
Woc=::getdlgctrlid (hwndchild);//Get the ID of the control
pwnd= GetDlgItem (WOC); //GetIDto WoCthe handle of the space
int i=0;
if (pWnd)
{
CRect rect; Gets the size of the current window
Pwnd->getwindowrect (&rect);
ScreenToClient (&rect);//Convert the control size to the area coordinates in the dialog box
for (i=0;i<10;i++)
{
if (M_control[i]. ID==WOC)//If the control is saved with the window ratio, multiply the current window size directly
{
Rect.left = m_control[i].scale[0] * CX;
Rect.right = m_control[i].scale[1] * CX;
Rect.top = m_control[i].scale[2] * CY;
Rect.bottom = m_control[i].scale[3] * CY;
Break
}
}
if (i==10)
{
for (i=0;i<10;i++)
{
if (M_control[i]. ID==-2)//Do not find the ratio of the control, then select a space without data to save
{
M_control[i]. ID=WOC;
M_control[i].scale[0] = (double) rect.left/m_rect. Width ();//Pay attention to type conversion, otherwise save to long type is directly 0.
M_CONTROL[I].SCALE[1] = (double) rect.right/m_rect. Width ();
M_CONTROL[I].SCALE[2] = (double) rect.top/m_rect. Height ();
M_CONTROL[I].SCALE[3] = (double) rect.bottom/m_rect. Height ();
resizing controls
Rect.left = m_control[i].scale[0] * CX;
Rect.right = m_control[i].scale[1] * CX;
Rect.top = m_control[i].scale[2] * CY;
Rect.bottom = m_control[i].scale[3] * CY;
Break
}
}
}
Pwnd->movewindow (rect);//Set Control size
}
Hwndchild=::getwindow (Hwndchild, Gw_hwndnext);
}
GetClientRect (&m_rect);//Set the changed dialog box size to the old size
}
TODO: Add Message Handler code here
}
In fact, there are many such code on the Internet, but many do not save the ratio, resulting in a number of changes in its position will change, but the code is still a disadvantage of some font control and so can not change the size, I still tune something, learned the back hair.
MFC controls Adaptive window size, varies with window size