Message Boxes

Message Box

 


 

1. Introduction

A Message Box is a relatively small dialog box that contains a message or a question intended for the user of your application. Since the message box is a modal dialog box, the user has to respond before continuing with the next operation. At a minimum, the message box gives some information to the user who clicks OK to dismiss the dialog box. A message box can also contain more than one button and you can customize it using its many features.

To create and/or implement a message box, you have a lot of choices, from the CWindow::MessageBox() to the SDK's MessageBox() functions, etc. These functions have different requirements.

The MessageBox( LPCTSTR lpszText, LPCTSTR lpszCaption = NULL, UINT nType = MB_OK ) function , which is the simplest, uses three arguments, two of which are not required because their default values have been set. This function returns an integer that represents the button clicked by the user.

The Win32's AfxMessageBox(LPCTSTR lpszText, UINT nType = MB_OK, UINT nIDHelp = 0) function is as flexible as the former implementation except that this one supports context sensitive help. If no help is defined, the message box still works because help is only an option. This function also returns an integer variable in response to the clicked button.

The third implementation of the Message Box is the MessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType) function. This time, all arguments are required. This function also returns an integer identifying the button that the user clicked.

Instead of going through all the details of all their arguments, I will just show some of their implementation. These functions are widely documented on the MSDN library (available on the CD-ROM, DVD, or msdn.microsoft.com web site); just keep in mind that the lpszText is always required, this is the message that the user will read and respond to. The lpszCaption is the title that will display on top box of the message box; if you don't provide a caption, the application will use the title of the application. nType allows you to specify the objects or items you want on the message box, items such as the kinds of buttons (if using more than one) and the icon identifier; this section also allows you to specify which button will be the default.

 

 

