关闭模式 MFC 对话框后获取编辑框文本
在模态 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在调用 DoModal() 之前,不会创建对话框及其控件,并且如前所述,在 DoModal() 返回时已被销毁。因此,您不能在 DoModal() 之前或之后调用 GetDlgItem()。将数据传递或检索到控件的解决方案是使用类中的变量。您可以在创建类实例时、调用 DoModal() 之前设置它。在 OnInitDialog() 中,您将变量的值放入控件中。然后,当窗口被销毁时,您从控件中获取值并将其放入变量中。然后从调用上下文中读取变量。
像这样的东西(注意我直接在浏览器中输入它,所以可能会有错误):
然后你可以像这样使用它:
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):
Then you can use it like this:
UpdateData(TRUE)
Outside对话框:
// 新值仍然在 dlg.m_myVariableName 中
UpdateData(TRUE)
Outside the dialog:
// the new value is still in dlg.m_myVariableName
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 nowAfxGetMainWnd()
is broken.Either way, you can't get the dialog box's control values after the dialog has been destroyed.
我经常使用
With dsohinh 是您想要将数据获取到 mainform 的对话框形式。
获取数据后调用 SetModifiedFlag(true) 设置视图数据更新。
调用 UpdateAllViews(NULL) 将数据设置到主窗体
I often use
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
这个解决方案可能看起来很长,这意味着已经为这个看似很小的任务编写了很多代码。
但是,当我们在子窗口中有一个列表或树时,所有项目都在子窗口中创建
并且项目必须移动到父窗口,
那么这就有意义了。
该源代码可以轻松创建一个窗口,并在关闭给父级之前从窗口传输信息。
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.