包含tab控制元件時,,如何讓子控制元件跟隨對話方塊大小變化(下)
阿新 • • 發佈:2019-02-07
前面是tab控制元件的編寫,真正resize的編寫則在此處。
其中m_point是記錄上一次的視窗座標的變數
由於有tab控制元件,所以單獨對其也進行了處理,將其檢視也進行重新調整大小,其整體思路,是將標籤頁的座標拿來使用,刪減其高度,就可以令內部檢視無縫顯示,否則要麼覆蓋了標籤頁,要麼會有留白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(); } }
但是,單單這樣還不足夠,還得在初始化時進行設定,否則一執行程式,還會有部分留白在,在OnInitDlg中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);
之後開啟程式,各個控制元件就可以進行大小變化了,但是,如果還想要tab中view的子控制元件也同樣的進行變化,那麼就得在tab中進行重寫了// 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