关闭模式 MFC 对话框后获取编辑框文本

发布于 2024-11-06 10:09:13 字数 292 浏览 3 评论 0原文

在模态 MFC 对话框中,我想在对话框关闭后从编辑框中提取文本。我尝试这样做:

CPreparationDlg Dlg;
CString m_str;

m_pMainWnd = &Dlg;
Dlg.DoModal();
CWnd *pMyDialog=AfxGetMainWnd();
CWnd *pWnd=pMyDialog->GetDlgItem(IDC_EDIT1);
pWnd->SetWindowText("huha max");
return TRUE;

它不起作用。

From a modal MFC dialog, I want to extract text from an edit box after the dialog is closed. I attempted this:

CPreparationDlg Dlg;
CString m_str;

m_pMainWnd = &Dlg;
Dlg.DoModal();
CWnd *pMyDialog=AfxGetMainWnd();
CWnd *pWnd=pMyDialog->GetDlgItem(IDC_EDIT1);
pWnd->SetWindowText("huha max");
return TRUE;

It does not work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

撞了怀 2024-11-13 10:09:13

在调用 DoModal() 之前,不会创建对话框及其控件,并且如前所述,在 DoModal() 返回时已被销毁。因此,您不能在 DoModal() 之前或之后调用 GetDlgItem()。将数据传递或检索到控件的解决方案是使用类中的变量。您可以在创建类实例时、调用 DoModal() 之前设置它。在 OnInitDialog() 中,您将变量的值放入控件中。然后,当窗口被销毁时,您从控件中获取值并将其放入变量中。然后从调用上下文中读取变量。

像这样的东西(注意我直接在浏览器中输入它,所以可能会有错误):

class CMyDialog : CDialog
{
  CString m_value;
public:  
  CString GetValue() const {return m_value;}
  void SetValue(const CString& value) {m_value = value;}

  virtual BOOL OnInitDialog();
  virtual BOOL DestroyWindow( );
}

BOOL CMyDialog::OnInitDialog()
{
  CDialog::OnInitDialog();

  SetDlgItemText(IDC_EDIT1, m_value);

  return TRUE;
}

BOOL CMyDialog::DestroyWindow()
{
  GetDlgItemText(IDC_EDIT1, m_value);

  return CDialog::DestroyWindow();
}

然后你可以像这样使用它:

CMyDialog dlg;

dlg.SetValue("stackoverflow");

dlg.DoModal();

CString response = dlg.GetValue();

The dialog and its controls is not created until you call DoModal() and as already pointed, is destroyed already by the time DoModal() returns. Because of that you cannot call GetDlgItem() neither before, nor after DoModal(). The solution to pass or retrieve data to a control, is to use a variable in the class. You can set it when you create the class instance, before the call to DoModal(). In OnInitDialog() you put in the control the value of the variable. Then, when the window is destroyed, you get the value from the control and put it into the variable. Then you read the variable from the calling context.

Something like this (notice I typed it directly in the browser, so there might be errors):

class CMyDialog : CDialog
{
  CString m_value;
public:  
  CString GetValue() const {return m_value;}
  void SetValue(const CString& value) {m_value = value;}

  virtual BOOL OnInitDialog();
  virtual BOOL DestroyWindow( );
}

BOOL CMyDialog::OnInitDialog()
{
  CDialog::OnInitDialog();

  SetDlgItemText(IDC_EDIT1, m_value);

  return TRUE;
}

BOOL CMyDialog::DestroyWindow()
{
  GetDlgItemText(IDC_EDIT1, m_value);

  return CDialog::DestroyWindow();
}

Then you can use it like this:

CMyDialog dlg;

dlg.SetValue("stackoverflow");

dlg.DoModal();

CString response = dlg.GetValue();
无戏配角 2024-11-13 10:09:13
  1. 打开对话框资源,右键单击文本框并选择“添加变量”,选择值类型和 CString
  2. 在对话框类中:关闭之前,调用 UpdateData(TRUE)
  3. Outside对话框:

    CPreparationDlg dlg(AfxGetMainWnd());
    
    dlg.m_myVariableName = "我的值"; 
    
    dlg.DoModal();
    

    // 新值仍然在 dlg.m_myVariableName 中

  1. Open your dialog resource, right-click on the textbox and choose "Add variable", pick value-type and CString
  2. In the dialog-class: before closing, call UpdateData(TRUE)
  3. Outside the dialog:

    CPreparationDlg dlg(AfxGetMainWnd());
    
    dlg.m_myVariableName = "my Value"; 
    
    dlg.DoModal();
    

    // the new value is still in dlg.m_myVariableName

