Can I return a custom value from a dialog box's DoModal function?

前端 未结 3 1634
灰色年华
灰色年华 2020-12-21 15:47

What I wish to do is, after creating a dialog box with DoModal() and pressing OK in the box to exit it, to have a custom value returned. For example, a couple o

3条回答
  •  执笔经年
    2020-12-21 16:06

    I was looking for an answer and agree that in most cases that you would not change the standard behavior of a dialog. But there might be a case where you would like to pick what the user is actually responding say if you had several buttons and want specifically that they picked the OK at the top versus the OK at the bottom. You know for metrics.

    Or say if you wanted to have slightly different results if the dialog caused an error when running on of your functions. It would be nice to return a value that is not IDOK but maybe some other value.

    I found Dialog::EndDialog() with details and an example of usage here: MSDN: Dialog::EndDialog

    #include "ANewDialog.h"
    void CMyWnd::ShowDialog()
    {
       CMyDialog myDlg;
       int nRet = myDlg.DoModal();
    
       if ( nRet == 18  )
          AfxMessageBox("Dialog closed. But there was a problem.");
    }
    
    /* MyDialog.cpp */
    void CMyDialog::OnSomeButtonAction()
    {
       int nRet = 0;
    
       // Run your function with return value;
       nRet = YourReallyFunFunction();
       EndDialog(nRet); // Set the return value returned by DoModal!
    
       return; // The dialog closes and DoModal returns here!
    }
    

提交回复
热议问题