从模态MFC窗体获取输入的信息 [英] Get entered info from modal MFC form

查看:255
本文介绍了从模态MFC窗体获取输入的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了 CPreparationDlg 的表单,它具有编辑控制。然后我创建的应用程序创建模态形式,并按下确定,我需要读取输入的文本在编辑控制变为主程序的变量。什么是最好的方法呢?

  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();
//我想读文本信息




return TRUE;


}

CPreparationApp theApp;


解决方案

p>

您不能从对话框类外部的对话框中读取或编写编辑控件。与MFC控件关联的窗口在调用DoModal之前不存在,或在从DoModal返回之后。



对话框类必须具有简单类型的成员变量,例如int,double,string。



您可以在构造函数中或在调用DoModal之前设置这些变量。



在OnOK处理程序的对话框类中,将值从控件移动到成员变量。



DoModal返回后,您可以从成员变量中检索值。



您还需要检查DoModal的返回值,因为您需要知道用户是否使用Ok或Cancel退出以了解返回的值是否有效。 / p>

这些是MFC对话框的基本原则。



至于你不问的问题,仍然不正确。注释掉的声明// CPreparationDlg Dlg;意味着变量Dlg未定义。设置m_pMainWnd,然后在InitInstance内调用DoModal也不是MFC应用程序的标准用法。



您需要做一些更多的研究,以了解它是如何工作的。


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;

解决方案

The answer to the question you are asking:

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.

The dialog class must have member variables with simple types such as int, double, string.

You can set these variables in the constructor, or before the call to DoModal.

Within the dialog class in an OnOK handler, you move values from the controls to the member variables.

After DoModal returns, you can retrieve the values from the member variables.

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.

These are fundamental principles of MFC dialogs.

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窗体获取输入的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