放赐 2024-11-13 10:09:13

DoModal() 在返回之前销毁对话框,因此该值不再可用。

很难说为什么要在对话框中设置 m_pMainWnd 。老实说,我不太确定你想在那里做什么。这肯定会导致问题,因为现在 AfxGetMainWnd() 已损坏。

无论哪种方式,在对话框被销毁后您都无法获取对话框的控制值。

DoModal() destroys the dialog box before it returns and so the value is no longer available.

It's hard to tell why you are setting m_pMainWnd to your dialog. To be honest, I'm not really sure what you are trying to do there. That's bound to cause problems as now AfxGetMainWnd() is broken.

Either way, you can't get the dialog box's control values after the dialog has been destroyed.

久伴你 2024-11-13 10:09:13

我经常使用

D_SOHINH dsohinh = new D_SOHINH();
    dsohinh.vd_kichthuoc=v_kichthuocDOC;
    dsohinh.vd_sohinh=v_soluongDOC;
    if(dsohinh.DoModal()==IDOK)
    {
        v_soluongDOC=dsohinh.vd_sohinh;
        v_kichthuocDOC=dsohinh.vd_kichthuoc;
    }
    SetModifiedFlag(true);
    UpdateAllViews(NULL);

With dsohinh 是您想要将数据获取到 mainform 的对话框形式。
获取数据后调用 SetModifiedFlag(true) 设置视图数据更新。
调用 UpdateAllViews(NULL) 将数据设置到主窗体

I often use

D_SOHINH dsohinh = new D_SOHINH();
    dsohinh.vd_kichthuoc=v_kichthuocDOC;
    dsohinh.vd_sohinh=v_soluongDOC;
    if(dsohinh.DoModal()==IDOK)
    {
        v_soluongDOC=dsohinh.vd_sohinh;
        v_kichthuocDOC=dsohinh.vd_kichthuoc;
    }
    SetModifiedFlag(true);
    UpdateAllViews(NULL);

With dsohinh is Dialog form that you want to get data to mainform .
After get data then call SetModifiedFlag(true) to set view data updated.
call UpdateAllViews(NULL) to Set data to mainform

氛圍 2024-11-13 10:09:13

这个解决方案可能看起来很长,这意味着已经为这个看似很小的任务编写了很多代码。
但是,当我们在子窗口中有一个列表或树时,所有项目都在子窗口中创建
并且项目必须移动到父窗口,
那么这就有意义了。
该源代码可以轻松创建一个窗口,并在关闭给父级之前从窗口传输信息。

    //copy the two functions in your code 

    //1-    bool peek_and_pump(void)

    //      template<class T,class THISCLASS>
    //2-    void TshowWindow(int id,T *&pVar,THISCLASS *ths)

    //and make two member variable
    // bool do_exit;
    // bool do_cancel;
    //in child dialog class.
    //set true value in do_exit in child dialog for exit

    CchildDialog *dlg;

    template<class T,class THISCLASS>
    void TshowWindow(int id,T *&pVar,THISCLASS *ths)
    {
        T *p=pVar;
        if(!p)
            p= new T;
        if(p->m_hWnd)
        {
            p->SetForegroundWindow();
        }
        else
        {
            delete p;
            p= new T;
            if(!(p->m_hWnd && IsWindow(p->m_hWnd)))
            {
                p->Create(id,ths);
                if(IsWindow(p->m_hWnd))
                    p->ShowWindow(TRUE);
            }   
        }
        pVar=p;
    }


    bool peek_and_pump(void)
    {
        MSG msg;

    #if defined(_AFX) || defined(_AFXDLL)
        while(::PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))
        {
            if(!AfxGetApp()->PumpMessage())
            {
                ::PostQuitMessage(0);
                return false;
            }
        }
        long lIdle = 0;
        while(AfxGetApp()->OnIdle(lIdle++))
            ;
    #else
        if(::PeekMessage(&msg,NULL,0,0,PM_REMOVE))
        {
            ::TranslateMessage(&msg);
            ::DispatchMessage(&msg);
        }
    #endif

        return true;
    }

    void CparentPage::OnBnClick1()
    {
        if(dlg)
        {
            dlg->DestroyWindow();   
        }
        TshowWindow<CchildDialog,CparentPage>(IDD_DIALOG_child,dlg,this);
        dlg->GetDlgItem(IDC_EDIT_1)->SetWindowText("");
        dlg->m_temp_window.EnableWindow(FALSE);//enable or disable controls.
        dlg->UpdateData(false);//for to be done enable of disable or else.
        dlg->do_exit=false;
        dlg->do_cancel=false;
        while(dlg->do_exit==false)
        {
            peek_and_pump();//wait for dlg->do_exit set true
        }
        if( dlg->do_cancel==false ) 
        {
            CString str1;
            dlg->GetDlgItem(IDC_EDIT_1)->GetWindowText(str1);
            //or other member variale of CchildDialog
            //after finish all work with dlg then destroy its.
        }
        dlg->DestroyWindow();   
        

    }
    
    void CchildDialog::OnBnClickedOk()
    {
        UpdateData();
        OnOK();
        do_exit=true;
        do_cancel=false;
    }
    void CchildDialog::OnBnClickedCancel()
    {
        OnCancel();
        do_exit=true;
        do_cancel=true;
    }

