如何创建按钮 - Win32 apps

2026-07-25 05:12:38 8308

若要动态创建按钮,请使用 CreateWindow 或 CreateWindowEx 函数。 本主题演示如何使用 CreateWindow 函数创建默认推送按钮。

需要了解的事项

技术

Windows 控件

先决条件

C/C++

Windows 用户界面编程

说明

使用 CreateWindow 函数创建按钮控件。

在以下 C++ 示例中, m_hwnd 参数是父窗口的句柄。 BS_DEFPUSHBUTTON 样式指定应创建默认的推送按钮。 请注意,必须指定大小和位置值,因为对按钮使用 CW_USEDEFAULT 可将值设置为零。

HWND hwndButton = CreateWindow(

L"BUTTON", // Predefined class; Unicode assumed

L"OK", // Button text

WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles

10, // x position

10, // y position

100, // Button width

100, // Button height

m_hwnd, // Parent window

NULL, // No menu.

(HINSTANCE)GetWindowLongPtr(m_hwnd, GWLP_HINSTANCE),

NULL); // Pointer not needed.

相关主题

关于按钮

按钮控件参考

使用按钮

按钮