深入浅出 CPropertySheet

 
::首页 >> 文档中心 >> 在线杂志 >> 属性页 [ 在线杂志 第15期 ]

<script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script> width="728" scrolling="no" height="90" frameborder="0" allowtransparency="true" hspace="0" vspace="0" marginheight="0" marginwidth="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4159669282587342&dt=1178678261765&lmt=1178678261&format=728x90_as&output=html&url=http%3A%2F%2Fwww.vckbase.com%2Fdocument%2Fviewdoc%2F%3Fid%3D427&ad_type=image&ref=http%3A%2F%2Fwww.google.cn%2Fsearch%3Faq%3Do%26complete%3D1%26hl%3Dzh-CN%26newwindow%3D1%26q%3DCpropertysheet%26meta%3D&cc=100&flash=9&u_h=768&u_w=1024&u_ah=738&u_aw=1024&u_cd=32&u_tz=480&u_his=1&u_java=true&u_nplug=6&u_nmime=15" name="google_ads_frame">

 
 
[ 翻译文档 本文适合初级读者 已阅读41653次 ]

深入浅出 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 );
}

return bResult;
}

六、在属性页左下角显示位图
如下代码所示:
void CMyPropSheet::OnPaint () 
{
CPaintDC dc(this); // device context for painting

int nOffset = 6;
// load IDB_BITMAP1 from our resources
CBitmap bmp;
if (bmp.LoadBitmap (IDB_BITMAP1))
{
// Get the size of the bitmap
BITMAP bmpInfo;
bmp.GetBitmap (&bmpInfo);

// Create an in-memory DC compatible with the
// display DC we''re using to paint
CDC dcMemory;
dcMemory.CreateCompatibleDC (&dc);

// Select the bitmap into the in-memory DC
CBitmap* pOldBitmap = dcMemory.SelectObject (&bmp);

// Find a bottom-left point for the bitmap in the client area
CRect rect;
GetClientRect (&rect);
int nX = rect.left + nOffset;
int nY = rect.top + (rect.Height () - bmpInfo.bmHeight) - nOffset;

// Copy the bits from the in-memory DC into the on-
// screen DC to actually do the painting. Use the centerpoint
// we computed for the target offset.
dc.BitBlt (nX, nY, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory,
0, 0, SRCCOPY);

dcMemory.SelectObject (pOldBitmap);
}

// Do not call CPropertySheet::OnPaint() for painting messages
}

七、在属性页右下角显示3D文字Logo
代码如下:
void CMyPropSheet::OnPaint () 
{
/
//在TAB按钮旁边显示3D文字提示,jingzhou xu
Cstring m_LogoName = “属性页”;
// if(m_LogoName == "")
// return;

GetWindowRect(rect);
ScreenToClient(rect);

LOGFONT logFont;
ZeroMemory((void*)&logFont,sizeof(logFont));
strcpy(logFont.lfFaceName,"宋体");
logFont.lfHeight = -12;
logFont.lfWeight = 400;
logFont.lfCharSet = GB2312_CHARSET;
logFont.lfOutPrecision = 3;
logFont.lfClipPrecision = 2;
logFont.lfQuality = 1;
logFont.lfPitchAndFamily = 2;
m_font.CreateFontIndirect(&logFont);
SetFont(&m_font);
CFont *pOldFont = pDC->SelectObject(&m_font);

rect.left += 6;
rect.right -= 6;
rect.bottom -= 1;
rect.top = rect.bottom - ITEMBUTTON_HEIGHT + 1;


CFont m_LogoFont;
CString sLogoString;

m_LogoFont.CreateFont(rect.Height()*4/5, 0, 0, 0, FW_BOLD, 1, FALSE, FALSE,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
FIXED_PITCH | FF_ROMAN, "楷体_GB2312");

sLogoString = m_LogoName;

RECT m_rDataBox;
CopyRect(&m_rDataBox,&rect);

TEXTMETRIC tm;
pDC->GetTextMetrics(&tm);
CFont* oldFont = pDC->SelectObject(&m_LogoFont);
CSize sz = pDC->GetTextExtent(sLogoString, sLogoString.GetLength());
//用GetTextExtent来计算字体logo大小,依靠于设备环境,使用logo位于右下角
m_rDataBox.left = m_rDataBox.right - sz.cx - tm.tmAveCharWidth/2;
m_rDataBox.top = m_rDataBox.bottom - sz.cy - tm.tmHeight/5;
pDC->SetBkMode(TRANSPARENT);
//用3D字体显示,先黑后白,最后再用默认色
COLORREF oldColor = pDC->SetTextColor(GetSysColor(COLOR_3DDKSHADOW));
pDC->DrawText(sLogoString, sLogoString.GetLength(), &m_rDataBox, DT_VCENTER | DT_SINGLELINE | DT_CENTER);
m_rDataBox.left -= tm.tmAveCharWidth;
pDC->SetTextColor(GetSysColor(COLOR_3DHILIGHT));
pDC->DrawText(sLogoString, sLogoString.GetLength(), &m_rDataBox, DT_VCENTER | DT_SINGLELINE | DT_CENTER);
m_rDataBox.left += 3*tm.tmAveCharWidth/5;
pDC->SetTextColor(RGB(0,0,255));
pDC->DrawText(sLogoString, sLogoString.GetLength(), &m_rDataBox, DT_VCENTER | DT_SINGLELINE | DT_CENTER);

//释放资源
pDC->SelectObject(oldFont);
pDC->SetTextColor(oldColor);
m_LogoFont.DeleteObject();
/
}