This solution may seem long, meaning that so much code has been written for this seemingly small task.
But when we have a list or tree inside the child window where all the items are created in the child window
and the items have to be moved to the parent window,
then it makes sense.
This source code can easily create a window and transfer information from the window before closing to the parents.

    //copy the two functions in your code 

    //1-    bool peek_and_pump(void)

    //      template<class T,class THISCLASS>
    //2-    void TshowWindow(int id,T *&pVar,THISCLASS *ths)

    //and make two member variable
    // bool do_exit;
    // bool do_cancel;
    //in child dialog class.
    //set true value in do_exit in child dialog for exit

    CchildDialog *dlg;

    template<class T,class THISCLASS>
    void TshowWindow(int id,T *&pVar,THISCLASS *ths)
    {
        T *p=pVar;
        if(!p)
            p= new T;
        if(p->m_hWnd)
        {
            p->SetForegroundWindow();
        }
        else
        {
            delete p;
            p= new T;
            if(!(p->m_hWnd && IsWindow(p->m_hWnd)))
            {
                p->Create(id,ths);
                if(IsWindow(p->m_hWnd))
                    p->ShowWindow(TRUE);
            }   
        }
        pVar=p;
    }


    bool peek_and_pump(void)
    {
        MSG msg;

    #if defined(_AFX) || defined(_AFXDLL)
        while(::PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))
        {
            if(!AfxGetApp()->PumpMessage())
            {
                ::PostQuitMessage(0);
                return false;
            }
        }
        long lIdle = 0;
        while(AfxGetApp()->OnIdle(lIdle++))
            ;
    #else
        if(::PeekMessage(&msg,NULL,0,0,PM_REMOVE))
        {
            ::TranslateMessage(&msg);
            ::DispatchMessage(&msg);
        }
    #endif

        return true;
    }

    void CparentPage::OnBnClick1()
    {
        if(dlg)
        {
            dlg->DestroyWindow();   
        }
        TshowWindow<CchildDialog,CparentPage>(IDD_DIALOG_child,dlg,this);
        dlg->GetDlgItem(IDC_EDIT_1)->SetWindowText("");
        dlg->m_temp_window.EnableWindow(FALSE);//enable or disable controls.
        dlg->UpdateData(false);//for to be done enable of disable or else.
        dlg->do_exit=false;
        dlg->do_cancel=false;
        while(dlg->do_exit==false)
        {
            peek_and_pump();//wait for dlg->do_exit set true
        }
        if( dlg->do_cancel==false ) 
        {
            CString str1;
            dlg->GetDlgItem(IDC_EDIT_1)->GetWindowText(str1);
            //or other member variale of CchildDialog
            //after finish all work with dlg then destroy its.
        }
        dlg->DestroyWindow();   
        

    }
    
    void CchildDialog::OnBnClickedOk()
    {
        UpdateData();
        OnOK();
        do_exit=true;
        do_cancel=false;
    }
    void CchildDialog::OnBnClickedCancel()
    {
        OnCancel();
        do_exit=true;
        do_cancel=true;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文