深入浅出 CPropertySheet

为了最大限度的发挥属性页的效用,首先让我们先从 CPropertySheet 继承一个新类,取名为 CMyPropSheet.

接着便可以进行下面的各种操作:

一、隐藏属性页默认按钮

隐藏掉Apply应用按钮:

1. propsheet.m_psh.dwFlags |= PSH_NOAPPLYNOW;

或隐藏掉Cancel取消按钮:

1. CWnd *pWnd = GetDlgItem( IDCANCEL );
2. pWnd->ShowWindow( FALSE );

二、移动属性页按钮

首先,要获取按钮的句柄,然后就可以象对待窗体一样处理它们了. 下面代码先隐藏掉Apply和Help铵钮,再把OK和Cancel按移动到右侧。

01. BOOL CMyPropSheet::OnInitDialog () 
02. {
03.     BOOL bResult = CPropertySheet::OnInitDialog();
04.   
05.     int ids [] = {IDOK, IDCANCEL};//, ID_APPLY_NOW, IDHELP };
06.       
07.     // Hide Apply and Help buttons
08.     CWnd *pWnd = GetDlgItem (ID_APPLY_NOW);
09.     pWnd->ShowWindow (FALSE);
10.     pWnd = GetDlgItem (IDHELP);
11.     pWnd->ShowWindow (FALSE);
12.       
13.     CRect rectBtn;
14.     int nSpacing = 6;        // space between two buttons...
15.   
16.     for( int i =0; i < sizeof(ids)/sizeof(int); i++)
17.     {
18.         GetDlgItem (ids [i])->GetWindowRect (rectBtn);
19.           
20.         ScreenToClient (&rectBtn);
21.         int btnWidth = rectBtn.Width();
22.         rectBtn.left = rectBtn.left + (btnWidth + nSpacing)* 2;
23.         rectBtn.right = rectBtn.right + (btnWidth + nSpacing)* 2;
24.   
25.         GetDlgItem (ids [i])->MoveWindow(rectBtn);
26.     }
27.   
28.       
29.     return bResult;
30. }

下面代码移动所有按钮到右侧,并且重新置属性页为合适的大小.

01. BOOL CMyPropSheet::OnInitDialog () 
02. {
03.     BOOL bResult = CPropertySheet::OnInitDialog();
04.   
05.       
06.     int ids[] = { IDOK, IDCANCEL, ID_APPLY_NOW };
07.       
08.     CRect rectWnd;
09.     CRect rectBtn;
10.       
11.     GetWindowRect (rectWnd);
12.     GetDlgItem (IDOK)->GetWindowRect (rectBtn);
13.       
14.     int btnWidth = rectBtn.Width();
15.     int btnHeight = rectBtn.Height();
16.     int btnOffset = rectWnd.bottom - rectBtn.bottom;
17.     int btnLeft = rectWnd.right - rectWnd.left;
18.   
19.     rectWnd.bottom = rectBtn.top;
20.     rectWnd.right = rectWnd.right + btnWidth + btnOffset;
21.     MoveWindow(rectWnd);
22.       
23.     rectBtn.left = btnLeft;
24.     rectBtn.right = btnLeft + btnWidth;
25.   
26.     for (int i = 0; i < sizeof (ids) / sizeof (int); i++)
27.     {
28.         rectBtn.top = (i + 1) * btnOffset + btnHeight * i;
29.         rectBtn.bottom = rectBtn.top + btnHeight;
30.         GetDlgItem (ids [i])->MoveWindow (rectBtn);
31.     }
32.       
33.     return bResult;
34. }

 三、改变属性页上的标签文字

首先修改TC_ITEM结构,然后用 SetItem 来修改标签文字,如下代码:

1. TC_ITEM item;
2. item.mask = TCIF_TEXT;
3. item.pszText = "New Label";
4.   
5. //Change the label of the first tab (0 is the index of the first tab)...
6. GetTabControl ()->SetItem (0, &item);

四、改变属性页标签文字的字体属性

代码如下

1. m_NewFont.CreateFont (14, 0, 0, 0, 800, TRUE, 0, 0, 1, 0, 0, 0, 0, _T("Arial") );
2.     GetTabControl()->SetFont (&m_NewFont);

五、在属性页标签上显示位图

可以用 CImageList 建立图像. 用 SetItem 来设置,如下代码所示:

01. BOOL CMyPropSheet::OnInitDialog ()
02. {
03.     BOOL bResult = CPropertySheet::OnInitDialog();
04.   
05.     m_imageList.Create (IDB_MYIMAGES, 13, 1, RGB(255,255,255));
06.     CTabCtrl *pTabCtrl = GetTabControl ();
07.     pTabCtrl->SetImageList (&m_imageList);
08.       
09.     TC_ITEM item;
10.     item.mask = TCIF_IMAGE;
11.     for (int i = 0; i < NUMBER_OF_TABS; i++)
12.     {
13.         item.iImage = i;
14.         pTabCtrl->SetItem (i, &item );
15.     }
16.   
17.     return bResult;
18. }

六、在属性页左下角显示位图

如下代码所示:

