bob
bob

Reputation: 2734

How to handle closing MessageBox

my environment is C++, MFC, compact-framework for WM 6.0+ devices.

In many places, I am showing pop-up messages using 'MessageBox()' to give a simple warning or get Yes/No repsonse from user. What I want to do is that whenever any message is closed, call some common function before I perform specific codes.

I tried WM_SHOWWINDOW in parent window but it doesn't seem to occur.

Any suggestion will be appreciated.

[Added] my screen has many buttons and I have to make sure only one button is focused all the time. When I show message box, button seems to loose its focus so I want to focus it back when message is closed. Of course, I can do it in every place where message is used but looking for a better way to handle this situation.

Upvotes: 0

Views: 2165

Answers (2)

Barış Uşaklı
Barış Uşaklı

Reputation: 13510

The MessageBox function returns specific return codes when it is closed, you can wrap the MessageBox function and check the return values and run some code based on that.

Here are the return codes from MSDN :

IDABORT    3    The Abort button was selected.
IDCANCEL    2    The Cancel button was selected.
IDCONTINUE    11    The Continue button was selected.
IDIGNORE    5    The Ignore button was selected.
IDNO    7    The No button was selected.
IDOK    1    The OK button was selected.
IDRETRY    4    The Retry button was selected.
IDTRYAGAIN    10    The Try Again button was selected.
IDYES    6    The Yes button was selected.

So the following code can be used to run different functions based on the return code.

void MyMessageBox(wstring title,wstring message)
{
  int msgboxID = MessageBox(
       NULL,
       (LPCWSTR)message.c_str(),
       (LPCWSTR)title.c_str(),
       MB_ICONWARNING | MB_CANCELTRYCONTINUE | MB_DEFBUTTON2
   );

 switch (msgboxID)
 {
    case IDCANCEL:
        // TODO: add code
        break;
    case IDTRYAGAIN:
        // TODO: add code
        break;
    case IDCONTINUE:
        // TODO: add code
        break;
    //so on
 }
}

More info here : http://msdn.microsoft.com/en-us/library/windows/desktop/ms645505(v=vs.85).aspx

Upvotes: 2

Jollymorphic
Jollymorphic

Reputation: 3530

You might try intercepting the WM_ACTIVATE message in the parent window.

Upvotes: 0

Related Questions