使用WindowsAPI,创建ReBarWindow32控件:
#include <windows.h>
#include <commctrl.h>
#pragma comment(lib, "comctl32.lib")
#define NUMBUTTONS 3
#define ID_TOOLBAR 201
#define ID_COMBOBOX 202
#define ID_REBAR 203
HINSTANCE g_hInst;
// 函数声明
HWND CreateRebar(HWND hwndOwner, HWND hwndToolbar, HWND hwndCombo);
HWND hwndCombo;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_CREATE: {
// 创建工具条
HWND hwndToolbar = CreateToolbarEx(hwnd, WS_CHILD | WS_VISIBLE | TBSTYLE_TOOLTIPS,
ID_TOOLBAR, 0, NULL, 0, NULL, 0,
0, 0, 0, 0, sizeof(TBBUTTON));
// 添加按钮到工具条
TBBUTTON tbb[NUMBUTTONS];
for (int i = 0; i < NUMBUTTONS; ++i) {
tbb[i].iBitmap = -1; // 不使用图标
tbb[i].idCommand = 1000 + i; // 按钮 ID
tbb[i].fsState = TBSTATE_ENABLED;
tbb[i].fsStyle = TBSTYLE_BUTTON;
tbb[i].dwData = 0;
tbb[i].iString = (INT_PTR)L"Button"; // 按钮文本
}
SendMessage(hwndToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
SendMessage(hwndToolbar, TB_ADDBUTTONS, NUMBUTTONS, (LPARAM)&tbb);
SendMessage(hwndToolbar, TB_AUTOSIZE, 0, 0);
// 创建组合框
hwndCombo = CreateWindowEx(0, WC_COMBOBOX, NULL,
WS_CHILD | WS_VISIBLE | CBS_DROPDOWN | CBS_HASSTRINGS,
150, 0, 200, 200, hwnd, (HMENU)ID_COMBOBOX, g_hInst, NULL);
// 添加项到组合框
SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM)L"Option 1");
SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM)L"Option 2");
SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM)L"Option 3");
// 创建重条控件
HWND hwndRebar = CreateRebar(hwnd, hwndToolbar, hwndCombo);
break;
}
case WM_SIZE: {
// 调整重条控件的大小
RECT rc;
GetClientRect(hwnd, &rc);
MoveWindow(GetDlgItem(hwnd, ID_REBAR), 0, 0, rc.right, rc.bottom, TRUE);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
HWND CreateRebar(HWND hwndOwner, HWND hwndToolbar, HWND hwndCombo) {
// Check parameters.
if ((hwndToolbar == NULL) || (hwndCombo == NULL)) {
return NULL;
}
// Create the rebar.
HWND hwndRebar = CreateWindowEx(0,
REBARCLASSNAME,
NULL,
WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | RBS_VARHEIGHT | RBS_BANDBORDERS,
0, 0, 500, 100,
hwndOwner,
NULL,
g_hInst,
NULL);
if (!hwndRebar) {
return NULL;
}
// Initialize band info used by both bands.
REBARBANDINFO rbBand = { sizeof(REBARBANDINFO) };
rbBand.fMask = RBBIM_STYLE | RBBIM_CHILD | RBBIM_CHILDSIZE | RBBIM_SIZE | RBBIM_ID;
rbBand.fStyle = RBBS_CHILDEDGE | RBBS_NOGRIPPER;
// Get the height of the toolbar.
DWORD dwBtnSize = (DWORD)SendMessage(hwndToolbar, TB_GETBUTTONSIZE, 0, 0);
// Set values unique to the band with the toolbar.
rbBand.hwndChild = hwndToolbar;
rbBand.cyMinChild = LOWORD(dwBtnSize);
rbBand.cyMaxChild = LOWORD(dwBtnSize);
rbBand.cx = 500; // Default width of the toolbar
// Add the band that has the toolbar.
SendMessage(hwndRebar, RB_INSERTBAND, (WPARAM)-1, (LPARAM)&rbBand);
// 获取combo box的高度,设定足够的空间进行展开
int cyCombo = 200;
// Set values unique to the band with the combo box.
RECT rc;
GetWindowRect(hwndCombo, &rc);
MapWindowPoints(HWND_DESKTOP, hwndOwner, (LPPOINT)&rc, 2); // 映射至相对位置
rbBand.hwndChild = hwndCombo;
rbBand.lpText = const_cast<wchar_t*>(L"Options");
rbBand.cyMinChild = cyCombo; // 核心修改: 使用实际需求的高度
rbBand.cyMaxChild = cyCombo;
rbBand.cx = 200; // Width for the combo box
// Add the band that has the combo box.
SendMessage(hwndRebar, RB_INSERTBAND, (WPARAM)-1, (LPARAM)&rbBand);
return hwndRebar;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow) {
g_hInst = hInstance;
// Initialize common controls
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(icex);
icex.dwICC = ICC_BAR_CLASSES; // Initialize ReBar and ToolBar classes
InitCommonControlsEx(&icex);
// Create window class
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = L"ReBarExample";
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
RegisterClass(&wc);
// Create the main window
HWND hwnd = CreateWindowEx(
0, L"ReBarExample", L"ReBarWindow32 Example",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, 500, 200,
NULL, NULL, hInstance, NULL);
// Message loop
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
(此代码存在bug)
来源:https://learn.microsoft.com/zh-cn/windows/win32/controls/create-rebar-controls
没有回复内容