Tom Tom
Tom Tom

Reputation: 1209

Confusing: Retrieve Parent Wnd in CDialog, that is given with SetParent

In a CView derived class I give the dialog the parent via:

CTestDlg& dlg = m_TestDLg;
dlg.SetParent(this);
if (dlg.DoModal() != IDOK)
    return;

Inside CTestDlg I try to retrieve the parent with

CMyView* pTView_ = (CMyView*)GetParent();        // Wrong Parent pointer
CMyView* pTView2 = (CMyView*)GetParentOwner();   // Wrong Parent pointer  
CMyView* pTView3 = (CMyView*)m_pParentWnd;       // Wrong Parent pointer

I thought with SetParent() I can get back it with GetParent().

Even the doc in MSDN confuses me. What really happens with Set/GetParent()?

Upvotes: 1

Views: 286

Answers (2)

JohnCz
JohnCz

Reputation: 1609

Only child windows (WS_CHILD style set) have a parent. For any other window style, SetOwner sets the owner window. Retrieve by calling GetOwner().

Corrected typo SetParent to SetOwner.

Upvotes: 1

Tom Tom
Tom Tom

Reputation: 1209

Not the answer exactly, but this Only ONE gives me the corrent Parent I wanted.
(So I mark it as solution for me)

CView* GenericGetActiveView()
{
  // Retrieve Active View
  CView* pActiveView = NULL;    

  CWnd* pWndMain = AfxGetMainWnd();
  if (NULL != pWndMain)
  {
    if (pWndMain->IsKindOf(RUNTIME_CLASS(CMDIFrameWnd)))
    {
        // MDI application
        CMDIChildWnd* pChild = ((CMDIFrameWnd*)(pWndMain->MDIGetActive();
        if (pChild)
            pActiveView = pChild->GetActiveView();  
    }
    else if (pWndMain->IsKindOf(RUNTIME_CLASS(CFrameWnd))) 
    {   
        // SDI appllication 
        pActiveView = ((CMainFrame*)pWndMain)->GetActiveView();
    }
    else
        TRACE(_T("Attention no MDI / SDI Main Wnd found!\n"));
  }

  return pActiveView;
}

@IInspectable pointed me to this direction in his comment. (MFC has a handle map internally that can easily cause surprising results)

Upvotes: 1

Related Questions