深入浅出 CPropertySheet
译者:徐景周(原作:Mustafa Demirhan)
为了最大限度的发挥属性页的效用,首先让我们先从 CPropertySheet 继承一个新类,取名为 CMyPropSheet.
接着便可以进行下面的各种操作:
一、隐藏属性页默认按钮
隐藏掉Apply应用按钮:
propsheet.m_psh.dwFlags |= PSH_NOAPPLYNOW;或隐藏掉Cancel取消按钮:
CWnd *pWnd = GetDlgItem( IDCANCEL );pWnd->ShowWindow( FALSE );二、移动属性页按钮
首先,要获取按钮的句柄,然后就可以象对待窗体一样处理它们了. 下面代码先隐藏掉Apply和Help铵钮,再把OK和Cancel按移动到右侧。
BOOL CMyPropSheet::OnInitDialog () { BOOL bResult = CPropertySheet::OnInitDialog(); int ids [] = {IDOK, IDCANCEL};//, ID_APPLY_NOW, IDHELP }; // Hide Apply and Help buttons CWnd *pWnd = GetDlgItem (ID_APPLY_NOW); pWnd->ShowWindow (FALSE); pWnd = GetDlgItem (IDHELP); pWnd->ShowWindow (FALSE); CRect rectBtn; int nSpacing = 6; // space between two buttons... for( int i =0; i < sizeof(ids)/sizeof(int); i++) { GetDlgItem (ids [i])->GetWindowRect (rectBtn); ScreenToClient (&rectBtn); int btnWidth = rectBtn.Width(); rectBtn.left = rectBtn.left + (btnWidth + nSpacing)* 2; rectBtn.right = rectBtn.right + (btnWidth + nSpacing)* 2; GetDlgItem (ids [i])->MoveWindow(rectBtn); } return bResult;}
下面代码移动所有按钮到右侧,并且重新置属性页为合适的大小.
BOOL CMyPropSheet::OnInitDialog () { BOOL bResult = CPropertySheet::OnInitDialog(); int ids[] = { IDOK, IDCANCEL, ID_APPLY_NOW }; CRect rectWnd; CRect rectBtn; GetWindowRect (rectWnd); GetDlgItem (IDOK)->GetWindowRect (rectBtn); int btnWidth = rectBtn.Width(); int btnHeight = rectBtn.Height(); int btnOffset = rectWnd.bottom - rectBtn.bottom; int btnLeft = rectWnd.right - rectWnd.left; rectWnd.bottom = rectBtn.top; rectWnd.right = rectWnd.right + btnWidth + btnOffset; MoveWindow(rectWnd); rectBtn.left = btnLeft; rectBtn.right = btnLeft + btnWidth; for (int i = 0; i < sizeof (ids) / sizeof (int); i++) { rectBtn.top = (i + 1) * btnOffset + btnHeight * i; rectBtn.bottom = rectBtn.top + btnHeight; GetDlgItem (ids [i])->MoveWindow (rectBtn); } return bResult;}
三、改变属性页上的标签文字
首先修改TC_ITEM结构,然后用 SetItem 来修改标签文字,如下代码:
TC_ITEM item;item.mask = TCIF_TEXT;item.pszText = "New Label";//Change the label of the first tab (0 is the index of the first tab)...GetTabControl ()->SetItem (0, &item);四、改变属性页标签文字的字体属性
代码如下
m_NewFont.CreateFont (14, 0, 0, 0, 800, TRUE, 0, 0, 1, 0, 0, 0, 0, _T("Arial") ); GetTabControl()->SetFont (&m_NewFont);五、在属性页标签上显示位图
可以用 CImageList 建立图像. 用 SetItem 来设置,如下代码所示:
BOOL CMyPropSheet::OnInitDialog (){ BOOL bResult = CPropertySheet::OnInitDialog(); m_imageList.Create (IDB_MYIMAGES, 13, 1, RGB(255,255,255)); CTabCtrl *pTabCtrl = GetTabControl (); pTabCtrl->SetImageList (&m_imageList); TC_ITEM item; item.mask = TCIF_IMAGE; for (int i = 0; i < NUMBER_OF_TABS; i++) { item.iImage = i; pTabCtrl->SetItem (i, &item );