MFC OK/Cancel Dialog Button Override?

14,347

Solution 1

To wire up your dialog to terminate and return IDSAVEONE, you need to add a click handler to the Save One button and have it call EndDialog:

void CSAPrefsDialog::OnBnClickedSaveone()
{
    EndDialog(IDSAVEONE);
}

If you add the click handler through the dialog editor (e.g. by double-clicking on your button) then the necessary framework code will be generated for you to wire this up; otherwise you'll need to add the following line into your BEGIN_MESSAGE_MAP section in your dialog class:

ON_BN_CLICKED(IDSAVEONE, &CSAPrefsDialog::OnBnClickedSaveone)

but (as AJG85's just beaten me to posting) depending on what the operation is, how fast it is and whether you want to report errors in the preferences dialog or not, you may want to just carry out the extra function in your on-clicked handler instead.

Solution 2

MFC has built in ids for the ok and cancel buttons. Those being IDOK and IDCANCEL. You can either handle these in a switch via the return of DoModal() or probably better would be to override OnOK() and OnCancel() methods in your dialog class to do what you want.

You can do this by adding a line to the message map to call your handler:

Edit: The same thing works for buttons you add to the dialog which I added to my example code below:

BEGIN_MESSAGE_MAP(MyDialog, CDialog)
    ON_BN_CLICKED(IDOK, &OnBnClickedOk)
    ON_BN_CLICKED(IDSAVEONE, &OnBnClickedSave)
END_MESSAGE_MAP()

void MyDialog::OnBnClickedOk()
{
   // do extra stuff when they click OK

   CDialog::OnOK(); // call base class version to complete normal behavior
}

void MyDialog::OnBnClickedSave()
{
   // this would be called for your save button with custom id IDSAVEONE

   // note: no base class call here as it's specific to your dialog
}
Share:
14,347
Jon
Author by

Jon

Programmer. Designer. Photo-/Videographer. Musician.

Updated on June 11, 2022

Comments

  • Jon
    Jon almost 2 years

    Language: C++

    Development Environment: Microsoft Visual C++

    Libraries Used: MFC

    Pretty new to MFC, so bear with me. I have a dialog that is launched via DoModal(). I'm attempting to add buttons to this dialog that will replace the default "OK" and "Cancel" buttons. Right now, I can't quite figure out how to do this. I deleted the OK and Cancel buttons and added new ones with new IDs, added event handlers, and just some simple code for them to execute upon being pressed, but I couldn't get it to work.

    I suspect it has something to do with the fact that DoModal() expects responses from OK or Cancel, but nothing else. I'm not really sure though. Any help would be greatly appreciated!

    EDIT: Stripped down code added for reference.

    void CPrefsDlg::Launch() {
    
    [ ... ]
    
      CSAPrefsDialog dlg;
    
      INT_PTR nRet = -1;
      nRet = dlg.DoModal();
    
      // Handle the return value from DoModal
      switch ( nRet )
      {
      case -1: 
         AfxMessageBox("Dialog box could not be created!");
         break;
      case IDABORT:
         // Do something
         break;
      case IDOK: // This works just fine.
         exit(0);
         break;
      case IDSAVEONE: // This does not work.
         MessageBox("Save One");
         break;
      default:
         break;
      };
    }
    
    void CPrefsDlg::SaveOne()
    {
    // I tried adding in my own handler for 'Save One'...this does not work.
    MessageBox("Save one");
    }