🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

windows program, two windows (SOLVED)

Started by
2 comments, last by jnbutler 4 years, 9 months ago

How do you make it where the program itself does not exit until both windows are closed. I have a project and created two windows using two different window classes two different window handles and two different winprocs, two boolean variables hwnd0Closed and hwnd1Closed. Then inside each of the winprocs in the WM_CLOSE handlers, I have an if statement that tests the two boolean variables


hwnd0Closed = true; 
if(hwnd0Closed && hwnd1Closed) 
{ 
   PostQuitMessage(0); 
} 
DestroyWindow(hwnd); 

 This is the one for hwnd0 in WindowProc0. A similar one is in WindowProc1 but sets the hwnd1Closed  = true. 

This should not close the whole program until both the windows have been closed, but when I click on one of the windows X to close it, both of them close and the program shuts down. I have attached my full source code file.

Thanks for any help you can provide.

 

TwoWindows.cpp

jnbutler

Advertisement

I didn't walk through your code to double check but when a window is closed it posts a WM_DESTROY (you call DestroyWindow in the case WM_CLOSE). Your windows are posting a WM_QUIT in the case of WM_DESTROY (you call PostQuitMessage), so the app will close. You don't have to destroy a window when it is closed, or post quit when it is.

So, I figured by TeaTreeTim's reply that I probably should do the test to see if both windows are closed inside the WM_DESTROY case. So here is just both of my WindowProc's.


LRESULT CALLBACK WindowProc0(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
	// this is the main message handler of the system
	PAINTSTRUCT ps;		// used in WM_PAINT
	HDC	hdc;			// handle to a device context

	// what is the message
	switch(msg)
	{
	case WM_CREATE:
		{
			// do initialization stuff here
			// return success
			return(0);
		}
		break;
	case WM_PAINT:
		{
			// simply validate the window
			hdc = BeginPaint(hwnd,&ps);
			// you would do all your painting here
			EndPaint(hwnd,&ps);

			//return success
			return(0);
		}
		break;

	case WM_CLOSE:
	{
		hwnd0Closed = true;
		DestroyWindow(hwnd);
	}
	break;

	case WM_DESTROY:
		{
			// test if both windows are closed
			// if so program closes.
			if (hwnd0Closed && hwnd1Closed)
			{
				PostQuitMessage(0);
				return(0);
			}
		}
		break;
	default:break;
	} // end of switch

	// process any messages that we didn't take care of
	return(DefWindowProc(hwnd, msg, wparam, lparam));
} // end of WinProc

LRESULT CALLBACK WindowProc1(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
	// this is the main message handler of the system
	PAINTSTRUCT ps;		// used in WM_PAINT
	HDC	hdc;			// handle to a device context

	// what is the message
	switch (msg)
	{
		case WM_CREATE:
		{
			// do initialization stuff here
			// return success
			return(0);
		}
		break;
		case WM_PAINT:
		{
			// simply validate the window
			hdc = BeginPaint(hwnd, &ps);
			// you would do all your painting here
			EndPaint(hwnd, &ps);

			//return success
			return(0);
		}
		break;

		case WM_CLOSE:
		{
			hwnd1Closed = true;
			DestroyWindow(hwnd);
		}
		break;

		case WM_DESTROY:
		{
			// test if both windows are closed
			// if so program closes.
			if (hwnd0Closed && hwnd1Closed)
			{
				PostQuitMessage(0);
				return(0);
			}
		}
		break;
		default:break;
	} // end of switch

	// process any messages that we didn't take care of
	return(DefWindowProc(hwnd, msg, wparam, lparam));
} // end of WinProc

This works like I want it to now. The program does not close until both windows are closed.

 

jnbutler

This topic is closed to new replies.

Advertisement