CCustomBitmapButton - MFC 位图按钮






4.69/5 (25投票s)
2004年9月17日
6分钟阅读

325005

9807
一个拥有绘制位图按钮和一个标题栏框架,包含在一个类中。
引言
CCustomBitmapButton
是一个派生自 CWnd
类的 MFC 控件。该按钮包含两部分:背景和前景。如果操作系统是 WinXP 并且启用了 XP 主题,背景将是从当前活动主题资源文件中加载的位图(我使用了类似的技术来绘制 CCustomTabCtrl 控件中的滚动按钮),否则将使用“DrawFrameControl
”函数来绘制按钮背景。前景是用户定义的单色位图(图标),以透明方式绘制在按钮背景上。
支持的功能
- 标准或 XP 主题视图
- 12 种预定义的背景样式
- 用户自定义前景(位图图标)
- 支持的按钮状态:“普通”、“鼠标悬停”、“按下”和“禁用”
- 可以在标题栏区域创建按钮
- 标题按钮支持对话框、SDI 和 MDI
- 无闪烁绘制
- 内置工具提示
使用代码
要将 CCustomBitmapButton
类作为标题框架集成到您的应用程序中,请按照以下步骤操作:
- 将 ThemeUtil.h、ThemeUtil.cpp、CustomBitmapButton.h、CustomBitmapButton.cpp、Tmschema.h 和 Schemadef.h 添加到您的项目中。
- 在适当的头文件中包含 CustomBitmapButton.h - 通常是在使用
CCustomBitmapButton
类的对话框类的头文件中。// CustomBitmapButtonDemoDlg.h : header file #include "CustomBitmapButton.h"
- 在您的对话框头文件中声明类型为
CCustomBitmapButton
的m_ctrlCaptionFrame
对象。// CustomBitmapButtonDemoDlg.h : header file class CCustomBitmapButtonDemoDlg : CDialog { ...... private: CCustomBitmapButton m_ctrlCaptionFrame; };
- 创建标题框架。
在您的对话框的
OnInitDialog
中,添加以下代码:// CustomBitmapButtonDemoDlg.cpp : definition file m_ctrlCaptionFrame.CreateCaptionFrame(this,IDR_MAINFRAME);
- 创建标题框架后,根据需要添加尽可能多的按钮。
要添加标题按钮,请在您的对话框的
OnInitDialog
中调用AddCaptionButton
。// CustomCaptionButtonDemoDlg.cpp : definition file m_ctrlCaptionFrame.AddCaptionButton(CRect(0,0,0,0),1, CBNBKGNDSTYLE_CLOSE, FALSE); m_ctrlCaptionFrame.AddCaptionButton(CRect(0,0,150,0),2, CBNBKGNDSTYLE_CAPTION, TRUE); CCustomBitmapButton* pBn1 = m_ctrlCaptionFrame.GetCaptionButtonPtr(1); if(pBn1) { CBitmap bmpGlyph1; bmpGlyph1.LoadBitmap(IDB_GLYPH1); pBn1->SetGlyphBitmap(bmpGlyph1); pBn1->SetTooltipText(_T("Double click to close the window, Right click to display the popup menu")); } CCustomBitmapButton* pBn2 = m_ctrlCaptionFrame.GetCaptionButtonPtr(2); if(pBn2) { CBitmap bmpGlyph2; bmpGlyph2.LoadBitmap(IDB_GLYPH2); pBn2->SetGlyphBitmap(bmpGlyph2); pBn2->SetTooltipText(_T("Articles by Andrzej Markowski")); }
- 在您的对话框类中处理来自标题按钮的
WM_NOTIFY
消息。当用户单击按钮时,按钮会将其通知消息(NM_CLICK
、NM_RCLICK
、NM_DBLCLK
、NM_RDBLCLK
)发送到其父窗口。如果您希望做出响应,请处理这些消息。// CustomBitmapButtonDemoDlg.cpp : definition file BEGIN_MESSAGE_MAP(CCustomBitmapButtonDemoDlg, CDialog) //{{AFX_MSG_MAP(CCustomBitmapButtonDemoDlg) ON_NOTIFY(NM_DBLCLK, 1, OnBnDblClickedCaptionbn1) ON_NOTIFY(NM_RCLICK, 1, OnBnRClickedCaptionbn1) ON_NOTIFY(NM_CLICK, 2, OnBnClickedCaptionbn2) //}}AFX_MSG_MAP END_MESSAGE_MAP() .... void CCustomBitmapButtonDemoDlg::OnBnDblClickedCaptionbn1 (NMHDR * pNotifyStruct, LRESULT * result) { CPoint pt; ::GetCursorPos(&pt); PostMessage(WM_SYSCOMMAND,SC_CLOSE,MAKEWORD(pt.x,pt.y)); } ....
- 请勿忘记销毁标题框架,否则会导致内存泄漏。
// CustomBitmapButtonDemoDlg.cpp : definition file void CCustomBitmapButtonDemoDlg::OnDestroy() { m_ctrlCaptionFrame.DestroyCaptionFrame(); CDialog::OnDestroy(); }
CCustomBitmapButton 类成员
构造/析构
CCustomBitmapButton | 构造一个 CCustomBitmapButton 对象。 |
Create | 创建一个位图按钮并将其附加到 CCustomBitmapButton 对象的实例。 |
CreateCaptionFrame | 创建一个标题框架。 |
DestroyCaptionFrame | 销毁一个标题框架。 |
属性
GetCaptionButtonPtr | 检索指向标题按钮的指针。 |
SetTooltipText | 更改按钮的工具提示文本。 |
GetBkStyle | 检索有关按钮控件背景样式的信息。 |
SetBkStyle | 更改按钮的背景样式。 |
GetGlyphBitmap | 检索之前使用 SetGlyphBitmap 设置的图标位图的句柄。 |
SetGlyphBitmap | 指定要显示在按钮上的图标位图。 |
EnableWindow | 启用或禁用按钮。 |
操作
AddCaptionButton | 在标题栏中插入一个新按钮。 |
CCustomBitmapButton::CCustomBitmapButton
CCustomBitmapButton( );
备注
调用此函数来构造一个 CCustomBitmapButton
对象。
CCustomBitmapButton::Create
BOOL Create( DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );
返回值
如果对象的初始化成功,则返回 TRUE
,否则返回 FALSE
。
参数
dwStyle
- 指定按钮的样式。rect
- 指定按钮的大小和位置。它可以是CRect
对象或RECT
结构。pParentWnd
- 指定按钮的父窗口。nID
- 指定按钮的 ID。
备注
创建一个位图按钮并将其附加到 CCustomBitmapButton
对象的实例。
CCustomBitmapButton::CreateCaptionFrame
int CreateCaptionFrame( CWnd* pCaptionWnd, int nIDIcon );
返回值
成功时返回 CBNERRR_NOERROR
,否则返回 CBNERRR_ERRORCODE
。
参数
pCaptionWnd
- 指定标题栏所有者窗口。nIDIcon
- 指定标题图标资源 ID。
备注
创建一个标题框架。
CCustomBitmapButton::DestroyCaptionFrame
int DestroyCaptionFrame();
返回值
成功时返回 CBNERRR_NOERROR
,否则返回 CBNERRR_ERRORCODE
。
备注
销毁一个标题框架。
CCustomBitmapButton::GetCaptionButtonPtr
CCustomBitmapButton* GetCaptionButtonPtr( int nButtonID ) const;
返回值
成功时返回指向 CCustomBitmapButton
对象的指针,否则返回 NULL
。
参数
nButtonID
- 标题按钮的 ID。
备注
调用此函数来检索指向标题按钮的指针。
CCustomBitmapButton::SetTooltipText
int SetTooltipText( CString sText
);
返回值
成功时返回 CBNERRR_NOERROR
,否则返回 CBNERRR_ERRORCODE
。
参数
sText
- 指向一个字符串对象的指针,该对象包含新的工具提示文本。
备注
此函数更改按钮的工具提示文本。
CCustomBitmapButton::GetBkStyle
int GetBkStyle();
返回值
返回此 CCustomBitmapButton
对象的按钮背景样式。
备注
调用此函数来检索有关按钮控件背景样式的信息。
CCustomBitmapButton::SetBkStyle
int SetBkStyle( int nBkStyle );
返回值
成功时返回 CBNERRR_NOERROR
,否则返回 CBNERRR_ERRORCODE
。
参数
nBkStyle
- 指定按钮背景样式。
支持的背景样式
CBNBKGNDSTYLE_CAPTION
- 框架按钮CBNBKGNDSTYLE_CLOSE
CBNBKGNDSTYLE_MDI
CBNBKGNDSTYLE_COMBO
- 组合框下拉按钮CBNBKGNDSTYLE_SPINUP
- 向上 spin 按钮CBNBKGNDSTYLE_SPINDN
CBNBKGNDSTYLE_SPINUPHOR
CBNBKGNDSTYLE_SPINDNHOR
CBNBKGNDSTYLE_SCROLLUP
- 滚动条按钮CBNBKGNDSTYLE_SCROLLDOWN
CBNBKGNDSTYLE_SCROLLLEFT
CBNBKGNDSTYLE_SCROLLRIGHT
备注
更改按钮的背景样式。
CCustomBitmapButton::GetGlyphBitmap
HBITMAP GetGlyphBitmap();
返回值
位图的句柄。如果之前未指定位图,则为 NULL
。
备注
调用此成员函数以获取与按钮关联的图标位图的句柄。
CCustomBitmapButton::SetGlyphBitmap
int SetGlyphBitmap( HBITMAP hBmpGlyph );
返回值
成功时返回 CBNERRR_NOERROR
,否则返回 CBNERRR_ERRORCODE
。
参数
hBmpGlyph
- 位图的句柄。
备注
调用此成员函数以将新的图标位图与按钮关联。
CCustomBitmapButton::EnableWindow
int EnableWindow( BOOL bEnable = TRUE);
返回值
成功时返回 CBNERRR_NOERROR
,否则返回 CBNERRR_ERRORCODE
。
参数
bEnable
- 指定要启用或禁用指定的窗口。如果此参数为TRUE
,则启用该窗口。如果此参数为FALSE
,则禁用该窗口。
备注
调用此函数以启用或禁用按钮控件。
CCustomBitmapButton::AddCaptionButton
int AddCaptionButton( LPCRECT lpRect, UINT nID, int nBkStyle, BOOL fUserDefWidth );
返回值
成功时返回 CBNERRR_NOERROR
,否则返回 CBNERRR_ERRORCODE
。
参数
lpRect
- 如果fUserDefWidth
为 true,则指定按钮的宽度,否则忽略。nID
- 指定按钮的 ID。nBkStyle
- 指定按钮背景样式。fUserDefWidth
- 如果为TRUE
- 用户定义的按钮宽度。
备注
调用此函数以在现有标题栏中插入一个新按钮。
通知消息
按钮控件支持以下通知代码:
NM_CLICK
- 用户在控件中单击了鼠标左键。NM_RCLICK
- 用户在控件中单击了鼠标右键。NM_DBLCLK
- 用户在控件中双击了鼠标左键。NM_RDBLCLK
- 用户在控件中双击了鼠标右键。
错误代码
按钮控件函数可能返回以下错误:
CBNERRR_NOERROR
- 操作成功。CBNERR_OUTOFMEMORY
- 由于没有足够的可用内存,函数调用失败。CBNERR_INCORRECTPARAMETER
- 由于指定了不正确的函数参数,函数调用失败。CBNERR_INCORRECTBUTTONSTYLE
- 由于指定了不正确的按钮样式,函数调用失败。CBNERR_INCORRECTFRAMESTYLE
- 由于未指定WS_CAPTION
样式,函数调用失败。CBNERR_INCORRECTFUNCCALL
- 函数调用不正确(例如:ob.SetTooltipText(...)
,其中ob
是标题框架对象的实例)。CBNERR_CREATEBUTTONFAILED
- 由于创建按钮控件失败,函数调用失败。CBNERR_CREATETOOLTIPCTRLFAILED
- 由于创建工具提示控件失败,函数调用失败。CBNERR_COPYIMAGEFAILED
- 由于复制图标图像失败,函数调用失败。CBNERR_SETWINDOWLONGFAILED
- 调用SetWindowLong
函数失败。CBNERR_FRAMEALREADYCREATED
- 由于标题框架已创建,函数调用失败。CBNERR_FRAMENOTCREATED
- 由于未创建标题框架,函数调用失败。