views:

90

answers:

1

I know its possible to do something like this with Windows:

MessageBox(hWnd, "Yes, No, or Cancel?", "YNCB_YESNOCANCEL);

But how do I react to what the user pressed (like closing the window if they clicked "yes")?

+7  A: 

MessageBox will return a integer referring to the button pressed. From the previous link:

Return Value
    IDABORT      Abort button was selected.
    IDCANCEL     Cancel button was selected.
    IDCONTINUE   Continue button was selected.
    IDIGNORE     Ignore button was selected.
    IDNO         No button was selected.
    IDOK         OK button was selected.
    IDRETRY      Retry button was selected.
    IDTRYAGAIN   Try Again button was selected.
    IDYES        Yes button was selected.

So something like:

int result = MessageBox(hWnd, "Save work?", MB_YESNOCANCEL);
if (result == IDOK)
{
    // ...
}
else if (result == IDNO)
{
    // ...
}
else // cancel
{
    // ...
}
GMan
MessageBox may also return 0 upon error. In that case GetLastError will return error code. I never run into error in this function. But I suspect that for example passing invalid HWND results in an error.
Adam Badura