[Win32 API] Custom quiting

Hello, I was wondering if there was a way to have a program prompt this before quiting:

1
2
if (MessageBox (hWnd, "You sure you want to quit?", "Message", MB_YESNO) == 6) return 0;
else {do what ever to keep program open}


I tried a few things with the window proc, and after the message loop, nothing worked for alt + f4 or closing the window with the X button, only with the escape key (I added a feature to close the program when I press escape).

so any help on this would be very cool :D

thanks !
One possible method - Trap the WM_CLOSE message and do it there.
1
2
3
4
5
6
           
 case WM_CLOSE:
    if (MessageBox (hWnd, "You sure you want to quit?", "Message", MB_YESNO) == IDYES) 
          return DefWindowProc (hwnd, message, wParam, lParam);
    else return 0;
    break;



One other thing
Why are you using the hard coded value 6 in your code rather than the pre- defined macro IDYES ??
Thanks it worked, and when I was reading tutorials I was told to use 6, never seen IDYES, I am guessing they do the same thing right?
Windows have a number of a predefined macros like this ( #include windows.h) especially for use with
messageboxes/dialogboxes

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
/*
 * Dialog Box Command IDs
 */
#define IDOK                1
#define IDCANCEL            2
#define IDABORT             3
#define IDRETRY             4
#define IDIGNORE            5
#define IDYES               6
#define IDNO                7
#if(WINVER >= 0x0400)
#define IDCLOSE         8
#define IDHELP          9
#endif /* WINVER >= 0x0400 */

#if(WINVER >= 0x0500)
#define IDTRYAGAIN      10
#define IDCONTINUE      11
#endif /* WINVER >= 0x0500 */

#if(WINVER >= 0x0501)
#ifndef IDTIMEOUT
#define IDTIMEOUT 32000
#endif
#endif /* WINVER >= 0x0501 */ 


It is better to use the ID than the actual value - although IDYES has the value 6, it is possible
for Microsoft to rearrange the values in the future. Ok, it is unlikely to happen, but
if they did change the values your code would be broken if you use the value 6 because that might not be IDYES anymore.
I see, I will use IDYES from now on, thanks.
Not only that, programming is all about readability. If not for that we're not too far from just coding in assembly.
closed account (3pj6b7Xj)
Well spoken Kiana! I hate it when people post the "sum += 13" sure we know what it does but that looks confusing to anyone trying to learn "sum = sum + 13" makes a lot more sense...in C++, just because you can get away with it and it works, it doesn't mean you've made it any easier for you, when you open up that program months later and nothing in it makes sense!
Last edited on
^I wouldn't call that the best example as += can be easier and may be faster then the = ... + version.
closed account (3pj6b7Xj)
Id be willing to see some tests done firedraco, not trying to be an ass or anything...it is easier because you write less but it ends in rather bizarre looking code sum += 13 llooks confusing because anyone could mistake it for sum + sum = 13; were as sum =+ could be read as sum = sum + 13; how about mov eax, sum | add eax, 13 | mov sum, eax ..... id be curious to know which is faster sum += 13 or adding 13 to the value of sum in assembler...perhaps I should run some experiments :)
I always assumed these two symbol operators were designed they way they are
based on operator precedence - the = operator has lower priority than all the other maths symbol
so the operators are written as:
+=, *=, -= and so forth.
Last edited on
@mrfaosfx: I read sum += 13 the same as sum = sum + 13. If I am reading C++ and there is one way used, and not the other, then I can still understand what it is saying, just like two works in English that mean the same thing.
Topic archived. No new replies allowed.