#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// Create a window
HWND hwnd;
{
WNDCLASSEXW winClass = {};
winClass.cbSize = sizeof(WNDCLASSEXW);
winClass.style = CS_HREDRAW | CS_VREDRAW;
winClass.lpfnWndProc = &WndProc;
winClass.hInstance = hInstance;
winClass.hCursor = LoadCursorW(0, IDC_ARROW);
winClass.lpszClassName = L"WindowClassName";
if (!RegisterClassEx(&winClass)) {
MessageBoxA(0, "RegisterClassEx failed", "Fatal Error", MB_OK);
return GetLastError();
}
RECT rect = { 0, 0, 1024, 768 };
AdjustWindowRectEx(&rect, WS_OVERLAPPEDWINDOW, FALSE, WS_EX_OVERLAPPEDWINDOW);
LONG width = rect.right - rect.left;
LONG height = rect.bottom - rect.top;
hwnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
winClass.lpszClassName,
L"A Windows",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT,
width,
height,
0, 0, hInstance, 0);
if (!hwnd) {
MessageBoxA(0, "CreateWindowEx failed", "Fatal Error", MB_OK);
return GetLastError();
}
}
if (hwnd != GetDesktopWindow()) {
MessageBoxA(0, "HWND is not a handle to the Desktop Windows", "Info", MB_OK);
}
else {
MessageBoxA(0, "HWND is a handle to the Desktop Windows", "Info", MB_OK);
}
bool running = true;
while (running) {
MSG msg = {};
while (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT) {
running = false;
}
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
LRESULT result = 0;
switch (msg)
{
case WM_KEYDOWN:
{
if (wparam == VK_ESCAPE) {
DestroyWindow(hwnd);
}
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
result = DefWindowProcW(hwnd, msg, wparam, lparam);
}
return result;
}
/*
run:
HWND is not a handle to the Desktop Windows
*/