全功能的自绘按钮CButtonST类

来源:互联网 发布:好看的古装电影知乎 编辑:程序博客网 时间:2024/05/14 10:15

翻译来源:https://www.codeproject.com/Articles/57/CButtonST-v-MFC-Flat-buttons?msg=5115581#xx5115581xx

这个类很常见,在很多源码中都有见到。

包含BtnST.cpp和BtnST.h

  • 下载演示项目 - 902 Kb
  • 下载源 - 19 Kb

样品图像 样品图像 样品图像 样品图像

SoftechSoftware主页
SoftechSoftware Email


抽象

CButtonST是一个派生自MFC CButton类的类。
使用这个课程,您的应用程序可以具有标准按钮或具有“平面”风格的新的和现代的按钮!

主要CButtonST功能有:

  • 标准CButton属性
  • 文本和图标(或位图)在同一按钮上
  • 只有文字或只有图标/位图按钮
  • 支持任何尺寸的图标(最多256色)
  • 支持位图
  • 支持透明按钮(用于位图应用程序)
  • 标准或平面按钮款式
  • 将运行时间从平面更改为标准样式
  • 按钮可以有两个图像。当鼠标在按钮上方时,当鼠标在外部时(仅适用于“平面”按钮)
  • 每种颜色都可以定制
  • 可以通过DDX_调用使用
  • 可以在DLL中使用
  • 可以动态创建
  • 每个按钮都可以有自己的鼠标指针
  • 当窗口处于非活动状态时,按钮也很危险,就像Internet Explorer中发生的那样
  • 内置支持多线工具提示
  • 内置菜单基本支持
  • 内置支持所有者绘制菜单(使用BCMenu类)
  • 内置基本支持声音
  • 可以导出创建默认情况下不提供的其他按钮样式
  • 包含完整的源代码!
  • UNICODE兼容
  • 现有应用中无需成本实现
位图应用程序
点击此处查看使用CButtonST制作的真实世界应用程序。

如何在您的应用程序中集成CButtonST

在您的项目中包括以下文件:

  • BtnST.h
  • BtnST.cpp
从版本3.5开始,CButtonST现在支持使用BCMenu类创建的菜单。
这个第三方课程让您能够使用最新的视觉风格来显示菜单,如
最新的Microsoft产品或Windows XP中所示。
最新的BCMenu版本可以在这里找到

要启用对BCMenu的支持,必须启用BtnST.h中的以下两行:
#defineBTNST_USE_BCMENU#include "BCMenu.h"
另外,您的项目中必须包含以下文件:
  • BCMenu.h
  • BCMenu.cpp
注意:请注意,当启用BCMenu支持时,SetMenu方法接受的参数是不同的!

从版本3.6开始,CButtonST可以在特定按钮状态下播放声音。

要启用支持声音,必须启用BtnST.h中的以下行:
#defineBTNST_USE_SOUND
这样可以访问SetSound方法。

静态创建CButtonST对象
使用对话框编辑器创建一个标准按钮,例如,IDOK(您不需要将其绘制为所有者),并为此按钮创建一个成员变量:
CButtonST m_btnOk;
现在将按钮附加到CButtonST。对于基于对话的应用程序,在OnInitDialog中:
// Call the base-class methodCDialog::OnInitDialog();// Create the IDOK buttonm_btnOk.SubclassDlgItem(IDOK, this);
或在您的DoDataExchange中:
// Call the base methodCDialog::DoDataExchange(pDX);// Create the IDOK buttonDDX_Control(pDX, IDOK, m_btnOk);
动态创建CButtonST对象
在应用程序中,为该按钮创建一个成员变量。请注意,这个变量是一个指针
CButtonST* m_pbtnOk;
现在创建按钮。对于基于对话的应用程序,在OnInitDialog中:
// Call the base-class methodCDialog::OnInitDialog();// Create the IDOK buttonm_pbtnOk = new CButtonST;m_pbtnOk->Create(_T("&Ok"), WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP, <BR>                CRect(10, 10, 200, 100), this, IDOK);// Set the same font of the applicationm_pbtnOk->SetFont(GetFont());
记住要摧毁按钮,否则会发生内存泄漏。这可以做到,例如,在你的类析构函数中:
if (m_pbtnOk) delete m_pbtnOk;