01. void CMyPropSheet::OnPaint () 
02. {
03.     CPaintDC dc(this); // device context for painting
04.       
05.     int nOffset = 6;
06.     // load IDB_BITMAP1 from our resources
07.     CBitmap bmp;
08.     if (bmp.LoadBitmap (IDB_BITMAP1))
09.     {
10.         // Get the size of the bitmap
11.         BITMAP bmpInfo;
12.         bmp.GetBitmap (&bmpInfo);
13.           
14.         // Create an in-memory DC compatible with the
15.         // display DC we''re using to paint
16.         CDC dcMemory;
17.         dcMemory.CreateCompatibleDC (&dc);
18.           
19.         // Select the bitmap into the in-memory DC
20.         CBitmap* pOldBitmap = dcMemory.SelectObject (&bmp);
21.           
22.         // Find a bottom-left point for the bitmap in the client area
23.         CRect rect;
24.         GetClientRect (&rect);
25.         int nX = rect.left + nOffset;
26.         int nY = rect.top + (rect.Height () - bmpInfo.bmHeight) - nOffset;
27.           
28.         // Copy the bits from the in-memory DC into the on-
29.         // screen DC to actually do the painting. Use the centerpoint
30.         // we computed for the target offset.
31.         dc.BitBlt (nX, nY, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory, 
32.             0, 0, SRCCOPY);
33.           
34.         dcMemory.SelectObject (pOldBitmap);
35.     }
36.   
37.     // Do not call CPropertySheet::OnPaint() for painting messages
38. }

七、在属性页右下角显示3D文字Logo

代码如下:

01. void CMyPropSheet::OnPaint () 
02. {
03.     /
04.     //在TAB按钮旁边显示3D文字提示,jingzhou xu
05.     Cstring m_LogoName = “属性页”;
06. //  if(m_LogoName == "")
07. //      return;
08.   
09.     GetWindowRect(rect);
10.     ScreenToClient(rect);
11.       
12.     LOGFONT logFont;
13.     ZeroMemory((void*)&logFont,sizeof(logFont));
14.     strcpy(logFont.lfFaceName,"宋体");
15.     logFont.lfHeight = -12;
16.     logFont.lfWeight = 400;
17.     logFont.lfCharSet = GB2312_CHARSET;
18.     logFont.lfOutPrecision = 3;
19.     logFont.lfClipPrecision = 2; 
20.     logFont.lfQuality = 1;
21.     logFont.lfPitchAndFamily = 2;
22.     m_font.CreateFontIndirect(&logFont);
23.     SetFont(&m_font);
24.     CFont   *pOldFont = pDC->SelectObject(&m_font);
25.   
26.         rect.left += 6;
27.         rect.right -= 6;
28.         rect.bottom -= 1;
29.         rect.top = rect.bottom - ITEMBUTTON_HEIGHT + 1;
30.       
31.   
32.     CFont m_LogoFont;
33.     CString sLogoString;
34.           
35.     m_LogoFont.CreateFont(rect.Height()*4/5, 0, 0, 0, FW_BOLD, 1, FALSE, FALSE,
36.             DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
37.             FIXED_PITCH | FF_ROMAN, "楷体_GB2312");
38.           
39.     sLogoString = m_LogoName;
40.           
41.     RECT m_rDataBox;
42.     CopyRect(&m_rDataBox,&rect);
43.           
44.     TEXTMETRIC tm;
45.     pDC->GetTextMetrics(&tm);
46.     CFont* oldFont = pDC->SelectObject(&m_LogoFont);
47.     CSize sz = pDC->GetTextExtent(sLogoString, sLogoString.GetLength());
48.     //用GetTextExtent来计算字体logo大小,依靠于设备环境,使用logo位于右下角
49.     m_rDataBox.left = m_rDataBox.right  - sz.cx - tm.tmAveCharWidth/2;
50.     m_rDataBox.top  = m_rDataBox.bottom - sz.cy - tm.tmHeight/5;
51.     pDC->SetBkMode(TRANSPARENT);
52.     //用3D字体显示,先黑后白,最后再用默认色
53.     COLORREF oldColor = pDC->SetTextColor(GetSysColor(COLOR_3DDKSHADOW));
54.     pDC->DrawText(sLogoString, sLogoString.GetLength(), &m_rDataBox, DT_VCENTER | DT_SINGLELINE | DT_CENTER);
55.     m_rDataBox.left -= tm.tmAveCharWidth;
56.     pDC->SetTextColor(GetSysColor(COLOR_3DHILIGHT));
57.     pDC->DrawText(sLogoString, sLogoString.GetLength(), &m_rDataBox, DT_VCENTER | DT_SINGLELINE | DT_CENTER);
58.     m_rDataBox.left += 3*tm.tmAveCharWidth/5;
59.     pDC->SetTextColor(RGB(0,0,255));
60.     pDC->DrawText(sLogoString, sLogoString.GetLength(), &m_rDataBox, DT_VCENTER | DT_SINGLELINE | DT_CENTER);
61.           
62.     //释放资源
63.     pDC->SelectObject(oldFont);
64.     pDC->SetTextColor(oldColor);   
65.     m_LogoFont.DeleteObject();
66.     //
67. }

八、在属性页中动态加入其它控件

下面演示如何在左下角加入一Edit控件:

MyPropSheet.h中:

1. public:
2.     CEdit m_edit;

MyPropSheet.cpp中:

01. BOOL CMyPropSheet::OnInitDialog ()
02. {
03.     BOOL bResult = CPropertySheet::OnInitDialog ();
04.   
05.       
06.     CRect rect;
07.       
08.     int nHeight = 24;
09.     int nWidth = 120;
10.     int nOffset = 6;
11.       
12.     GetClientRect (&rect);
13.   
14.     // Find a bottom-left point for the edit control in the client area
15.     int nX = rect.left + nOffset;
16.     int nY = rect.top + (rect.Height() - nHeight) - nOffset;
17.       
18.     // finally create the edit control
19.     m_Edit.CreateEx (WS_EX_CLIENTEDGE, _T("EDIT"), NULL,
20.                      WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER, 
21.         nX, nY, nWidth, nHeight, m_hWnd, 0, 0 );
22.   
23.     return bResult;
24. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值