按照说明这个函数返回其父窗口对象,但是我在实际使用中却发现不对额。建了一个基于对话框的程序,主窗口(即主对话框)创建一个非模态对话框1,该非模态对话框1又创建一个非模态对话框2。我在非模态对话框2中调用GetParent(),按说明应该返回非模态对话框1指针,但是我调试发现其返回的是主窗口(即主对话框对象),又尝试调用::GetParent(m_hWnd),仍然返回主窗口句柄。
   如果直接在非模态对话框2中使用m_pParentWnd,该指针就是正确的非模态对话框1.求解释额~
   对话框1是在主对话框中创建的,对话框2是在对话框1创建的,创建时都传入this。    

解决方案 »

  1.   

    我想我找到原因了,在资源中对话框模板风格属性不为child,而是popup,也就是说在对话框A中创建对话框B,对话框B不一定就是对话框A的子窗口哇,除非风格设置为child,这样理解对不呢?
      

  2.   

    那肯定,不是child,哪来的parent?CWnd::GetParent  
    CWnd* GetParent( ) const;Return ValueIdentifies the parent window if the member function is successful. Otherwise, the value is NULL, which indicates an error or no parent window. The returned pointer may be temporary and should not be stored for later use.ResCall this function to get a pointer to a child window’s parent window (if any). The GetParent function returns a pointer the immediate parent. In contrast, the GetParentOwner function returns a pointer to the most immediate parent or owner window that is not a child window (does not have the WS_CHILD style). If you have a child window within a child window GetParent and GetParentOwner return different results.