使用WindowsAPI,创建SysStatusBar控件:
#include <windows.h>
#include <commctrl.h>
#pragma comment(lib, "comctl32.lib")
HINSTANCE hInst;
HWND hwndStatusBar;
void InitializeStatusBar(HWND hwndParent)
{
hwndStatusBar = CreateWindowEx(
0, STATUSCLASSNAME, NULL,
WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP,
0, 0, 0, 0, // 初始位置和尺寸,将在 WM_SIZE 中调整
hwndParent, (HMENU)1, hInst, NULL);
int parts[] = { 150, 300, -1 }; // 修改部分宽度,使其更适合文本
SendMessage(hwndStatusBar, SB_SETPARTS, (WPARAM)3, (LPARAM)parts);
// 设置每一部分的文本,并尝试居中
SendMessage(hwndStatusBar, SB_SETTEXT, 0, (LPARAM)L"Part 1");
SendMessage(hwndStatusBar, SB_SETTEXT, 1, (LPARAM)L"Part 2");
SendMessage(hwndStatusBar, SB_SETTEXT, 2 | SBT_NOBORDERS, (LPARAM)L"Part 3");
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) {
case WM_CREATE:
InitializeStatusBar(hwnd);
break;
case WM_SIZE:
{
// 在窗口大小改变时调整状态栏的大小和位置
RECT rc;
GetClientRect(hwnd, &rc);
MoveWindow(hwndStatusBar, 0, rc.bottom - 20, rc.right, 20, TRUE); // 设置状态栏位置为窗口底部
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow)
{
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_BAR_CLASSES; // 状态栏类
InitCommonControlsEx(&icex);
hInst = hInstance;
WNDCLASS wc = {};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = L"StatusBar";
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
if (!RegisterClass(&wc))
{
MessageBox(NULL, L"Window registration failed!", L"Error", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
HWND hwnd = CreateWindowEx(
0, L"StatusBar", L"StatusBar Example",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
NULL, NULL, hInstance, NULL);
if (hwnd == NULL)
{
MessageBox(NULL, L"Window creation failed!", L"Error", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return static_cast<int>(msg.wParam);
}
效果图:
没有回复内容