Sign in to follow this  

win32: need help with blank window

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

I've made windows all the time, but today I wrote one, execute it, and it flashes and quits. What am I doing wrong in this code?

// PREPROCESSOR DIRECTIVES
#include <windows.h>
#define WIN32_LEAN_AND_MEAN

// GLOBALS
const char g_szClassName[] = "myClassWindow";

// PROTOTYPES
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);

// WINMAIN
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASSEX wc;
	HWND hWnd;
	MSG Msg;

	// Registering Class Window
	wc.cbClsExtra		= 0;
	wc.cbSize			= sizeof(WNDCLASSEX);
	wc.cbWndExtra		= 0;
	wc.hbrBackground	= (HBRUSH) (COLOR_WINDOW+1);
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);
	wc.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
	wc.hIconSm			= LoadIcon(NULL, IDI_APPLICATION);
	wc.hInstance		= hInstance;
	wc.lpfnWndProc		= WndProc;
	wc.lpszClassName	= g_szClassName;
	wc.lpszMenuName		= NULL;
	wc.style			= 0;

	if (!RegisterClassEx(&wc))
	{
		MessageBox(NULL, "Registering Class Window Failed!", "Error...", MB_OK | MB_ICONEXCLAMATION);
		return 0;
	}

	// Creating Parent Window
	hWnd = CreateWindowEx(NULL, g_szClassName, "Win32 API Programming", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
		CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);

	if (hWnd == NULL)
	{
		MessageBox(NULL, "Creating Window Failed!", "Error...", MB_OK | MB_ICONEXCLAMATION);
		return 0;
	}

	// Show & Update Window
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	// Message Loop
	if (GetMessage(&Msg, NULL, 0, 0) > 0)
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}

	return Msg.wParam;
}


// WINDOW PROCEDURE
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	switch (Msg)
	{
		case WM_CLOSE:    DestroyWindow(hWnd);
						  break;
		case WM_DESTROY:  PostQuitMessage(WM_QUIT);
						  break;
		default:		  return DefWindowProc(hWnd, Msg, wParam, lParam);
	}

	return 0;
}

Share this post


Link to post
Share on other sites
you return instantly from main and once WinMain exits your window dies. to get the window up longer you have to put in some kind of infinite loop that waits for a quit command or something. it's similar to the "why does my console app quit immediately when i run it". the solution there is to throw a cin; at the end of main, so you just need a similar solution but within the windows API. you could just wait for a keypress or something

-me

Share this post


Link to post
Share on other sites
Quote:
Original post by Palidine
you return instantly from main and once WinMain exits your window dies. to get the window up longer you have to put in some kind of infinite loop that waits for a quit command or something. it's similar to the "why does my console app quit immediately when i run it". the solution there is to throw a cin; at the end of main, so you just need a similar solution but within the windows API. you could just wait for a keypress or something

-me


umm, yeah. only in a more Win32 type of way. try replacing your message check with an actual message loop. as in:


while(GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}


Share this post


Link to post
Share on other sites
I retyped the same message loop and this time it worked. That was weird. As long as it's working now...

EDIT: heheh I had the message loop set as a 'if' condition besides it being a 'while' loop.

Thanks for the help.


while (GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}

return Msg.wParam;

Share this post


Link to post
Share on other sites
another way is to do it like this:
	while( 1 ) {
if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) {
if ( msg.message == WM_QUIT )
break;

TranslateMessage( &msg );
DispatchMessage( &msg );
}

cheers ;)

Share this post


Link to post
Share on other sites

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