Get entered info from modal MFC form(从模态 MFC 表单中获取输入信息)
问题描述
我创建了具有 Edit Control
的表单 CPreparationDlg
.然后我创建了创建模态表单的应用程序,并在其上按 OK 后,我需要将编辑控件中输入的文本读入主程序的变量中.最好的方法是什么?
I have created form CPreparationDlg
that has Edit Control
. Then I have created application that creates modal form and afer pressing OK on it I need to read entered text in Edit Control into variable of main program. What is the best way to do it?
class CPreparationApp : public CWinApp
{
public:
BOOL InitInstance();
};
class CPreparationDlg : public CDialog
{
public:
enum { IDD = IDD_PREPARATION_DLG };
CPreparationDlg();
~CPreparationDlg();
};
CPreparationDlg::CPreparationDlg()
: CDialog(CPreparationDlg::IDD)
{
}
CPreparationDlg::~CPreparationDlg()
{
}
BOOL CPreparationApp::InitInstance()
{
//CPreparationDlg Dlg;
m_pMainWnd = &Dlg;
Dlg.DoModal();
// there I would like to read text info
return TRUE;
}
CPreparationApp theApp;
推荐答案
你所问问题的答案:
您无法从对话框类外部读取或写入对话框上的编辑控件.在调用 DoModal 之前或从 DoModal 返回之后,与 MFC 控件关联的窗口不存在.
You can not read or write from Edit controls on a dialog from outside of the dialog class. The windows associated with the MFC controls do not exist before the call to DoModal, or after the the return from DoModal.
对话框类必须有int、double、string等简单类型的成员变量.
The dialog class must have member variables with simple types such as int, double, string.
您可以在构造函数中设置这些变量,也可以在调用 DoModal 之前设置这些变量.
You can set these variables in the constructor, or before the call to DoModal.
在 OnOK 处理程序的对话框类中,您将值从控件移动到成员变量.
Within the dialog class in an OnOK handler, you move values from the controls to the member variables.
DoModal 返回后,您可以从成员变量中检索值.
After DoModal returns, you can retrieve the values from the member variables.
您还需要检查 DoModal 的返回值,因为您需要知道用户是使用 Ok 还是 Cancel 退出才能知道返回值是否有效.
You would also want to check the return value of DoModal, since you need to know if the user exited with Ok or Cancel to know whether the returned value is valid.
这些是 MFC 对话框的基本原则.
These are fundamental principles of MFC dialogs.
至于你没有问的问题,贴出来的代码还是不正确的.注释掉的声明//CPreparationDlg Dlg;表示变量 Dlg 未定义.设置 m_pMainWnd,然后在 InitInstance 中调用 DoModal 似乎也不是 MFC 应用程序的标准用法.
As for the questions you don't ask, the posted code is still not correct. The commented-out declaraion //CPreparationDlg Dlg; means that the variable Dlg is undefined. Setting m_pMainWnd, then calling DoModal within InitInstance also does not appear to be standard usage of an MFC application.
你需要做更多的研究来了解这一切是如何运作的.
You will need to do some more research to find out how that all works.
这篇关于从模态 MFC 表单中获取输入信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从模态 MFC 表单中获取输入信息
基础教程推荐
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01