前面是tab控件的编写,真正resize的编写则在此处。
其中m_point是记录上一次的窗口坐标的变量
void CMTSWDlg::Resize()
{
float fsp[2];
CRect rect;
Point NewPoint;
GetWindowRect(&rect);
NewPoint.X = rect.right - rect.left;
NewPoint.Y = rect.bottom - rect.top;
fsp[0] = (float)NewPoint.X/m_point.X;
fsp[1] = (float)NewPoint.Y/m_point.Y;
int nID;
CPoint OldTLPoint,TLPoint; //左上角
CPoint OldBRPoint,BRPoint; //右下角
CRect rectChild;
HWND hwndChild = ::GetWindow(m_hWnd,GW_CHILD);
while (hwndChild)
{
nID = ::GetDlgCtrlID(hwndChild);
GetDlgItem(nID)->GetWindowRect(rectChild);
ScreenToClient(rectChild);
OldTLPoint = rectChild.TopLeft();
TLPoint.x = long(OldTLPoint.x*fsp[0]);
TLPoint.y = long(OldTLPoint.y*fsp[1]);
OldBRPoint = rectChild.BottomRight();
BRPoint.x = long(OldBRPoint.x*fsp[0]);
BRPoint.y = long(OldBRPoint.y*fsp[1]);
rectChild.SetRect(TLPoint,BRPoint);
GetDlgItem(nID)->MoveWindow(rectChild,TRUE);
if (nID == IDC_TAB)//when it's tabctrl,we need to resize the views inside the tabctrl
{
ViewResize(&rectChild);
}
hwndChild=::GetWindow(hwndChild, GW_HWNDNEXT);
}
m_point = NewPoint;
}
void CMTSWDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: 在此处添加消息处理程序代码
if(m_TabCtrl)
{
Resize();
}
}
由于有tab控件,所以单独对其也进行了处理,将其视图也进行重新调整大小,其整体思路,是将标签页的坐标拿来使用,删减其高度,就可以令内部视图无缝显示,否则要么覆盖了标签页,要么会有留白
void CMTSWDlg::ViewResize(LPRECT lpRect)
{
CRect rect ;
m_TabCtrl.GetItemRect(0,&rect);
int nHeight = rect.Height();//标签页的高度
int nY = rect.bottom;
int nX = rect.left;
((CWnd*)m_TabCtrl.GetCurPage())->GetWindowRect(&rect);
ScreenToClient(&rect);
CPoint OldTLPoint; //左上角
CPoint OldBRPoint; //右下角
#if 0
OldTLPoint.x = lpRect->left;
OldTLPoint.y = lpRect->top - nHeight;//整个tabctrl控件-标签页高度
OldBRPoint.x = lpRect->right;
OldBRPoint.y = lpRect->bottom;
rect.SetRect(OldTLPoint,OldBRPoint);
rect.OffsetRect(CSize(-lpRect->left,-lpRect->top/2));
#endif
OldTLPoint.x = nX;
OldTLPoint.y = nY;//lpRect->top - nHeight;//整个tabctrl控件-标签页高度
OldBRPoint.x = lpRect->right;
OldBRPoint.y = lpRect->bottom;
rect.SetRect(OldTLPoint,OldBRPoint);
((CWnd*)m_TabCtrl.GetCurPage())->MoveWindow(&rect,TRUE);
但是,单单这样还不足够,还得在初始化时进行设置,否则一运行程序,还会有部分留白在,在OnInitDlg中
// TODO: 在此添加额外的初始化代码
CRect rectPage,rectDlg;
m_pAnalogDlg = new CAnalogSetPage;
m_pAnalogDlg->Create(IDD_DLG_ANALOGSET,&m_TabCtrl);
m_pCommonDlg = new CCommonSetDlg;
m_pCommonDlg->Create(IDD_DLG_COMMONSET,&m_TabCtrl);
m_pDigitDlg = new CDigitSetDlg;
m_pDigitDlg->Create(IDD_DLG_DIGITSET,&m_TabCtrl);
//保存整个对话框大小
::GetClientRect(m_hWnd,&rectDlg);
ScreenToClient(&rectDlg);
m_point.X = rectDlg.right - rectDlg.left;
m_point.Y = rectDlg.bottom - rectDlg.top;
m_TabCtrl.GetWindowRect(&rectPage);
ScreenToClient(&rectPage);
m_pointPage.X = rectPage.left - rectPage.right;
m_pointPage.Y = rectPage.bottom - rectPage.top;
CRect rectItem;
m_TabCtrl.GetItemRect(0,rectItem);
rectPage.left = rectItem.left;
rectPage.top = rectItem.bottom;
m_pAnalogDlg->MoveWindow(&rectPage);
m_pCommonDlg->MoveWindow(&rectPage);
m_pDigitDlg->MoveWindow(&rectPage);
m_TabCtrl.AddPage(m_pCommonDlg,(LPTSTR)(LPCTSTR)strTitle[0]);
m_TabCtrl.AddPage(m_pDigitDlg,(LPTSTR)(LPCTSTR)strTitle[1]);
m_TabCtrl.AddPage(m_pAnalogDlg,(LPTSTR)(LPCTSTR)strTitle[2]);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
之后打开程序,各个控件就可以进行大小变化了,但是,如果还想要tab中view的子控件也同样的进行变化,那么就得在tab中进行重写了