我可以从对话框的 DoModal 函数返回自定义值吗?

发布于 2024-11-09 13:48:54 字数 72 浏览 4 评论 0原文

我想要做的是,在使用 DoModal() 创建对话框并按框中的“确定”退出后,返回自定义值。例如,用户将在对话框中输入几个字符串。

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 of strings the user would input in the dialog.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巴黎盛开的樱花 2024-11-16 13:48:54

您无法更改 DoModal() 函数的返回值,即使可以,我也不会推荐它。这不是惯用的做法,如果您将其返回值更改为字符串类型,您将无法查看用户何时取消对话框(在这种情况下,返回的字符串值应完全忽略)。

相反,请向对话框类添加另一个(或多个)函数,例如 GetUserName()GetUserPassword,然后在 DoModal 之后查询这些函数的值 返回IDOK

例如,显示对话框并处理用户输入的函数可能如下所示:

void CMainWindow::OnLogin()
{
    // Construct the dialog box passing the ID of the dialog template resource
    CLoginDialog loginDlg(IDD_LOGINDLG);

    // Create and show the dialog box
    INT_PTR nRet = -1;
    nRet = loginDlg.DoModal();

    // Check the return value of DoModal
    if (nRet == IDOK)
    {
        // Process the user's input
        CString userName = loginDlg.GetUserName();
        CString password = loginDlg.GetUserPassword();

        // ...
    }
}

You can't change the return value of the DoModal() function, and even if you could, I wouldn't recommend it. That's not the idiomatic way of doing this, and if you changed its return value to a string type, you would lose the ability to see when the user canceled the dialog (in which case, the string value returned should be ignored altogether).

Instead, add another function (or multiple) to your dialog box class, something like GetUserName() and GetUserPassword, and then query the values of those functions after DoModal returns IDOK.

For example, the function that shows the dialog and processes user input might look like this:

void CMainWindow::OnLogin()
{
    // Construct the dialog box passing the ID of the dialog template resource
    CLoginDialog loginDlg(IDD_LOGINDLG);

    // Create and show the dialog box
    INT_PTR nRet = -1;
    nRet = loginDlg.DoModal();

    // Check the return value of DoModal
    if (nRet == IDOK)
    {
        // Process the user's input
        CString userName = loginDlg.GetUserName();
        CString password = loginDlg.GetUserPassword();

        // ...
    }
}
久伴你 2024-11-16 13:48:54

我正在寻找答案,并同意在大多数情况下您不会更改对话框的标准行为。但在某些情况下,您可能希望选择用户实际响应的内容,例如,如果您有多个按钮,并且特别希望他们选择顶部的“确定”而不是底部的“确定”。你知道指标。

或者说,如果对话框在运行您的函数时导致错误,您是否希望得到稍微不同的结果。最好返回一个不是 IDOK 而是其他值的值。

我在这里找到了 Dialog::EndDialog() ,其中包含详细信息和使用示例: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!
}

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!
}
夜唯美灬不弃 2024-11-16 13:48:54

我认为这是不可能的(或合理的)。 DoModal 返回一个 INT_PTR,通常用于了解用户退出对话框时执行的操作(按“确定”、“取消”、出现错误...)。做到这一点的方法是拥有公共成员或函数,对话框集和对话框的调用者可以访问这些成员或函数来了解值。
就像这样:

CMyDialog dlg;

if(dlg.DoModal()==IDOK)
{
    CString str1 = dlg.m_String1;
    CString str2 = dlg.GetString2();
}

例如,这就是您使用 CFileDialog 的方式。

I don't think it is possible (or reasonable). DoModal returns an INT_PTR, which is usually used to know what the user did to exit the dialog (press OK, Cancel, there was an error...). The way to do it is have public members or functions which the dialog set and the caller of the dialog can access to know the values.
Like so:

CMyDialog dlg;

if(dlg.DoModal()==IDOK)
{
    CString str1 = dlg.m_String1;
    CString str2 = dlg.GetString2();
}

It's the way you would use CFileDialog, for example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文