在mfc中获取另一个对话框控件的句柄 [英] Get handle of another dialog control in mfc

查看:999
本文介绍了在mfc中获取另一个对话框控件的句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的应用程序中,有两个对话框,我在第一个对话框中发生事件时更新第二个对话框控件变量的值。

First我创建了第二个对话框的一个对象并尝试在其中一个控件变量上调用SetWindowText()方法,但它抛出异常。当我调试应用程序时,第二个对话框对象在Autos部分显示Hwnd值= 0x0000000.Below我粘贴了一些代码,请建议所需的更改。

如何解决这个问题?



Hi,
In my application,there are two dialogs and I am tyring to update value of second dialog control variable on the event occurred in first dialog.
First of all I create an object of the second dialog and trying to call SetWindowText() method on one of the control variables of it,but it throws exception.When I debug application,second dialog object shows Hwnd values=0x0000000 in Autos section.Below I paste some code snippest,please suggest required changes.
How to solve this problem?

void CFirstDlg::OnBnClickedConnect()
{
      UpdateData(1);
      CSecondDlg* dlg=new CSecondDlg(); //Create object of second dialog
      dlg->mtxtTS1.SetWindowTextW(L"0");  //Trying to access control variable and set properties on it
      delete dlg;
}

推荐答案

Quote:



dlg-> mtxtTS1.SetWindowTextW(L0); //试图访问控制变量并在其上设置属性


dlg->mtxtTS1.SetWindowTextW(L"0"); //Trying to access control variable and set properties on it

你不能这样做,因为 CDialog 对象实际上是创建的但是它没有窗口关联(因为,假设一个模态对话框,窗口仅在调用 DoModal 时创建)。

您应该设置 CSecondDlg 的成员变量(比如 m_szText ),然后在中CSecondDlg :: OnInitDialog 用它来实际查看窗口文本。)



顺便说一句,看看这个老人 - 黄金 toxcct 的文章:对话通讯 [ ^ ]。

You cannot do that, because the CDialog object is actually created but it has no window associated (because, assuming a modal dialog, the window is created only when DoModal is called).
You should instead set a member variable of CSecondDlg (say m_szText) and then, in CSecondDlg::OnInitDialog use it to actually chage the window text).

By the way, have a look at this oldie-goldie toxcct's article: Dialogs Communication[^].


这篇关于在mfc中获取另一个对话框控件的句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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