对话框程序,对话框类的构造函数第二个参数CWnd* pParentWnd为空值,当调用DoModal函数时为什么又有了父类窗口句柄?

解决方案 »

  1.   

    有默认的
    CDlg1的类中DoModal默认父窗口就是CDlg1
      

  2.   

    第一、建立对话框类对象:
    派生类对话框的构造函数如下:
    CMyDlg::CMyDlg():CDialog(CMyDlg::IDD)
    {
      m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }
    从而调用基类构造函数
    CDialog::CDialog(UINT nIDTemplate,CWnd * pParentWnd=NULL)
    {
      m_pParentWnd=pParentWnd;
      m_lpszTemplateName=MAKEINTRESOURECE(nIDTemplate);
      m_nIDhelp=nIDTemplate;
    }
       
    第二、调用DoModal()
    代码简述如下:
    {
    //略
    HWND hWndParent=PreModal() //这行代码的意思是好像找到了父类窗口的句柄,看看是它是怎么
                                //找到的。
    //略
    }CDialog::PreModal()
    {     //略
          //下面调用函数的第一个参数似乎有问题,在前面m_pParentWnd已经被赋了空值,这里怎么
            能调用GetSafeHwnd()函数呢?请高人指点。(在线等候)
           HWND hWnd = CWnd::GetSafeOwner_(m_pParentWnd->GetSafeHwnd(), &m_hWndTop);      return hWnd;
    }
      

  3.   

    CWnd::GetSafeOwnerResCall this member function to retrieve the owner window that should be used for dialog boxes or other modal windows. The safe owner is the first non-child parent window of pParentWnd. If pParentWnd is NULL, the thread’s main window (retrieved via AfxGetMainWnd) is used to find an owner
      

  4.   

    在DoModal时按F11,跟下去可以看到MFC有个判断,如果pParentWnd为空,则
    hWnd = AfxGetMainWnd()->GetSafeHwnd();
    会设为主程序的窗口句柄具体函数在CWnd::GetSafeOwner_里