类方法

SetIcon(使用多尺寸资源)
为按钮分配图标。
任何以前的图标或位图都将被删除。

// Parameters://     [IN]   nIconIn//            ID number of the icon resource to show when the mouse is over the<BR>//            button.  Pass NULL to remove any icon from the button.//     [IN]   nCxDesiredIn//            Specifies the width, in pixels, of the icon to load.//     [IN]   nCyDesiredIn//            Specifies the height, in pixels, of the icon to load.//     [IN]   nIconOut//            ID number of the icon resource to show when the mouse is outside <BR>//            the button. Can be NULL.//            If this parameter is the special value BTNST_AUTO_GRAY (cast to int) <BR>//            the second icon will be automatically created starting from nIconIn <BR>//            and converted to grayscale.//            If this parameter is the special value BTNST_AUTO_DARKER (cast <BR>//            to int) the second icon will be automatically created 25% <BR>//            darker starting from nIconIn.//     [IN]   nCxDesiredOut//            Specifies the width, in pixels, of the icon to load.//     [IN]   nCyDesiredOut//            Specifies the height, in pixels, of the icon to load.//// Return value://      BTNST_OK//          Function executed successfully.//      BTNST_INVALIDRESOURCE//          Failed loading the specified resource.//DWORD SetIcon(int nIconIn, int nCxDesiredIn, int nCyDesiredIn, <BR>              int nIconOut = NULL, int nCxDesiredOut = 0, int nCyDesiredOut = 0)
SetIcon(使用资源)
将图标分配给按钮。
任何以前的图标或位图都将被删除。
// Parameters://     [IN]   nIconIn//            ID number of the icon resource to show when the mouse is over the <BR>//            button.//            Pass NULL to remove any icon from the button.//     [IN]   nIconOut//            ID number of the icon resource to show when the mouse is <BR>//            outside the button. Can be NULL.//            If this parameter is the special value BTNST_AUTO_GRAY (cast to int) <BR>//            the second icon will be automatically created starting from <BR>//            nIconIn and converted to grayscale. If this parameter is the <BR>//            special value BTNST_AUTO_DARKER (cast to int) the second//            icon will be automatically created 25% darker starting from nIconIn.//// Return value://      BTNST_OK//          Function executed successfully.//      BTNST_INVALIDRESOURCE//          Failed loading the specified resource.//DWORD SetIcon(int nIconIn, int nIconOut = NULL)
SetIcon(使用手柄)
将图标分配给按钮。
任何以前的图标或位图都将被删除。
// Parameters://     [IN]   hIconIn//            Handle fo the icon to show when the mouse is over the button.//            Pass NULL to remove any icon from the button.//     [IN]   hIconOut//            Handle to the icon to show when the mouse is outside the button. <BR>//            Can be NULL.//            If this parameter is the special value BTNST_AUTO_GRAY the second//            icon will be automatically created starting from hIconIn and <BR>//            converted to grayscale.//            If this parameter is the special value BTNST_AUTO_DARKER the second//            icon will be automatically created 25% darker starting from hIconIn.//// Return value://      BTNST_OK//          Function executed successfully.//      BTNST_INVALIDRESOURCE//          Failed loading the specified resource.//DWORD SetIcon(HICON hIconIn, HICON hIconOut = NULL)
SetBitmaps(使用资源)
为按钮分配位图。
任何以前的图标或位图都将被删除。
// Parameters://     [IN]   nBitmapIn//            ID number of the bitmap resource to show when the mouse is <BR>//            over the button.//            Pass NULL to remove any bitmap from the button.//     [IN]   crTransColorIn//            Color (inside nBitmapIn) to be used as transparent color.//     [IN]   nBitmapOut//            ID number of the bitmap resource to show when the mouse <BR>//            is outside the button.//            Can be NULL.//     [IN]   crTransColorOut//            Color (inside nBitmapOut) to be used as transparent color.//// Return value://     BTNST_OK//        Function executed successfully.//     BTNST_INVALIDRESOURCE//        Failed loading the specified resource.//     BTNST_FAILEDMASK//        Failed creating mask bitmap.//DWORD SetBitmaps(int nBitmapIn, COLORREF crTransColorIn, <BR>                 int nBitmapOut = NULL, COLORREF crTransColorOut = 0)

