windows 我可以从对话框的 DoModal 函数返回自定义值吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6111809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I return a custom value from a dialog box's DoModal function?
提问by Karudi
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.
我想要做的是,在创建一个对话框DoModal()
并在框中按 OK 退出它后,返回一个自定义值。例如,用户将在对话框中输入的几个字符串。
回答by Cody Gray
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 canceledthe dialog (in which case, the string value returned should be ignored altogether).
您不能更改DoModal()
函数的返回值,即使可以,我也不建议这样做。这不是执行此操作的惯用方法,如果您将其返回值更改为字符串类型,您将无法查看用户何时取消对话框(在这种情况下,返回的字符串值应完全忽略)。
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
.
相反,添加其他功能(或多个)到您的对话框类,像GetUserName()
和GetUserPassword
,然后经过查询这些功能的价值DoModal
回报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();
// ...
}
}
回答by CaptainBli
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.
我一直在寻找答案并同意在大多数情况下您不会更改对话框的标准行为。但是在某些情况下,如果您有多个按钮,并且特别希望他们选择顶部的 OK 与底部的 OK,那么您可能希望选择用户实际响应的内容。你知道指标。
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.
或者说,如果对话框在运行您的函数时导致错误,您是否希望得到稍微不同的结果。返回一个不是 IDOK 而是其他一些值的值会很好。
I found Dialog::EndDialog()
with details and an example of usage here: MSDN: Dialog::EndDialog
我在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!
}
回答by MikMik
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:
我不认为这是可能的(或合理的)。DoModal 返回一个 INT_PTR,它通常用于了解用户为退出对话框所做的操作(按 OK、Cancel、出现错误...)。这样做的方法是拥有公共成员或函数,对话集和对话的调用者可以访问这些成员或函数以了解这些值。像这样:
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.
例如,这是您使用 CFileDialog 的方式。