2. Message Boxes Implementations:

  1. Start Microsoft Visual C++ and open the MsgBoxes project.
  2. Click the ResourceView tab. Expand the MsgBoxes resources, if necessary. Expand the Dialog folder. Double-click IDD_MSGBOX_SIMPLE.
  3. Drop a Button on the dialog. Change the identifier of the button to IDC_SIMPLEST_BTN and its caption to &Simples Message Box.
  4. Double-click the new button. That create a function for the BN_CLICKED message with a suggested name. Accept the name of the function and implement it as follows:
    void CMsgBoxSimple::OnSimplestBtn() 
    {
    	MessageBox("This is the simplest message box");	
    }
  5. Add another button on the IDD_MSGBOX_SIMPLE page identified as IDC_SIMPLE_BTN with  a caption of A Simple &Message Box. Double-click the new button, accept the suggested name of the function and implement it as follows:
    void CMsgBoxSimple::OnSimpleBtn() 
    {
    	AfxMessageBox("Another simple message box");	
    }
  6. In the ResourceView, double the IDD_MSGBOX_OPTIONS page to open it. Add the following buttons to the property page:
      
    Identifier Caption
    IDC_OK_BTN OK
    IDC_OKCANCEL_BTN OK or Cancel
    IDC_YESNO_BTN Yes or No
    IDC_YESNOCANCEL_BTN Yes, No, or Cancel
    IDC_RETRYCANCEL_BTN Retry or Cancel
    IDC_ABORTRETRYIGNORE_BTN Abort, Retry, or Cancel

  7. Add an edit box at the bottom of the page with IDC_MESSAGE as the identifier.
  8. Access the ClassWizard property sheet. In MFC ClassWizard, click Member Variables, double-click IDC_MESSAGE, and add a CString variable named m_Message.
  9. Still in MFC AppWizard, click the Message Maps property page, and make sure that MsgBoxes is selected in the Project combo box. In the Class Name combo box, select the CMsgBoxOptions class, if necessary. In the Object IDs list box, click IDC_OK_BTN, in the Messages list box, double-click BN_CLICKED, accept the suggested name for the function.
  10. Add a function for the following buttons: IDC_OKCANCEL_BTN, IDC_YESNO_BTN, IDC_YESNOCANCEL_BTN, IDC_RETRYCANCEL, IDC_ABORTRETRYCANCEL_BTN.
  11. Implement the functions as follows:
    void CMsgBoxOptions::OnOkBtn() 
    {
        MessageBox("Message with only one button, OK?",
          "Exploring Message Boxes",
          MB_OK|MB_ICONINFORMATION);
    
        m_Message = "You didn't have any choice";
        UpdateData(FALSE);
    }
    
    void CMsgBoxOptions::OnOkcancelBtn() 
    {
        int Response;
    
        Response =
        MessageBox("In this message, you can accept by clicking OK"
          "or dismiss with Cancel"
          "\n\nYou can write a long message on multiple lines"
          "\non any message box",
          "Exploring Message Boxes",
          MB_OKCANCEL|MB_ICONASTERISK);
    
        if( Response == IDOK )
        {
          m_Message = "So you accept the mission.";
          UpdateData(FALSE);
        }
        else
        {
          m_Message = "You didn't even try.";
          UpdateData(FALSE);
        }
    }
    
    void CMsgBoxOptions::OnYesnoBtn() 
    {
      int Response;
      
      Response =
      MessageBox("Did you eat already, I mean since yesterday?",
        "Amazing Stomach To Feed", MB_YESNO|MB_ICONQUESTION);
        
      if( Response == IDYES )
      {
        m_Message = "Then, I don't have to give you anything.";
        UpdateData(FALSE);
      }
      else
      {
        m_Message = "Well, sorry,
          I ain't got no food for your stomach!.";
        UpdateData(FALSE);
      }
    }
    
    void CMsgBoxOptions::OnYesNoCancelBtn() 
    {
        int Response;
        
        Response =
        MessageBox("This one would come up when you delete a folder"
          "\nor perform an action that needs"
          "confirmation of the action.",
          "Folder Or File Deletion Message",
          MB_YESNOCANCEL|MB_ICONWARNING);
          
        if( Response == IDYES )
        {
          m_Message = "Now the action will proceed.";
          UpdateData(FALSE);
        }
        else if( Response == IDNO )
        {
          m_Message = "Mission dismissed.";
          UpdateData(FALSE);
        }
        else
        {
          m_Message = "Abort Action. See you next time";
          UpdateData(FALSE);
          }
    }
    
    void CMsgBoxOptions::OnRetryCancelBtn() 
    {
        int Response;
        
        Response =
        MessageBox("I know you can, I know you can, I know you can."
          "\nJust keep trying",
          "Trying Various Alternatives",
          MB_RETRYCANCEL|MB_ICONWARNING);
    
        if( Response == IDRETRY )
        {
          m_Message = "Yes, keep trying, until...";
          UpdateData(FALSE);
        }
        else
        {
          m_Message = "Excuse me? Finish what you have started.";
          UpdateData(FALSE);
        }
    }
    
    void CMsgBoxOptions::OnAbortRetryIgnoreBtn() 
    {
        int Response;
    
        Response =
        MessageBox("This is the nastiest message box,
          "it usually means trouble.",
          "Trouble On Horizon",
          MB_ABORTRETRYIGNORE|MB_ICONSTOP);
    
        if( Response == IDYES )
        {
          m_Message = "Thank God. You got me worried!";
          UpdateData(FALSE);
        }
        else if( Response == IDNO )
        {
          m_Message = "You are stubborn, aren't you?";
          UpdateData(FALSE);
        }
        else
        {
          m_Message = "Right, right, right. Change your mind.";
          UpdateData(FALSE);
        }
    }
  12. By default, the first button on a message box is the default button. Fortunately, using the SDK's MessageBox function, you can change the default button as MB_DEFBUTTON1 (the default), MB_DEFBUTTON2, MB_DEFBUTTON3, or MB_DEFBUTTON4.
    In the ResourceView, double-click the IDD_MSGBOX_CUSTOM. Add a button identified as IDC_SECONDDEFAULT_BTN and with the caption Second Button Default.
  13. Add a second button identified as IDC_THIRDDEFAULT_BTN and with a caption of Third Button Default.
  14. Double-click the IDC_SECONDDEFAULT_BTN button, accept the suggested name of the function and implement it as follows:
    void CMsgBoxCustom::OnSeconddefaultBtn() 
    {
        int BtnClicked;
        
        BtnClicked = 
          MessageBox("If you press Enter, the default button will be"
            "accessed.\nOtherwise, click one of the buttons.",
            "Changing Default Button",
            MB_YESNOCANCEL|MB_ICONINFORMATION|MB_DEFBUTTON2);
    
        if( BtnClicked == IDYES )
          MessageBox("You clicked the first button: YES");
        else if( BtnClicked == IDNO )
          MessageBox("You clicked No or
            you accepted the default button.");
        else
          MessageBox("You clicked the third button");
    }
  15. Implement the function of the other button as follows:
    void CMsgBoxCustom::OnThirddefaultBtn() 
    {
      int BtnClicked;
      
      BtnClicked =
        MessageBox("If you press Enter, the default button will be"
          "accessed.\nOtherwise, click one of the buttons.",
          "Changing Default Button",
          MB_ABORTRETRYIGNORE|MB_ICONSTOP|MB_DEFBUTTON3);
          
        if( BtnClicked == IDABORT )
          MessageBox("You clicked the first button: ABORT");
        else if( BtnClicked == IDRETRY )
          MessageBox("You clicked the second button: RETRY");
        else
          MessageBox("You clicked Ignore or
            you accepted the default button.");	
    }

 
Source

Copyright © 2000-2009, FunctionX, Inc.