Windows

#include 

LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wPrama, LPARAM lParam );

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow ) {
	
	WNDCLASS wc;
	HWND hWnd;
	MSG msg;
	int nRet;

	if( !hPrevInstance ) {
		wc.hInstance = hInstance;
		wc.lpfnWndProc = WndProc;
		wc.lpszClassName = "Generic";

		wc.cbClsExtra = 0;
		wc.cbWndExtra = 0;
		wc.hbrBackground = (HBRUSH) ( COLOR_WINDOW + 1 );
		wc.hCursor = LoadCursor( NULL, IDC_CROSS );
		wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
		wc.lpszMenuName = NULL;
		wc.style = 0;

		RegisterClass( &wc );
	}

	hWnd = CreateWindow( "Generic", "Window Name", WS_OVERLAPPEDWINDOW,  
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
		(HWND) NULL, 
		(HMENU) NULL, 
		hInstance, 
		(LPVOID) NULL );

	ShowWindow( hWnd, nCmdShow );
	UpdateWindow( hWnd );
	
	while( ( nRet = GetMessage( &msg, NULL, 0, 0 ) ) != 0 ) {
		TranslateMessage(&msg); 
		DispatchMessage(&msg);
	}


	// if this point is reached, a WM_QUIT message was received (normally as a result of a PostQuitMessage() call in the WindowProc.)
	// WinMain then returns the wParam of the WM_QUIT message
	return msg.wParam;

}


LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) {
	switch( msg ) {
	case WM_DESTROY:
		PostQuitMessage( 0 );
		return 0;
	default:
		return( DefWindowProc( hWnd, msg, wParam, lParam ) );
	}
}

Notes:

  • GetMessage() returns 0 if the message is WM_QUIT
  • One way for a WM_QUIT message to be received is when the WindowProc calls PostQuitMessage(). Which it is programmed to do when it receives a WM_DESTROY message (which is sent by the OS when the user closes the window for example).
  • WinMain should return the wParam of the WM_QUIT message