从模态 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.
相关文章