MessageBox Exercise help

Jun 28, 2011 at 8:23pm
The exercise is to make a piece of code make a message box come up asking if the user wants to exit if escape is pressed.

Here is my code:
1
2
3
4
5
6
7
8
9
      case WM_KEYDOWN:
		if( wParam == VK_ESCAPE )
			MessageBoxA(0, "YESNO message", "Msg", MB_YESNO);
			if(IDYES)
			{
				DestroyWindow(ghMainWnd);
				return 0;
			}




No matter what i click it exits the program. I want yes to exit and no to make the message box go away and go back to the program.


Last edited on Jun 28, 2011 at 8:43pm
Jun 28, 2011 at 8:37pm
Try if(MessageBox(NULL, "Do you want to exit?", "My Program", MB_YESNO) == IDYES)

The MessageBox() function returns the button clicked, it doesn't set some global constant true/false for each button.
Last edited on Jun 28, 2011 at 8:37pm
Jun 28, 2011 at 8:40pm
okay i wrote
1
2
3
4
5
6
7
        case WM_KEYDOWN:
		if( wParam == VK_ESCAPE )
			if(MessageBoxA(0, "YESNO message", "Msg", MB_YESNO) == IDYES)
			{
				DestroyWindow(ghMainWnd);
				return 0;
			} 


and it still doesnt work. what did i do wrong?

Last edited on Jun 28, 2011 at 8:42pm
Jun 28, 2011 at 8:57pm
It works fine here; what do you mean it does not work? Clicking No prevents it from closing, and clicking Yes closes it.
Jun 28, 2011 at 9:09pm
By it does not work i mean that if i click Yes it closes and if i click No is closes. Here is my full code.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <Windows.h>

// Store handles to the main window and application
// instance globally.
HWND		ghMainWnd = 0;
HINSTANCE	ghAppInst = 0;

// Step 1: Define and implement the window procedure.
LRESULT CALLBACK
WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch( msg )
	{
	//Handle left mouse button click message.
	case WM_LBUTTONDOWN:
		MessageBoxA(0, "WM_LBUTTONDOWN message", "Msg", MB_OK);
		return 0;
	// Handle key down message.
	case WM_KEYDOWN:
		if( wParam == VK_ESCAPE )
			if(MessageBoxA(0, "Do you want to exit?", "Msg", MB_YESNO) == IDYES)
			{
				DestroyWindow(ghMainWnd);
				return 0;
			} 

	// Handle destroy window message.
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}//	end switch

	// Forward any other messages we diddn;t handle to the
	// defualt window procedure.
	return DefWindowProc(hWnd, msg, wParam, lParam);
}// end WindProc

// WinMain: Entry point for a Windows Application.
int WINAPI
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
		PSTR cmdLine, int showCmd)
{
	// Save a handle to application instance.
	ghAppInst = hInstance;

	// Step 2: Fill out a WNDCLASS instance.
	WNDCLASS wc;
	wc.style			= CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc		= WndProc;
	wc.cbClsExtra		= 0;
	wc.cbWndExtra		= 0;
	wc.hInstance		= ghAppInst;
	wc.hIcon			= ::LoadIcon(0, IDI_APPLICATION);
	wc.hCursor			= ::LoadCursor(0, IDC_ARROW);
	wc.hbrBackground	= (HBRUSH)::GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName		= 0;
	wc.lpszClassName	= L"MyWndClassName";

	// Step 3: Register the WNDCLASS instance with Windows.
	RegisterClass( &wc );

	// Step 4: Create the window, and save handle in globle
	// window handle variable ghMainWnd
	ghMainWnd = ::CreateWindowA("MyWndClassName", "MyWindow",
					WS_VSCROLL | WS_OVERLAPPEDWINDOW | WS_HSCROLL, 0, 0, 500, 500, 0, 0,
					ghAppInst, 0);

	if(ghMainWnd == 0)
	{
		::MessageBoxA(0, "CreateWindow - Failed", 0, 0);
		return false;
	}

	// Step 5: Show and update the window
	ShowWindow(ghMainWnd, showCmd);
	UpdateWindow(ghMainWnd);

	//Step 6: Enter the message loop and don't quit until
	// a WM_QUIT message is received.
	MSG msg;
	ZeroMemory(&msg, sizeof(MSG));

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

	// Return exit code back to operating system.
	return (int)msg.wParam;

}// End WinMain  
Jun 28, 2011 at 10:57pm
It closes no matter what you click because you have it written as a fall-through.

By that I mean that you need to place a break; statement in your WM_KEYDOWN case, i.e., BEFORE your WM_DESTROY case.
Jun 28, 2011 at 11:10pm
Thanks that did the trick ;)
Topic archived. No new replies allowed.