65.9K
CodeProject 正在变化。 阅读更多。
Home

带图标的 CButton

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.52/5 (22投票s)

2002年3月2日

CPOL
viewsIcon

258096

downloadIcon

7282

一个小的类,用于创建带有图标的按钮。

Sample Image - iconbutton.jpg

引言

最初的想法是创建一个小段代码,以便轻松地创建一个带有图标的按钮。我看到很多很棒的代码,但它们并不完全是我想要的。所以我决定创建我自己的 CIconButton 类。这个类可以轻松地在按钮上设置图标。

  1. 只需将一个按钮添加到你的对话框中。
  2. 将按钮样式设置为“ownerdraw”(所有者绘制),否则代码将无法工作。
  3. 然后使用类向导生成一个成员变量。
  4. 将类名从 CButton 更改为 CIconButton(确保你包含了“iconbutton.h”文件,并将“iconbutton.cpp”文件添加到你的项目中)
  5. 在资源管理器中创建一个图标。
  6. 将图标设置为按钮,设置字体、字体大小等。

最新版本

可以为按钮添加工具提示文本。你可以通过调用函数 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 );

注意

我认为这是最后的增强,否则这个“小段代码”将会变得很大 ;-))
带有图标的 CButton - CodeProject - 代码之家
© . All rights reserved.