一、在对话框属性中添加OnSize消息相应
二、在对话框.h文件中添加成员函数、成员变量
/*************响应OnSize消息****************/
public:
afx_msg void OnSize(UINT nType, int cx, int cy);
//根据对话框放大 缩小比例重画控件
void RePaintCtrl(UINT ID, int last_Width, int now_Width, int last_Height, int now_Height);
private:
CRect m_DlgRect;
三、构造函数初始化
m_DlgRect.SetRect(0, 0, 0, 0);//初始化对话框大小存储变量
四、重画控件函数的实现
void CMyDlg::RePaintCtrl(UINT ID, int last_Width, int now_Width, int last_Height, int now_Height)
{
CRect rect;
CWnd* pWnd = NULL;
pWnd = GetDlgItem(ID);
if (NULL == pWnd)
{
return;
}
pWnd->GetWindowRect(&rect);
ScreenToClient(&rect);
//缩放比例
double proportion_x = now_Width / (double)last_Width;
double proportion_y = now_Height / (double)last_Height;
if (ID == IDC_BUTTON_APPLY)
{//大小不变,位置始终在对话框的最右侧
int oriWidth = rect.Width();
rect.left = (long)(rect.left * proportion_x + 0.5);
rect.right = rect.left + oriWidth;
}
else if(ID == IDC_GRID)
{
//rect.left = (long)(rect.left * proportion_x + 0.5); //左侧坐标不变
rect.right = (long)(rect.right * proportion_x + 0.5); //向右拉伸
//rect.top = (long)(rect.top * proportion_y + 0.5); //高度不变
//rect.bottom = (long)(rect.bottom * proportion_y + 0.5); //高度不变
}
else if(ID == IDC_STATIC_LDCASENAME)
{
//rect.left = (long)(rect.left * proportion_x + 0.5); //左侧坐标不变
rect.right = (long)(rect.right * proportion_x + 0.5); //向右拉伸
//rect.top = (long)(rect.top * proportion_y + 0.5); //高度不变
//rect.bottom = (long)(rect.bottom * proportion_y + 0.5); //高度不变
}
pWnd->MoveWindow(&rect);
}
五、Onsize( )函数实现
void CMyDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: 在此处添加消息处理程序代码
if (0 == m_DlgRect.left && 0 == m_DlgRect.right &&
0 == m_DlgRect.top && 0 == m_DlgRect.bottom)
{//第一次启动对话框时的大小变化不做处理
}
else
{
if (0 == cx && 0 == cy)
{//如果是按下了最小化,则触发条件,这时不保存对话框数据
return;
}
CRect rectDlgChangeSize;
GetClientRect(&rectDlgChangeSize);//存储对话框大小改变后对话框大小数据
RePaintCtrl(IDC_BUTTON_APPLY , m_DlgRect.Width(), rectDlgChangeSize.Width(), m_DlgRect.Height(), rectDlgChangeSize.Height());
RePaintCtrl(IDC_GRID , m_DlgRect.Width(), rectDlgChangeSize.Width(), m_DlgRect.Height(), rectDlgChangeSize.Height());
RePaintCtrl(IDC_STATIC_TEXT , m_DlgRect.Width(), rectDlgChangeSize.Width(), m_DlgRect.Height(), rectDlgChangeSize.Height());
}
GetClientRect(&m_DlgRect); //save size of current dialog
Invalidate();//更新窗口
}