Sign in to follow this  

CreateWindowEx() Problem

This topic is 4309 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

CreateWindowEx() returns NULL but when I query GetLastError( ) I get "The operation completed successfully". What could be wrong?
bool CWin32Window::Create( LPSTR lpTitle )
{
	WNDCLASSEX	wc;

	ZeroMemory( &wc, sizeof( wc ) );

	wc.cbSize = sizeof( wc );
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;

	wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
	wc.hIconSm = LoadIcon( NULL, IDI_APPLICATION );
	wc.hCursor = LoadCursor( NULL, IDC_ARROW );
	wc.hbrBackground = (HBRUSH) GetStockObject( WHITE_BRUSH );

	wc.hInstance = GetModuleHandle( NULL );
	wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	wc.lpfnWndProc = MessageRouter;
	wc.lpszMenuName = NULL;
	wc.lpszClassName = "test";

	if( !RegisterClassEx( &wc ) )
	{
		ShowMessage( "RegisterClassEx() Failed!" );
		return false;
	}

	this->m_hWnd = CreateWindowEx( WS_EX_APPWINDOW | WS_EX_WINDOWEDGE,
								   "test",
								   lpTitle,
								   WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
								   CW_USEDEFAULT,
								   CW_USEDEFAULT,
								   800,
								   600,
								   NULL,
								   NULL,
								   GetModuleHandle( NULL ),
								   this );

	if( this->m_hWnd == NULL )
	{
		ShowMessage( "CreateWindowEx() Failed!" );
		ShowLastError( );
		return false;
	}

	return true;
}

Share this post


Link to post
Share on other sites
Could the problem be with your MessageRouter function? If you're passing 'this' as the last param in CreateWindowEx, wouldnt you have to have a wc.cbWndExtra !=0 ? I dont have a compiler handy, but it might worth a try to look at these.

Share this post


Link to post
Share on other sites
Successful completion of CreateWindowEx is partially dependent on your message handler, particularly if you handle pre-creation messages like WM_NCCALCSIZE. Show us your MessageRouter (and is it a static member function?)

Also, you should cast the return value of GetModuleHandle to HINSTANCE. It may or may not make a difference: C++ does not automatically convert pointer types.

Share this post


Link to post
Share on other sites
Ok, the MSDN entry says I have to return true from WM_NCCREATE which I have done (although last time it worked without me having to do this). The windows appears now but seems to freeze, no rollover effects on caption buttons etc.

Here are the wndproc and messagepump functions:

int CWin32Window::MessagePump( )
{
MSG msg;

ZeroMemory( &msg, sizeof( msg ) );

while( this->m_bRun == true )
{
if( PeekMessage( &msg, this->m_hWnd, 0, 0, PM_REMOVE ) )
{
switch( msg.message )
{
case WM_QUIT:
this->m_bRun = false;
break;

default:
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
else
{
// TODO: Add Engine->Update();
}
}

return 0;
}


LRESULT CALLBACK CWin32Window::MessageRouter( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
CWin32Window* pWindow;

if( uMsg == WM_NCCREATE )
{
pWindow = (CWin32Window*)((LPCREATESTRUCT)lParam)->lpCreateParams;
SetWindowLongPtrA( hWnd, GWL_USERDATA, (LONG_PTR) pWindow );
return TRUE;
}
else
{
pWindow = (CWin32Window*) GetWindowLongPtrA( hWnd, GWL_USERDATA );
if( !pWindow )
{
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
}

pWindow->m_hWnd = hWnd;
return pWindow->WindowProcedure( uMsg, wParam, lParam );
}

LRESULT CWin32Window::WindowProcedure( UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch( uMsg )
{
case WM_CLOSE:
PostQuitMessage( 0 );
break;

case WM_KEYDOWN:
break;

default:
DefWindowProc( this->m_hWnd, uMsg, wParam, lParam );
}

return 0;
}

Share this post


Link to post
Share on other sites
You need to return the return value of DefWindowProc in your MessageRouter function.

Right now the handler calls DefWindowProc and then falls through to return 0.


Other nitpicks:

Using the DefWindowProc in the default case will have problem later on if you want to handle a message AND pass the message on to the DefWindowProc.

Your message pump used the HWND. You will NOT get WM_QUIT this way. WM_QUIT is sent to your thread but not to a specific HWND.

Share this post


Link to post
Share on other sites
Quote:
Original post by Smit
Doesn't "return DefWindowProc( hWnd, uMsg, wParam, lParam );" return the value of DefWindowProc?


Yes it does, but in the posted snippet above there is no return ;)

Edit: More specifically, in the function WindowProcedure there is no return at the DefWindowProc call.

Edit.2: Also, in the case of WM_NCCREATE you are NOT passing the message on to DefWindowProc which you should do.

[Edited by - Endurion on March 1, 2006 2:01:59 PM]

Share this post


Link to post
Share on other sites

This topic is 4309 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this