问题描述
我想做的是,在使用 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.
推荐答案
您不能更改 DoModal()
函数,即使你可以,我不会推荐它。这不是惯用的方式,如果你将它的返回值更改为字符串类型,你将失去看到用户取消对话框的能力(在这种情况下,返回的字符串值
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).
而是添加另一个函数(或多个)到你的对话框类,例如 GetUserName()
和 GetUserPassword
,然后查询 DoModal
后返回 IDOK
。
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();
// ...
}
}
这篇关于我可以从对话框的DoModal函数返回自定义值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!