CreateWindowW 快速创建多个窗口
时间: 2024-01-20 20:03:04 浏览: 116
可以使用循环语句结合 CreateWindowW 函数来快速创建多个窗口。例如,以下代码可以创建 10 个相同样式的窗口:
```c++
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSW wc = {0};
wc.lpfnWndProc = DefWindowProcW;
wc.hInstance = hInstance;
wc.hCursor = LoadCursorW(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = L"MyWindowClass";
if (!RegisterClassW(&wc))
return -1;
for (int i = 0; i < 10; i++) {
HWND hwnd = CreateWindowW(L"MyWindowClass", L"Window Title", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (!hwnd)
return -1;
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
}
MSG msg = {0};
while (GetMessageW(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return (int)msg.wParam;
}
```
此代码将创建 10 个具有相同样式的窗口,并在循环中处理每个窗口的消息。
阅读全文