So to make the title true, this is the code:
#include <Windows.h> LRESULT WindowProc(HWND hWindow, UINT uMessage, WPARAM wParam, LPARAM lParam){ switch (uMessage) { case WM_CLOSE: PostQuitMessage(0); return 0; default: return DefWindowProcW(hWindow, uMessage, wParam, lParam); } } int _stdcall wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nShowCmd){ LPCWSTR szClassName = L"SampleWindowClass"; WNDCLASSEXW windowClass = { 0 }; windowClass.cbSize = sizeof(WNDCLASSEX); windowClass.lpszClassName = szClassName; windowClass.hInstance = hInstance; windowClass.lpfnWndProc = WNDPROC(WindowProc); windowClass.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW; if(!RegisterClassExW(&windowClass)) return -1; HWND hWindow = CreateWindowExW(WS_EX_OVERLAPPEDWINDOW, szClassName, L"Window Sample", WS_OVERLAPPED | WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU, 0, 0, 640, 480, 0, 0, hInstance, 0); if(hWindow == 0 || hWindow == INVALID_HANDLE_VALUE) return -1; ShowWindow(hWindow, nShowCmd); MSG msg = { 0 }; do{ if(PeekMessageW(&msg, 0, 0, 0, PM_REMOVE)){ TranslateMessage(&msg); DispatchMessageW(&msg); } } while(msg.message != WM_QUIT); DestroyWindow(hWindow); UnregisterClassW(szClassName, hInstance); return msg.wParam; }
It’s very simple, and straight to the point. If you create a Win32 Project in Visual Studio (an empty one), add an main.cpp source file and then paste this code into it – it should compile without problems. The program runs with a very basic window, with no background colour and it will terminate the application when you try to close it.
A neat trick to initialize a class or structure is to use the table initialization with single zero:
WNDCLASSEX windowClass = { 0 };This will assign the single value to each field of the class (or structure). Unfortunately this method is limited only to field types that can have zeros assigned to – that leaves out any type with strong typing (like C++0×11 enum class type) and have to by initialized properly.
In future posts this code will be the base for samples that initialize graphic API’s like OpenGL, DirectX 9, DirectX 11, etc.