[Win32 API] X(close) button behavior.

In my program I have a menu with an exit button that will pop up a message box that has a YES and NO button controlled with an if statement. Basically a "Are you sure you want to quit" sort of dialog. This only comes up when the user goes to File > Exit because of the switch statement. What I'm trying to accomplish is getting the same dialog box to come up with the same behavior when the user presses the X button in the top right.

I suspect it's somewhere in the WM_DESTROY case but when I put the same messagebox and if statement inside the WM_DESTROY case it closes the window, and then pops up the dialog.

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
37
  LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	switch (Msg)
	{
	case WM_CREATE:
		break;

	case WM_DESTROY:
		PostQuitMessage(WM_QUIT);
		break;

	case WM_COMMAND:
	{
		switch (LOWORD(wParam))
		{
		case ID_FILE_EXIT:
			EnableWindow(hWnd, FALSE);
			if (MessageBoxA(NULL, "Are you sure you want to exit?", "Exit", MB_YESNO) == IDYES)
			{
				PostQuitMessage(WM_QUIT);
				return(0);
			}
			else
			{
				EnableWindow(hWnd, TRUE);
				SetFocus(hWnd);
			} break;
		}
	} break;

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

	return 0;
}
Last edited on
You need to process the WM_CLOSE event.

Hope this helps.
Ah yes. Forgot about WM_CLOSE. That indeed does the trick for me. Thanks you.
Topic archived. No new replies allowed.