Jump to content
  • Advertisement
Sign in to follow this  
ukdeveloper

Win32 window won't appear on screen

This topic is 4835 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

Hi, I've decided to move away from games and try something a little different. I've managed to create a Win32 window before, but this time it won't work. I'm using VC++ 2005 Express Beta 2 with the Platform SDK installed and working. Here's my code:
#include <iostream>
#include <windows.h>


const char *WindowClassName="IMSVR";

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	switch(Msg)
	{
		case WM_DESTROY:
			PostQuitMessage(WM_QUIT);
			break;

		default:
			return DefWindowProc(hWnd,Msg,wParam,lParam);
	}

	return 0;
}



int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{

	HWND MainWindow;

	MSG MsgPump;

	WNDCLASSEX WndClassEx;

	WndClassEx.cbSize=sizeof(WNDCLASSEX);
	WndClassEx.style=CS_HREDRAW|CS_VREDRAW;
	WndClassEx.lpfnWndProc=WndProcedure;
	WndClassEx.cbClsExtra=0;
	WndClassEx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
	WndClassEx.hCursor=LoadCursor(NULL,IDC_HAND);
	WndClassEx.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
	WndClassEx.lpszMenuName=NULL;
	WndClassEx.lpszClassName=WindowClassName;
	WndClassEx.hInstance=hInstance;

	RegisterClassEx(&WndClassEx);

	MainWindow=CreateWindowEx(0,WindowClassName,"IM Server",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,
		CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);

	ShowWindow(MainWindow,SW_SHOWNORMAL);

	UpdateWindow(MainWindow);

	while(GetMessage(&MsgPump,MainWindow,0,0))
	{
		TranslateMessage(&MsgPump);
		DispatchMessage(&MsgPump);
	}

	return MsgPump.wParam;
		
}

It compiles with one warning (warning C4244: 'return' : conversion from 'WPARAM' to 'int', possible loss of data), and runs. It is definitely running, because the app appears in the Task Manager and VC++ still has the Stop button enabled and the Play button greyed out. But the window won't appear on screen, what am I missing? Thanks, ukdeveloper.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
You're get message isn't in a larger loop. It's translating any messages and bailing winmain immediately without ending the process (posting quit message).

Share this post


Link to post
Share on other sites
OK, how do I fix that? What do I need to do?

EDIT: I've just discovered that the program works fine in Release mode but not in Debug.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

while( true ) {
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE )) {
if( msg.message == WM_QUIT )
break;
TranslateMessage( &msg );
DispathMessage( &msg );

}
GameMain( );
}

GameCleanUp( );
return msg.wParam;

Share this post


Link to post
Share on other sites
The AP's code is not correct for your program. The problem doesnt have anything to do with your message loop, the problem is the RegisterClassEx() is failing. The reason for the failure is that you haven't zeroed all the members of the WNDCLASSEX struct leaving garbage values in some of the members. When RegisterClassEx() is called using this WNDCLASSEX it sees these garbage values as invalid and bails.

The solution is simple, change this


WNDCLASSEX WndClassEx;


to this


WNDCLASSEX WndClassEx;
ZeroMemory(&WndClassEx, sizeof(WndClassEx));


ZeroMemory() sets all the members of WndClassEx to 0. These are all valid values.

Also, I saw an unrelated problem in your WndProcedure() that will bite you after you fix this problem. You should also be handling WM_CLOSE and you are using PostQuitMessage() incorrectly. When quiting normally 0 is passed to PostQuitMessage().

Your new WndProcedure() will look like this:

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_CLOSE:
DestroyWindow(hWnd);

case WM_DESTROY:
PostQuitMessage(0);
break;

default:
return DefWindowProc(hWnd,Msg,wParam,lParam);
}

return 0;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!