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

CXPStyleButtonST v1.2

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.77/5 (26投票s)

2002年1月22日

3分钟阅读

viewsIcon

254458

downloadIcon

11063

Windows XP 按钮,具有所有 CButtonST 功能。

Sample Image Sample Image

SoftechSoftware 主页
SoftechSoftware 电子邮件

引言

CXPStyleButtonST 是一个可伸缩的、派生自 CButtonST 的控件。如果在 Windows XP 下运行,应用程序按钮将使用当前选定的主题进行皮肤化(如果存在任何主题),而如果在 Windows 9x/ME/NT/2000 下运行,按钮将具有标准的、众所周知的 CButtonST 平面样式。在这两种情况下,都保证所有 CButtonST 的功能!使用 CXPStyleButtonST - 以零成本实现 - 您的应用程序在 XP 下运行时将具有新的、平滑和优雅的样式,并且在旧的 Windows 版本下也能无错误地运行。

将 CXPStyleButtonST 集成到您的应用程序中

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

  • BtnST.h
  • BtnST.cpp
  • XPStyleButtonST.h
  • XPStyleButtonST.cpp
  • ThemeHelperST.h
  • ThemeHelperST.cpp

创建 CThemeHelperST 的实例。 此类封装了所有必需的 API,以访问当前选定的主题(如果存在),或者如果在旧的 Windows 版本下运行或未选择任何主题,则无错误地运行。 您只需要一个按钮的所有实例,因此这可以全局用于应用程序或仅用于当前对话框。

CThemeHelperST m_ThemeHelper;

静态创建 CXPStyleButtonST 对象

使用对话框编辑器创建一个标准按钮,例如,名为 IDOK (您不需要使其成为所有者绘制),并为此按钮创建一个成员变量

CXPStyleButtonST m_btnOk;

现在将按钮附加到 CXPStyleButtonST。对于基于对话框的应用程序,在您的 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);

CThemeHelperST 实例分配给按钮。这是至关重要的,否则您的按钮即使在 XP 下运行也不会具有当前主题样式!在您的 OnInitDialog

// Assign theme helper instance to the button
m_btnOk.SetThemeHelper(&m_ThemeHelper);

动态创建 CXPStyleButtonST 对象

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

CXPStyleButtonST* m_pbtnOk;

现在创建按钮。对于基于对话框的应用程序,在您的 OnInitDialog

// Call the base-class method
CDialog::OnInitDialog();

// Create the IDOK button
m_pbtnOk = new CXPStyleButtonST;
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());

如上一节所述,将 CThemeHelperST 实例分配给该按钮。 记住销毁按钮,否则您将遇到内存泄漏。 例如,这可以在您的类析构函数中完成

if (m_pbtnOk) delete m_pbtnOk;

类方法

SetThemeHelper
CThemeHelperST 实例分配给按钮。

// Parameters:
//     [IN]   pTheme
//            Pointer to a CThemeHelperST instance.
//            Pass NULL to remove any previous instance.
//
void SetThemeHelper(CThemeHelperST* pTheme)

DrawAsToolbar
设置是否必须使用平面工具栏样式绘制按钮。

// Parameters:
//     [IN]   bDrawAsToolbar
//            If TRUE the button will be drawn using the flat toolbar-style.
//     [IN]   bRepaint
//            If TRUE the control will be repainted.
//
// Return value:
//     BTNST_OK
//        Function executed successfully.
//
DWORD DrawAsToolbar(BOOL bDrawAsToolbar, BOOL bRepaint = TRUE)

GetVersionI
以短数值形式返回类版本。

// Return value:
//     Class version. Divide by 10 to get actual version.
//
static short GetVersionI()

GetVersionC
以字符串值的形式返回类版本。

// Return value:
//     Pointer to a null-terminated string containing the class version.
//
static LPCTSTR GetVersionC()

备注

要编译 CXPStyleButtonST,您需要 Platform SDK(2001 年 8 月或更新版本)。 这不是强制性的,因为在编译时,如果找不到,它将被模拟。 在运行时,您不需要安装任何其他 SDK 或库。

历史

  • v1.2 (2003年1月24日)
    添加了 DrawAsToolbar 方法
    与 CButtonST v3.8 兼容
  • v1.1 (2002年10月31日)
    修复了按下按钮时的视觉错误
  • v1.0 (2002年1月21日)
    首次发布

致谢

CThemeHelperST 基于 David Yuheng Zhao (yuheng_zhao@yahoo.com) 发布的 CVisualStylesXP 代码。 非常感谢!

免责声明

本软件和随附文件“按原样”分发,不提供任何明示或暗示的担保。 对于可能的损害甚至功能不承担任何责任。 用户必须承担使用本软件的全部风险。

使用条款

本软件可免费用于个人用途或免费软件应用程序。 如果您在商业或共享软件应用程序中使用本软件,请您向作者捐赠 5 美元(五美元)

Davide Calabro'
P.O. Box 65
21019 Somma Lombardo (VA)
意大利

© . All rights reserved.