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

CCeButtonST v1.2

starIconstarIconstarIconstarIconstarIcon

5.00/5 (16投票s)

2001年12月6日

CPOL

4分钟阅读

viewsIcon

228674

downloadIcon

4072

MFC 带有文本和图标的扁平按钮的参考控件。让您的 CE 应用程序拥有专业外观!

Sample Image Sample Image Sample Image

摘要

CCeButtonST 是一个派生自 MFC CButton 类的类。
使用这个类,您的 CE 应用程序可以拥有标准按钮或全新现代的“扁平”风格按钮!

CCeButtonST 的主要功能包括:

  • 标准 CButton 属性
  • 按钮上的文本和图标
  • 仅文本或仅图标按钮
  • 支持任何大小的图标(最多256色)
  • 标准或扁平按钮样式
  • 运行时从扁平样式切换到标准样式
  • 按钮可以有两个图像。一个在鼠标悬停在按钮上时显示,一个在鼠标离开按钮时显示(仅适用于“扁平”按钮)
  • 所有颜色都可以自定义
  • 可以通过 DDX_ 调用使用
  • 可以在 DLL 中使用
  • 可以动态创建
  • 每个按钮都可以有自己的鼠标指针
  • 即使在窗口不活动时,按钮也会被高亮显示,就像 Internet Explorer 中的情况一样
  • 内置基本菜单支持
  • 可以派生以创建默认未提供的其他按钮样式
  • 包含完整的源代码!
  • 兼容 UNICODE
  • 兼容 Windows CE v3.0
  • 在现有应用程序中实现成本为零

如何将 CCeButtonST 集成到您的应用程序中

在你的项目中包含以下文件

  • CeBtnST.h
  • CeBtnST.cpp

静态创建 CCeButtonST 对象

使用对话框编辑器创建一个标准的按钮,例如命名为 IDOK(您无需将其设置为属主绘制),并为此按钮创建一个成员变量

CCeButtonST m_btnOk;
现在将按钮附加到 CCeButtonST。对于基于对话框的应用程序,在您的 OnInitDialog
// Call the base-class method
CDialog::OnInitDialog();

// Create the IDOK button
m_btnOk.SubclassDlgItem(IDOK, this);
或者在您的 DoDataExchange
// Call the base method
CDialog::DoDataExchange(pDX);

// Create the IDOK button
DDX_Control(pDX, IDOK, m_btnOk);

动态创建 CCeButtonST 对象

在您的应用程序中,为按钮创建一个成员变量。请注意,此变量是一个指针

CCeButtonST* m_pbtnOk;
现在创建按钮。对于基于对话框的应用程序,在您的 OnInitDialog
// Call the base-class method
CDialog::OnInitDialog();

// Create the IDOK button
m_pbtnOk = new CCeButtonST;
m_pbtnOk->Create(_T("&Ok"), WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP, CRect(10, 10, 200, 100), this, IDOK);
// Set the same font of the application
m_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 button.
//              Pass NULL to remove any icon from the button.
//      [IN]    sizeIn
//              Size of the icon.
//      [IN]    nIconOut
//              ID number of the icon resource to show when the mouse is outside the button.
//              Can be NULL.
//      [IN]    sizeOut
//              Size of the icon.
//      [IN]    nIconDis
//              ID number of the icon resource to show when the button is disabled.
//              Can be NULL.
//      [IN]    sizeDis
//              Size of the icon.
//
// Return value:
//      BTNST_OK
//          Function executed successfully.
//
DWORD SetIcon(int nIconIn, CSize sizeIn = CSize(32,32), int nIconOut = NULL, 
              CSize sizeOut = CSize(32,32), int nIconDis = NULL, CSize sizeDis = CSize(32,32))
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]   sizeIn
//            Size of the icon.
//     [IN]   hIconOut
//            Handle to the icon to show when the mouse is outside the button.
//            Can be NULL.
//     [IN]   sizeOut
//            Size of the icon.
//     [IN]   hIconDis
//            ID number of the icon resource to show when the button is disabled.
//            Can be NULL.
//     [IN]   sizeDis
//            Size of the icon.
//
// Return value:
//      BTNST_OK
//          Function executed successfully.
//
DWORD SetIcon(HICON hIconIn, CSize sizeIn = CSize(32,32), HICON hIconOut = NULL, 
              CSize sizeOut = CSize(32,32), HICON hIconDis = NULL, CSize sizeDis = CSize(32,32))
SetFlat
设置按钮为标准样式或扁平样式。
// Parameters:
//     [IN]   bFlat
//            If TRUE the button will have a flat style, else
//            will have a standard style.
//     [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 on the left, text on the right
//            ST_ALIGN_VERT           Icon on the top, text on the bottom
//            ST_ALIGN_HORIZ_RIGHT    Icon on the right, text on the left
//            By default, CCeButtonST 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)
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. 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
//     [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)
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)
GetDefault
返回按钮是否为默认按钮。
// Return value:
//     TRUE
//        The button is the default button.
//     FALSE
//        The button is not the default button.
//
BOOL GetDefault()
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
将菜单与按钮关联。
点击按钮时将显示菜单。
// 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)
OnDrawBackground
每次需要绘制按钮背景时都会调用此函数。
这是一个虚函数,可以在 CCeButtonST 派生类中重写
以生成默认未提供的各种按钮。
// 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, LPCRECT pRect)
OnDrawBorder
每次需要绘制按钮边框时都会调用此函数。
这是一个虚函数,可以在 CCeButtonST 派生类中重写
以生成默认未提供的各种按钮。
// 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, LPCRECT 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()

历史

  • v1.2 (2001 年 12 月 18 日)
    修正了 OnLButtonDown 中的 CE 错误
  • v1.1 (2001 年 11 月 28 日)
    修正了“DestroyCursor”问题
  • v1.0 (2001 年 10 月 22 日)
    首次发布

备注

演示应用程序展示了 CCeButtonST 类的几乎所有功能。
它包含了 Visual C++ eMbedded Tools v3.0 中所有模拟器的项目设置,以及
在 Advantech PCM-4823 单板计算机上编译和运行的设置。
CCeButtonST 的架构使得能够生成默认未提供的各种按钮。
如果有人实现了新的按钮样式,我很乐意将其代码包含在
下一个 CCeButtonST 演示应用程序中。

免责声明

本软件及配套文件按“原样”分发,不附带任何明示或暗示的保证。对于可能造成的损害甚至功能性问题,不承担任何责任。用户必须承担使用本软件的全部风险。

© . All rights reserved.