-
Yesno
Hi all.
First off, I'm a total newbie to windows programming, and have a few questions.
1. I have a YES/NO messagebox, and I want to know what code is required to determine which button the user pressed. I tried this in a while loop :
Code:
MessageBox(NULL, " Some random text ", " Random text", MB_YESNO | MB_ICONEXCLAMATION);
if ( IDYES )
{
cout<<"Blah Blah Blah.";
MessageBox(NULL, " Some random text ", " Random text", MB_YESNO | MB_ICONEXCLAMATION);
}
But which ever button the user pressed, the message pops up again. Is this the correct syntax ? I can post the rest of the loop if you want.
2. Forgot what 2 was, but It'll come back to me.
Please help !
I'm confused. :confused:
-
You have to save the return value of the MessageBox function.
Code:
retval = MessageBox(...);
if( retval == IDYES)
{
...
}
-
if(IDYES) is like saying if(0) or if(1) - you'll always get the same answer, and the stuff inside the if() will either always or never execute. (A good compiler, with some warnings turned on, will note this. I believe gcc does it with -Wall)
What you need to do is store the result of MessageBox() somewhere, in a variable. Then see if that variable is equal to IDYES. Example:
Code:
int mb_result = 0;
mb_result = MessageBox(HWND_DESKTOP, "Text", "Text", MB_YESNO);
if(mb_result == IDYES)
{
// User clicked Yes.
}
else
{
// User clicked no.
}
-
Or, to hide temp variables:
Code:
switch(MessageBox(...))
{
case IDYES:
{
break;
}
case IDNO:
{
break;
}
case IDCANCEL:
{
break;
}
}
-
or simply
Code:
if (MessageBox(...)==IDYES)
{
...
}
else
{
...
}