八、在属性页中动态加入其它控件
下面演示如何在左下角加入一Edit控件:
MyPropSheet.h中:
public:
CEdit m_edit;
MyPropSheet.cpp中:
BOOL CMyPropSheet::OnInitDialog ()
{
BOOL bResult = CPropertySheet::OnInitDialog ();


CRect rect;

int nHeight = 24;
int nWidth = 120;
int nOffset = 6;

GetClientRect (&rect);

// Find a bottom-left point for the edit control in the client area
int nX = rect.left + nOffset;
int nY = rect.top + (rect.Height() - nHeight) - nOffset;

// finally create the edit control
m_Edit.CreateEx (WS_EX_CLIENTEDGE, _T("EDIT"), NULL,
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER,
nX, nY, nWidth, nHeight, m_hWnd, 0, 0 );

return bResult;
}

<script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script> width="468" scrolling="no" height="60" frameborder="0" allowtransparency="true" hspace="0" vspace="0" marginheight="0" marginwidth="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4159669282587342&dt=1178678266828&lmt=1178678261&alt_color=FFFFFF&prev_fmts=728x90_as&format=468x60_as&output=html&url=http%3A%2F%2Fwww.vckbase.com%2Fdocument%2Fviewdoc%2F%3Fid%3D427&color_bg=ECF8FF&color_text=6F6F6F&color_link=0000CC&color_url=008000&color_border=B4D0DC&ad_type=text_image&ref=http%3A%2F%2Fwww.google.cn%2Fsearch%3Faq%3Do%26complete%3D1%26hl%3Dzh-CN%26newwindow%3D1%26q%3DCpropertysheet%26meta%3D&cc=8&flash=9&u_h=768&u_w=1024&u_ah=738&u_aw=1024&u_cd=32&u_tz=480&u_his=1&u_java=true&u_nplug=6&u_nmime=15" name="google_ads_frame">

最新评论 [发表评论] [文章投稿] 查看所有评论 推荐给好友 打印

to bsnail & ladder
addpage前加一步操作
mypage.m_psp.dwFlags |= PSP_PREMATURE; ( lovelywanz 发表于 2007-1-6 10:45:00)
 
ladder:多个page时,默认是只显示一个page,而只有这个page才会初始化,其他的page只有你去点击他才会初始化,如何在属性页显示时就初始化所有page?
=======================================
如何解决该问题,谢谢 ( bsnail 发表于 2006-10-18 16:41:00)
 
yourpage.m_psp.dwFlags |= PREMATURE;
( Wolfgang_Jia 发表于 2005-6-9 10:51:00)
 
翻译的不错
原 文见: http://support.microsoft.com/default.aspx?scid=http: //support.microsoft.com:80/support/kb/articles/q140/5/87.asp&NoWebContent=1
要是再谈及如何disable(不是hide)Help button就更好了...
如果使用GetDlgItem(IDHELP)取得CWnd *,再设置EnableWindow(FALSE), 开始可以, 但切换到其他TAB后, HELP button 又变成Enabled了... 
( mingweb 发表于 2004-5-18 13:33:00)
 
多个page时,默认是只显示一个page,而只有这个page才会初始化,其他的page只有你去点击他才会初始化,如何在属性页显示时就初始化所有page? ( ladder 发表于 2003-6-7 12:37:00)
 
在工作的区里选对应对话框改属性为chinese后,在重写所有汉字。刚请教高手的。呵呵 ( Derk 发表于 2003-2-19 10:52:00)
 
改变属性页的字体,英文字体好像没有什么问题,
可是不知道为什么我就是无法显示汉字,我也改成使用中文字符集了,还是不行。 ( 自由飞翔 发表于 2002-11-23 3:54:00)
 
在属性页上加图标的例子,我也试了,可图像就是不出来啊。 ( rebel 发表于 2002-11-15 15:34:00)
 
都不能用的! ( 啃死你这垃圾 发表于 2002-11-7 13:36:00)
 
关于在属性页上加图标的问题:
我试验了但不成功,光是出现了给图表用的空间,但图标不能显示,不知为何?可否指点一二,多谢。 ( freepu 发表于 2002-10-2 3:29:00)
 
.......................................................
More...


版权所有 © 2006 VC知识库 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值