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 函数返回自定义值吗?
基础教程推荐
- 这个宏可以转换成函数吗? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
