how to create a window in c++
#include
#pragma warning ( disable : 28251 )
// window message procedure
LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CLOSE: PostQuitMessage(69); break;
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}
// window main
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// class name
const auto pClassName = L"test name";
// the new window class "ex"
WNDCLASSEX wc = { 0 };
wc.cbSize = sizeof(wc);
wc.style = CS_OWNDC;
wc.lpfnWndProc = WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hIcon = nullptr;
wc.hCursor = nullptr;
wc.hbrBackground = nullptr;
wc.lpszClassName = pClassName;
wc.hIconSm = nullptr;
// register window class
RegisterClassEx(&wc);
// create window instance (window heandeler)
HWND hwnd = CreateWindowEx(
0, pClassName,
L"Test Window",
WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,
200, 200, 640, 480,
nullptr, nullptr, hInstance, nullptr
);
// show the window
ShowWindow(hwnd, SW_SHOW);
// message pump
MSG msg;
BOOL gResult;
while ((gResult = GetMessage(&msg, nullptr, 0, 0)) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (gResult == -1) {
return -1;
}
else {
return msg.wParam;
}
return 0;
}
|