MFC多文档程序中,如何在Doc类中获取ChildFrame的指针?

解决方案 »

  1.   

    用AfxGetMainWnd()->MDIGetActive()或者用AfxGetMainWnd()->GetActiveFrame()应该都可以
      

  2.   

    BOOL CMDIDoc::OnOpenDocument(LPCTSTR lpszPathName) 
    {
    if (!CDocument::OnOpenDocument(lpszPathName))
    return FALSE;

    // TODO: Add your specialized creation code here
    m_Image.Load(lpszPathName);

    CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();
    CChildFrame* pChildFrame = (CChildFrame*)pMainFrame->GetActiveFrame();
    pChildFrame->SetWindowPos(NULL,0,0,m_Image.Width(),m_Image.Height(),SWP_SHOWWINDOW); return TRUE;
    }在上面这个函数中我加载一幅图片,并根据图片尺寸来设置子窗口的大小,但实际上修改的是主窗口的尺寸,这是怎么回事?
      

  3.   

    子框架和视图被设为Active都发生在OnOpenDocument()或者OnNewDocument()后。所以在这个函数中是得不到子窗口的指针的。
      

  4.   

    得到子框架窗口是通过
    CMDIChildWnd* pWnd = ((CMainFrame*)AfxGetMainWnd())->MDIGetActive();得到的,不是
    ((CMainFrame*)AfxGetMainWnd())->GetActiveFrame();//得到是活动的框架窗口,不一定是子框架窗口
    其他的问题ls都已经说明了
    你可以在OnOpenDocuemnt里PostMessage一个自定义的消息,然后在消息响应函数中去做处理,比如
    AfxGetMainWnd()->PostMessage(UM_SETCHILDSIZE, 0, 0);
    然后在主框架类中处理该自定义的消息
    LRESULT CMainFrame::OnSetChildSize(WPARAM, LPARAM)
    {
     MDIGetActive()->MoveWindow(CRect(0, 0, 400 ,300));
     return 0;
    }