Skip to main content
GameDev.net gamedev.net
🔒 Locked

CreateWindow fails

Started by dimovich Sep 26, 2005 at 5:21 AM 1 replies 650+ views
Original Post
dimovich
dimovich
I don't understand why i get the message box "CreateWindow!", i.e. the CreateWindow function fails...

#include <windows.h>

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


int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, char * szCmdLine, int nCmdShow ) {

	WNDCLASS wnd;
	MSG msg;
	HWND hwnd;

	wnd.style			= 0;
	wnd.cbClsExtra		= 0;
	wnd.cbWndExtra		= 0;
	wnd.hbrBackground	= (HBRUSH) GetStockObject( WHITE_BRUSH );
	wnd.hCursor			= LoadCursor( NULL, IDC_ARROW );
	wnd.hIcon			= LoadIcon( NULL, IDI_APPLICATION );
	wnd.hInstance		= hInstance;
	wnd.lpfnWndProc		= WndProc;
	wnd.lpszClassName	= "GDI";
	wnd.lpszMenuName	= NULL;
	
	if( !RegisterClass( &wnd ) ) {
		MessageBox( NULL, "RegisterClass!", "Error", 0 );
		return 0;
	}

	hwnd = CreateWindow( "GDI", "",
		WS_OVERLAPPEDWINDOW,
		100, 100, 300, 400,
		NULL, NULL, hInstance, NULL );
	
	if( NULL == hwnd ) {
		MessageBox( NULL, "CreateWindow!", "Error", 0 );
		return 0;
	}
	
	ShowWindow( hwnd, SW_SHOWNORMAL );
	UpdateWindow( hwnd );
	
	while( TRUE ) {
		while( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) ) {
			if( !GetMessage( &msg, NULL, 0, 0 ) ) {
				return 0;
			}
			TranslateMessage( &msg );
			DispatchMessage( &msg );
		}
	}
}
We're doing this for a living, George. We are not amateurs.
Brother Bob
Brother Bob
Change the message callback to this instead.
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ) {	switch( msg ) {		case WM_DESTROY:			PostQuitMessage( 0 );			return 0;	}		return DefWindowProc( hwnd, msg, wParam, lParam );}

No matter what message it receives, 0 is returned, and that is not always what you want. During window creation (inside the call to CreateWindow) the WM_NCCREATE message is sent, and the callback should return 1 on that message if it should continue to create the window. You, however, return 0 on that message, which means CreateWindow should stop and return a null handle.

If you don't handle a message, you should not only let DefWindowProc handle it, you should also let it handle the return code.

Just note the added return when handling the WM_DESTROY message. This is because you should not let DefWindowProc handle that message after you have done so.
dimovich
dimovich
Thanks a LOT!
We're doing this for a living, George. We are not amateurs.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.