SetBitmaps(使用句柄)
为按钮分配位图。
任何以前的图标或位图都将被删除。

// Parameters://     [IN]   hBitmapIn//            Handle fo the bitmap to show when the mouse is over the button.//            Pass NULL to remove any bitmap from the button.//     [IN]   crTransColorIn//            Color (inside hBitmapIn) to be used as transparent color.//     [IN]   hBitmapOut//            Handle to the bitmap to show when the mouse is outside the button.//            Can be NULL.//     [IN]   crTransColorOut//            Color (inside hBitmapOut) to be used as transparent color.//// Return value://     BTNST_OK//        Function executed successfully.//     BTNST_INVALIDRESOURCE//        Failed loading the specified resource.//     BTNST_FAILEDMASK//        Failed creating mask bitmap.//DWORD SetBitmaps(HBITMAP hBitmapIn, COLORREF crTransColorIn, <BR>                 HBITMAP hBitmapOut = NULL, COLORREF crTransColorOut = 0)

SetFlat
将按钮设置为具有标准或平面样式。

// Parameters://     [IN]   bFlat//            If TRUE the button will have a flat style, else//            will have a standard style.//            By default, CButtonST buttons are flat.//     [IN]   bRepaint//            If TRUE the control will be repainted.//// Return value://     BTNST_OK//        Function executed successfully.//DWORD SetFlat(BOOL bFlat = TRUE, BOOL bRepaint = TRUE)
SetAlign
设置图标/位图和文本之间的对齐类型。
// Parameters://     [IN]   byAlign//            Alignment type. Can be one of the following values://            ST_ALIGN_HORIZ          Icon/bitmap on the left, text on the right//            ST_ALIGN_VERT           Icon/bitmap on the top, text on the bottom//            ST_ALIGN_HORIZ_RIGHT    Icon/bitmap on the right, text on the left//            ST_ALIGN_OVERLAP        Icon/bitmap on the same space as text//            By default, CButtonST buttons have ST_ALIGN_HORIZ alignment.//     [IN]   bRepaint//            If TRUE the control will be repainted.//// Return value://     BTNST_OK//        Function executed successfully.//     BTNST_INVALIDALIGN//        Alignment type not supported.//DWORD SetAlign(BYTE byAlign, BOOL bRepaint = TRUE)
SetPressedStyle
设置按下的样式。
// Parameters://     [IN]   byStyle//            Pressed style. Can be one of the following values://            BTNST_PRESSED_LEFTRIGHT     Pressed style from left to right (as usual)//            BTNST_PRESSED_TOPBOTTOM     Pressed style from top to bottom//            By default, CButtonST buttons have BTNST_PRESSED_LEFTRIGHT style.//     [IN]   bRepaint//            If TRUE the control will be repainted.//// Return value://     BTNST_OK//        Function executed successfully.//     BTNST_INVALIDPRESSEDSTYLE//        Pressed style not supported.//DWORD SetPressedStyle(BYTE byStyle, BOOL bRepaint = TRUE)
SetCheck
设置复选框的状态。
如果按钮不是复选框,则此功能无意义。
// Parameters://     [IN]   nCheck//            1 to check the checkbox.//            0 to un-check the checkbox.//     [IN]   bRepaint//            If TRUE the control will be repainted.//// Return value://     BTNST_OK//        Function executed successfully.//DWORD SetCheck(int nCheck, BOOL bRepaint = TRUE)
GetCheck
返回复选框的当前状态。
如果按钮不是复选框,则此功能无意义。
// Return value://     The current state of the checkbox.//        1 if checked.//        0 if not checked or the button is not a checkbox.//int GetCheck()
SetDefaultColors
将所有颜色设置为默认值。
// Parameters://     [IN]   bRepaint//            If TRUE the control will be repainted.//// Return value://     BTNST_OK//        Function executed successfully.//DWORD SetDefaultColors(BOOL bRepaint = TRUE)
SetColor
设置用于特定状态的颜色。
// Parameters://     [IN]   byColorIndex//            Index of the color to set. Can be one of the following values://            BTNST_COLOR_BK_IN       Background color when mouse is over the button//            BTNST_COLOR_FG_IN       Text color when mouse is over the button//            BTNST_COLOR_BK_OUT      Background color when mouse is outside the button//            BTNST_COLOR_FG_OUT      Text color when mouse is outside the button//            BTNST_COLOR_BK_FOCUS    Background color when the button is focused//            BTNST_COLOR_FG_FOCUS    Text color when the button is focused//     [IN]   crColor//            New color.//     [IN]   bRepaint//            If TRUE the control will be repainted.//// Return value://     BTNST_OK//        Function executed successfully.//     BTNST_INVALIDINDEX//        Invalid color index.//DWORD SetColor(BYTE byColorIndex, COLORREF crColor, BOOL bRepaint = TRUE)
GetColor
返回用于特定状态的颜色。
// Parameters://     [IN]   byColorIndex//            Index of the color to get.//            See SetColor for the list of available colors.//     [OUT]  crpColor//            A pointer to a COLORREF that will receive the color.//// Return value://     BTNST_OK//        Function executed successfully.//     BTNST_INVALIDINDEX//        Invalid color index.//DWORD GetColor(BYTE byColorIndex, COLORREF* crpColor)
OffsetColor
此功能将偏移量应用于指定颜色的RGB分量。
该功能可以被看作是使颜色比默认值更暗或更轻的简单方法。
// Parameters://     [IN]   byColorIndex//            Index of the color to set.//            See SetColor for the list of available colors.//     [IN]   shOffsetColor//            A short value indicating the offset to apply to the color.//            This value must be between -255 and 255.//     [IN]   bRepaint//            If TRUE the control will be repainted.//// Return value://     BTNST_OK//        Function executed successfully.//     BTNST_INVALIDINDEX//        Invalid color index.//     BTNST_BADPARAM//        The specified offset is out of range.//DWORD OffsetColor(BYTE byColorIndex, short shOffset, BOOL bRepaint = TRUE)
SetAlwaysTrack
设置按钮的高亮逻辑。
仅适用于扁平按钮。
// Parameters://     [IN]   bAlwaysTrack//            If TRUE the button will be hilighted even if the window that owns it, is//            not the active window.//            If FALSE the button will be hilighted only if the window that owns it,//            is the active window.//// Return value://     BTNST_OK//        Function executed successfully.//DWORD SetAlwaysTrack(BOOL bAlwaysTrack = TRUE)
SetBtnCursor
设置当鼠标在按钮上方时使用的光标。
// Parameters://     [IN]   nCursorId//            ID number of the cursor resource.//            Pass NULL to remove a previously loaded cursor.//     [IN]   bRepaint//            If TRUE the control will be repainted.//// Return value://     BTNST_OK//        Function executed successfully.//     BTNST_INVALIDRESOURCE//        Failed loading the specified resource.//DWORD SetBtnCursor(int nCursorId = NULL, BOOL bRepaint = TRUE)
DrawBorder
设置是否必须绘制按钮边框。
仅适用于扁平按钮。
// Parameters://     [IN]   bDrawBorder//            If TRUE the border will be drawn.//     [IN]   bRepaint//            If TRUE the control will be repainted.//// Return value://     BTNST_OK//        Function executed successfully.//DWORD DrawBorder(BOOL bDrawBorder = TRUE, BOOL bRepaint = TRUE)
DrawFlatFocus
设置是否必须为平面按钮绘制焦点矩形。
// Parameters://     [IN]   bDrawFlatFocus//            If TRUE the focus rectangle will be drawn also for flat buttons.//     [IN]   bRepaint//            If TRUE the control will be repainted.//// Return value://     BTNST_OK//        Function executed successfully.//DWORD DrawFlatFocus(BOOL bDrawFlatFocus, BOOL bRepaint = TRUE)
SetTooltipText(使用资源)
设置在按钮工具提示中显示的文本。
// Parameters://     [IN]   nText//            ID number of the string resource containing the text to show.//     [IN]   bActivate//            If TRUE the tooltip will be created active.//void SetTooltipText(int nText, BOOL bActivate = TRUE)
SetTooltipText
设置在按钮工具提示中显示的文本。
// Parameters://     [IN]   lpszText//            Pointer to a null-terminated string containing the text to show.//     [IN]   bActivate//            If TRUE the tooltip will be created active.//void SetTooltipText(LPCTSTR lpszText, BOOL bActivate = TRUE)
EnableBalloonTooltip
启用使用气球样式显示工具提示。
在调用SetTooltipText之前,必须调用此函数。
// Return value://     BTNST_OK//        Function executed successfully.//DWORD EnableBalloonTooltip()
ActivateTooltip
启用或禁用按钮工具提示。
// Parameters://     [IN]   bActivate//            If TRUE the tooltip will be activated.//void ActivateTooltip(BOOL bEnable = TRUE)
GetDefault
返回如果按钮是默认按钮。
// Return value://     TRUE//        The button is the default button.//     FALSE//        The button is not the default button.//BOOL GetDefault()
DrawTransparent
启用透明模式。
注意:此操作不可逆。
DrawTransparent应该在创建按钮之后调用。
在您真正需要它之前,请勿使用trasparent按钮(您有位图
背景),因为每个透明按钮都会在其背景内存中复制内容。
这可能会导致不必要的内存使用和执行过载。
// Parameters://     [IN]   bRepaint//            If TRUE the control will be repainted.//void DrawTransparent(BOOL bRepaint = FALSE)
SetURL
设置点击按钮时打开的URL。
// Parameters://     [IN]   lpszURL//            Pointer to a null-terminated string that contains the URL.//            Pass NULL to removed any previously specified URL.//// Return value://     BTNST_OK//        Function executed successfully.//DWORD SetURL(LPCTSTR lpszURL = NULL)
SetMenu
将菜单关联到按钮。
菜单将显示单击按钮。
仅当BTNST_USE_BCMENU未定义时,此方法才可用
// Parameters://     [IN]   nMenu//            ID number of the menu resource.//            Pass NULL to remove any menu from the button.//     [IN]   hParentWnd//            Handle to the window that owns the menu.//            This window receives all messages from the menu.//     [IN]   bRepaint//            If TRUE the control will be repainted.//// Return value://     BTNST_OK//        Function executed successfully.//     BTNST_INVALIDRESOURCE//        Failed loading the specified resource.//DWORD SetMenu(UINT nMenu, HWND hParentWnd, BOOL bRepaint = TRUE)
SetMenu
将菜单关联到按钮。
菜单将显示单击按钮。
仅当定义了BTNST_USE_BCMENU时,此方法才可用该菜单将由BCMenu类处理。
// Parameters://     [IN]   nMenu//            ID number of the menu resource.//            Pass NULL to remove any menu from the button.//     [IN]   hParentWnd//            Handle to the window that owns the menu.//            This window receives all messages from the menu.//     [IN]   bWinXPStyle//            If TRUE the menu will be displayed using the new Windows XP style.//            If FALSE the menu will be displayed using the standard style.//     [IN]   nToolbarID//            Resource ID of the toolbar to be associated to the menu.//     [IN]   sizeToolbarIcon//            A CSize object indicating the size (in pixels) of each icon <BR>//            into the toolbar.//            All icons into the toolbar must have the same size.//     [IN]   crToolbarBk//            A COLORREF value indicating the color to use as background <BR>//            for the icons into the toolbar.//            This color will be used as the "transparent" color.//     [IN]   bRepaint//            If TRUE the control will be repainted.//// Return value://     BTNST_OK//        Function executed successfully.//     BTNST_INVALIDRESOURCE//        Failed loading the specified resource.//DWORD SetMenu(UINT nMenu,              HWND hParentWnd,              BOOL bWinXPStyle = TRUE,              UINT nToolbarID = NULL,              CSize sizeToolbarIcon = CSize(16, 16),              COLORREF crToolbarBk = RGB(255, 0, 255),              BOOL bRepaint = TRUE)
SetMenuCallback
设置在显示
与该按钮相关的菜单之前将发送到指定窗口的回调消息
// Parameters://     [IN]   hWnd//            Handle of the window that will receive the callback message.//            Pass NULL to remove any previously specified callback message.//     [IN]   nMessage//            Callback message to send to window.//     [IN]   lParam//            A 32 bits user specified value that will be passed to the <BR>//            callback function.//// Remarks://     the callback function must be in the form://     LRESULT On_MenuCallback(WPARAM wParam, LPARAM lParam)//     Where://            [IN]     wParam//                     If support for BCMenu is enabled: a pointer to BCMenu//                     else a HMENU handle to the menu that is being to be <BR>//                      displayed.//            [IN]     lParam//                     The 32 bits user specified value.//// Return value://     BTNST_OK//        Function executed successfully.//DWORD SetMenuCallback(HWND hWnd, UINT nMessage, LPARAM lParam = 0)
SizeToContent
将按钮调整为与图像大小相同的按钮。
为了获得良好的效果,IN和OUT图像应具有相同的大小。
void SizeToContent()
SetSound
设置特定按钮状态下必须播放的声音。
此方法仅在定义了BTNST_USE_SOUND时可用
// Parameters://     [IN]   lpszSound//            A string that specifies the sound to play.//            If hMod is NULL this string is interpreted as a filename, <BR>//            else it is interpreted as a resource identifier.//            Pass NULL to remove any previously specified sound.//     [IN]   hMod//            Handle to the executable file that contains the resource to <BR>//            be loaded.//            This parameter must be NULL unless lpszSound specifies a <BR>//            resource identifier.//     [IN]   bPlayOnClick//            TRUE if the sound must be played when the button is clicked.//            FALSE if the sound must be played when the mouse is moved over <BR>//            the button.//     [IN]   bPlayAsync//            TRUE if the sound must be played asynchronously.//            FALSE if the sound must be played synchronously. The <BR>//            application takes control after the sound is completely played.//// Return value://     BTNST_OK//        Function executed successfully.//DWORD SetSound(LPCTSTR lpszSound,                HMODULE hMod = NULL,                BOOL bPlayOnClick = FALSE,                BOOL bPlayAsync = TRUE)
OnDrawBackground
每次按钮背景需要绘制时,此函数被调用。
如果按钮处于透明模式,则不会调用此功能
这是一个虚拟函数,可以在CButtonST派生类中重写,
以产生默认情况下不可用的整个范围的按钮。
// Parameters://     [IN]   pDC//            Pointer to a CDC object that indicates the device context.//     [IN]   pRect//            Pointer to a CRect object that indicates the bounds of the//            area to be painted.//// Return value://     BTNST_OK//        Function executed successfully.//virtual DWORD OnDrawBackground(CDC* pDC, CRect* pRect)
OnDrawBorder
每当需要绘制按钮边框时调用此函数。
这是一个虚拟函数,可以在CButtonST派生类中重写,
以产生默认情况下不可用的整个范围的按钮。
// Parameters://     [IN]   pDC//            Pointer to a CDC object that indicates the device context.//     [IN]   pRect//            Pointer to a CRect object that indicates the bounds of the//            area to be painted.//// Return value://     BTNST_OK//        Function executed successfully.//virtual DWORD OnDrawBorder(CDC* pDC, CRect* pRect)
GetVersionI
将类版本作为一个简短的值返回。
// Return value://     Class version. Divide by 10 to get actual version.//static short GetVersionI()
GetVersionC
返回类版本作为字符串值。
// Return value://     Pointer to a null-terminated string containig the class version.//static LPCTSTR GetVersionC()

原创粉丝点击