+ -
当前位置:首页 → 问答吧 → 临时构造的对话框变量,为何父窗口无效?

临时构造的对话框变量,为何父窗口无效?

时间:2011-12-27

来源:互联网

基于对话框工程
假设我在主对话框一个单击按钮处理代码,如下
C/C++ code

 CMyDialog dlg(this); // CMyDialog 是一个继承CDialog的类,通过插入资源而得到.
 dlg.Domodal();  // 然后会产生一个模态对话框,为何模态对话框里,它的父类为啥无效呢.



我的问题是,这样传递父类窗口是不对的吗? 是不是必须通过继承来产生呢?

作者: cbx_xx   发布时间: 2011-12-27

父类和父窗口是俩概念

作者: hotpos   发布时间: 2011-12-27

CMyDialog类的构造函数是怎么定义的? CDialog的构造函数可不能这样用
CDialog( LPCTSTR lpszTemplateName, CWnd* pParentWnd = NULL );
CDialog( UINT nIDTemplate, CWnd* pParentWnd = NULL );
CDialog( );
模态对话框不用指定父窗口,这样就可以了
 CMyDialog dlg; 
 dlg.Domodal();

作者: jixiang1983   发布时间: 2011-12-27

不用传递,在子对话框中要获取父对话框的指针直接使用AfxGetMainWnd()就可以了

作者: jiuzhoulh   发布时间: 2011-12-27

CDialog类构造函数有三种形式:
explicit CDialog(
  LPCTSTR lpszTemplateName,
  CWnd* pParentWnd = NULL 
);
explicit CDialog(
  UINT nIDTemplate,
  CWnd* pParentWnd = NULL 
);
CDialog( );

关于pParentWnd参数,MSDN如下定义:
Points to the parent or owner window object (of type CWnd) to which the dialog object belongs. If it is NULL, the dialog object's parent window is set to the main application window.

楼主所说的情况,第二个参数不用赋值,CMyDialog对话框即自动以主对话框为主窗口。

作者: fight_in_dl   发布时间: 2011-12-27

dlg.Domodal(); // 然后会产生一个模态对话框,为何模态对话框里,它的父类为啥无效呢.
===========
你所说的父类是指什么啊?

作者: wltg2001   发布时间: 2011-12-27

我的问题是,这样传递父类窗口是不对的吗? 是不是必须通过继承来产生呢?

这样调用是对的,CMyDialog 已经从CDialog类继承了
CMyDialog dlg(this);//显式调用构造函数。

作者: gameslq   发布时间: 2011-12-27