user98188
user98188

Reputation:

How to receive feedback from a Windows MessageBox?

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")?

Upvotes: 2

Views: 5683

Answers (2)

Ravi Sharma
Ravi Sharma

Reputation: 11

int result = MessageBox(hWnd,_T(""),_T("Save work?"), MB_YESNOCANCEL);
if (result == 6){
    MessageBox(NULL, _T("YES"),_T("Press"),MB_OK);
}
else if (result == 7){
    MessageBox(NULL, _T("NO"),_T("Press"),MB_OK);
}
else{
    MessageBox(NULL, _T("CANCEL"),_T("Press"),MB_OK);
}

Upvotes: 1

GManNickG
GManNickG

Reputation: 504303

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
{
    // ...
}

Upvotes: 11

Related Questions