how to close one window.

hello everyone.
i am making a program which has multiple windows.
how do I program that when one parent window pops-up, the other parent window closes?
i am using Microsoft visual studio 2010, in a Win32 application.
i am not using a windows form.
Assuming you have a global variable holding the HWND of the current parent window, I would do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
LRESULT CALLBACK MyWindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
        case WM_CREATE:
            if (g_parentWindow)
            {
                SendMessage(g_parentWindow, WM_CLOSE, 0, NULL);
                g_parentWindow = hWnd;
            }
            break;
        ...
    }
}


That should do it.
when I used that, all windows closed and the application ended.
here is the bit of the code, hMainMenu is the HWND of the window whitch needs to be destroyed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

LRESULT CALLBACK windProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	
{

	
	switch (uMsg)
	{

		 case WM_CREATE:
            if (hMainMenu)
            {
                SendMessage(hMainMenu, WM_CLOSE, 0, NULL);
               hMainMenu = hWnd;
            }
            break;




	case WM_DESTROY:
		PostQuitMessage(WM_QUIT);
		break;

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

}

Simple fix. Add a boolean control variable that controls whether POstQuitMessage() is called. Basically, if you are closing the window because you are creating a new one, don't post the quit message.

I'm posting the code I would initially use:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//This declaration goes at the beginning of the Window Procedure:
static bool bDontQuit = false;

//Then inside the if (hMainMenu) block:
bDontQuit = true;
SendMessage(hMainMenu, WM_CLOSE, 0, NULL);
hMainMenu = hWnd;

//Then in the block for WM_DESTROY:
if (!bDontQuit)
{
    PostQuitMessage(0); //You don't pass WM_QUIT here.  You pass whatever number you want to return as exit code for the process.  Usually 0 means "normal exit".
}
bDontQuit = false;
break;


EDIT: As I read this, I realize that I should have used a variable called bYesQuit. :-P It would have been simpler to understand, hehe. Your choice.
Last edited on
I would just hide the window until we need to destroy it:

ShowWindow (handle, SW_HIDE);
when I use your code like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

LRESULT CALLBACK winProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	static bool bDontQuit = false;
	


	
	switch (uMsg)
	{
		case WM_CREATE:
            if (hMainMenu)
            {
                bDontQuit = true;
				SendMessage(hMainMenu, WM_CLOSE, 0, NULL);
				hMainMenu = hWnd;
            }
            break;



	case WM_DESTROY:
		if (!bDontQuit)
		{
		 PostQuitMessage(0); //You don't pass WM_QUIT here.  You pass whatever number you want to return as exit code for the process.  Usually 0 means "normal exit".
		}
		bDontQuit = false;
		break;

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


both windows close and the program ends
It works Ok for me with one correction:

1
2
3
4
5
6
7
8
case WM_CREATE:
    if (hMainMenu)
    {
        bDontQuit = true;
        SendMessage(hMainMenu, WM_CLOSE, 0, NULL);
    }
    hMainMenu = hWnd;
    break;


How I tested: Using VS 2008 Pro, I created a new Win32 project. I allowed the project to have the basic Win32 app skeleton VS provides. I added a New Window menu item, I separated the window creation logic from InitInstance() into a separate function and then called that function on the menu click (WM_COMMAND). The code works fine.
thank you, that code works for me either
> SendMessage(hMainMenu, WM_CLOSE, 0, NULL);

BTW, this is wrong
Last edited on
Topic archived. No new replies allowed.