#include <Windows.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); void CreateMyWindow(int height, int width, LPCWSTR windowName, LPCWSTR windowType, COLORREF backgroundColor);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { CreateMyWindow(600, 800, L"My Window", L"MyWindowClass", RGB(255, 0, 0));

MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

return 0;

}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_DESTROY: PostQuitMessage(0); return 0; }

return DefWindowProc(hwnd, uMsg, wParam, lParam);

}

void CreateMyWindow(int height, int width, LPCWSTR windowName, LPCWSTR windowType, COLORREF backgroundColor) { WNDCLASSEX wc = {}; wc.cbSize = sizeof(wc); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WindowProc; wc.hInstance = GetModuleHandle(NULL); wc.hbrBackground = CreateSolidBrush(backgroundColor); wc.lpszClassName = windowType;

RegisterClassEx(&wc);

HWND hwnd = CreateWindowEx(0, windowType, windowName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, width, height, NULL, NULL, GetModuleHandle(NULL), NULL);

ShowWindow(hwnd, SW_SHOWDEFAULT);
UpdateWindow(hwnd);

}

// 使用示例 int main() { int height = 600; int width = 800; LPCWSTR windowName = L"My Window"; LPCWSTR windowType = L"MyWindowClass"; COLORREF backgroundColor = RGB(255, 0, 0);

CreateMyWindow(height, width, windowName, windowType, backgroundColor);

return 0;

}

C++ Windows.h 创建窗口并设置背景颜色函数:background()

原文地址: https://www.cveoy.top/t/topic/pT8N 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录