最近我编些MDI界面的程序的时候,用的是一个文档对应多个视图,即生成的视图都是和一个文档关联,这样在我要激活我需要的视图的时候,激活不了,请问各位大侠是什么问题,望多多指教!下面是我的程序片断:
MdiTestApp头文件中定义:
CMultiDocTemplate *m_ptest1;
CMultiDocTemplate *m_ptest2;
MdiTestApp.cpp文件中
m_ptest1 = new CMultiDocTemplate(
IDR_MDITESTYPE,
RUNTIME_CLASS(CMdiTestDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(test1)); m_ptest2 = new CMultiDocTemplate(
IDR_MDITESTYPE,
RUNTIME_CLASS(CMdiTestDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(test2));MainFrm.cpp文件中产生新视图
void CMainFrame::OnCreateTest1View() 
{
// TODO: Add your command handler code here

CMDIChildWnd *pActiveChild = MDIGetActive();
CDocument *pDoc;
if(pActiveChild == NULL||(pDoc = pActiveChild->GetActiveDocument()) == NULL)
{
return;
}

CDocTemplate *pTemDoc =  ((CMdiTestApp*)AfxGetApp())->m_ptest1;
ASSERT_VALID(pTemDoc);
CFrameWnd *pFrame = pTemDoc->CreateNewFrame(pDoc,pActiveChild);
if(pFrame)
{
pTemDoc->InitialUpdateFrame(pFrame,pDoc);
}}问题视下面的代码在我想激活指定视图时无用
void CMainFrame::OnFind() 
{
// TODO: Add your command handler code here
CMDIChildWnd *pChild = 
             (CMDIChildWnd *)this->GetActiveFrame();

CView *pView1 = (CView*)(this->MDIGetActive());

if(pView1)
{
BOOL bFlag = pView1->IsKindOf(RUNTIME_CLASS(test1));
} CMDIChildWnd *pActiveChild = MDIGetActive();//???????为什么会无效/////////////////////////////////////////////////////////////////////////////////下面是另一种写法为什么也无效
CDocument *pDoc;
if(pActiveChild == NULL||(pDoc = pActiveChild->GetActiveDocument()) == NULL)
{
return;
}

   POSITION pos = pDoc->GetFirstViewPosition();
   while (pos != NULL)
   {
      CView* pView = pDoc->GetNextView(pos);
      BOOL bTRUE = pView->IsKindOf(RUNTIME_CLASS(test1));
  pView->UpdateWindow();
  if(bTRUE)
  {
             this->MDIActivate(pView);///或 this->SetActiveView(pView);     break; 
  }
   }   讨教了,望各位大侠热心帮忙指点一下,本人项目急用!!!!万分感谢
}

解决方案 »

  1.   

    你这样等于是两个文档类型个有各的view。在一个文档上加一个view应该用CDocument::AddView()。
      

  2.   

    那我该在哪儿加???给各小例子,在我目前的程序上加的:
    CFrameWnd *pFrame = pTemDoc->CreateNewFrame(pDoc,pActiveChild);
    pDoc->AddView((CView*)pFrame);
    程序出错
      

  3.   

    不要activate view,应该activate frame:
          BOOL bTRUE = pView->IsKindOf(RUNTIME_CLASS(test1));
      pView->UpdateWindow();
      if(bTRUE)
               {
                    ((CFrameWnd *)pView->GetParent())->ActivateFrame(SH_SHOW);
                }