使用WindowsAPI,创建GroupBox:
#include <windows.h>
#include <commctrl.h>
#pragma comment(lib, "comctl32.lib")
#define ID_GROUPBOX 101
#define ID_BUTTON 102
LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_CREATE: {
// 创建组框控件
HWND hwndGroupBox = CreateWindowEx(0, L"BUTTON", L"Group Box",
WS_CHILD | WS_VISIBLE | BS_GROUPBOX,
10, 10, 200, 100, // x, y, width, height
hwnd, (HMENU)ID_GROUPBOX, NULL, NULL);
// 创建按钮控件
HWND hwndButton = CreateWindowEx(0, L"BUTTON", L"Click Me",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
20, 40, 150, 30, // x, y, width, height
hwndGroupBox, (HMENU)ID_BUTTON, NULL, NULL);
break;
}
case WM_COMMAND: {
if (LOWORD(wParam) == ID_BUTTON) {
MessageBox(hwnd, L"You clicked the button!", L"Notification", MB_OK);
}
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR, int nCmdShow) {
// 初始化 Common Controls
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(icex);
icex.dwICC = ICC_STANDARD_CLASSES; // 初始化标准控件类
InitCommonControlsEx(&icex);
// 创建窗口类
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = L"GroupBoxExample";
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
RegisterClass(&wc);
// 创建窗口
HWND hwnd = CreateWindowEx(
0, L"GroupBoxExample", L"GroupBox Control Example",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, 300, 200,
NULL, NULL, hInstance, NULL);
// 消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
效果图:
没有回复内容