核心思想:
1,设置控件的起始位置和大小即可;
2,起始坐标计算法法是:计算 x坐标相对于 父窗口宽度的位置的百分比,计算 y坐标相对于 父窗口高度的位置的百分比
3,起始大小计算法法是:计算 长度 相对于 父窗口宽度的位置的百分比,计算 宽度相对于 父窗口高度的位置的百分比
计算百分比函数
void CMFCApplication2Dlg::addArray(CWnd *m_pwnd)
{
CRect m_oldrect, m_rect;
CContolChange *m_contrl = new CContolChange();
m_contrl->m_wndColtol = m_pwnd;
m_pwnd->GetWindowRect(&m_oldrect);
//m_pwnd->GetClientRect(&m_rect);
//m_pwnd->ClientToScreen(&m_rect);
//m_pwnd->SetWindowPos(NULL, m_oldrect.left-8, m_oldrect.top-31, m_oldrect.Width(), m_oldrect.Height(), SWP_NOZORDER);
m_pwnd->GetWindowRect(&m_oldrect);
this->GetClientRect(&m_rect);
m_contrl->m_xPersent = (float)m_oldrect.left / m_rect.Width();
m_contrl->m_yPersent = (float)m_oldrect.top / m_rect.Height();
m_contrl->m_widthPersen = (float)m_oldrect.Width() / m_rect.Width();
m_contrl->m_heightPersent = (float)m_oldrect.Height() / m_rect.Height();
m_controlManager.Add(m_contrl);
}
OnSize()布局
void CMFCApplication2Dlg::OnSize(UINT nType, int cx, int cy)
{
CDialogEx::OnSize(nType, cx, cy);
int x, y;
int width, height;
int m_cnt = m_controlManager.GetCount();
CContolChange *m_control;
for (int i = 0; i < m_cnt; i++)
{
m_control = m_controlManager.GetAt(i);
if (::IsWindow(m_testBtn.GetSafeHwnd()))
{
x = m_control->m_xPersent*cx;
y = m_control->m_yPersent*cy;
width = cx*m_control->m_widthPersen;
height = cy*m_control->m_heightPersent;
m_control->m_wndColtol->SetWindowPos(NULL, x-8, y-31, width, height, SWP_NOZORDER);
}
}
// TODO: 在此处添加消息处理程序代码
}