Can I return a custom value from a dialog box#39;s DoModal function?(我可以从对话框的 DoModal 函数返回自定义值吗?)
问题描述
我想要做的是,在使用 DoModal() 创建一个对话框并在框中按 OK 退出它之后,返回一个自定义值.例如,用户将在对话框中输入的几个字符串.
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 函数返回自定义值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我可以从对话框的 DoModal 函数返回自定义值吗?
- 近似搜索的工作原理 2021-01-01
- 静态初始化顺序失败 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 从python回调到c++的选项 2022-11-16
- C++ 协变模板 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
