带图标的 CButton






4.52/5 (22投票s)
一个小的类,
引言
最初的想法是创建一个小段代码,以便轻松地创建一个带有图标的按钮。我看到很多很棒的代码,但它们并不完全是我想要的。所以我决定创建我自己的 CIconButton
类。这个类可以轻松地在按钮上设置图标。
- 只需将一个按钮添加到你的对话框中。
- 将按钮样式设置为“ownerdraw”(所有者绘制),否则代码将无法工作。
- 然后使用类向导生成一个成员变量。
- 将类名从
CButton
更改为CIconButton
(确保你包含了“iconbutton.h”文件,并将“iconbutton.cpp”文件添加到你的项目中) - 在资源管理器中创建一个图标。
- 将图标设置为按钮,设置字体、字体大小等。
最新版本
可以为按钮添加工具提示文本。你可以通过调用函数 SetTipText ( "tooltip-text" );
来实现。
更多细节请查看演示项目。
方法
void SetIconID ( const UINT nID ) void SetTextColor ( const COLORREF color ) void SetItalic ( bool bVal = true ) void SetUnderline ( bool bVal = true ) void SetStrikeOut ( bool bVal = true ) void SetFaceName ( const CString &sVal ) void SetWeight ( const int nVal ) void SetHeight ( const int nVal) void SetWidth ( const int nVal) void SetIconSize ( const int x, const int y ) void SetIconRight ( bool bVal = true ) void SetTipText ( const CString &sTxt ) new void Disable ( void ); void Enable ( void );