代码如下: 
void CMainFrame::CreateNewWindow()        
{
         CMDIChildWnd* pActiveChild = MDIGetActive();
CDocument* pDocument;
if (pActiveChild == NULL ||
  (pDocument = pActiveChild->GetActiveDocument()) == NULL)
{
TRACE0("Warning: No active document for WindowNew command.\n");
AfxMessageBox(AFX_IDP_COMMAND_FAILURE);
return;     // command failed
} // otherwise we have a new frame !
CDocTemplate* pTemplate = pDocument->GetDocTemplate();
ASSERT_VALID(pTemplate);
CFrameWnd* pFrame = pTemplate->CreateNewFrame(pDocument, pActiveChild);
if (pFrame == NULL)
{
TRACE0("Warning: failed to create new frame.\n");
return;     // command failed
} pTemplate->InitialUpdateFrame(pFrame, pDocument);
}
现在我直接调用该函数没有问题,可以如新建窗口命令创建新的子窗口;
我想让它在程序运行时创建新的子窗口(可能是多个),但是MDIGetActive函数得到的pActiveChild指针值为NULL,请问各位大哥如何解决这个问题?

解决方案 »

  1.   

    换个问法简单点,可能跟原问题不搭嘎,但对于我的程序却是有联系的,大家都知道在InitInstance中加上cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;是让应用初始化时的文档为空,有没有办法让应用从最近文件列表打开时打开空的文档和窗口,即所有的子窗口都是手动生成?
      

  2.   

    在theApp的InitInstance()结尾做。
    int DocLen = 3;
    pDocTemplate->CloseAllDocuments(TRUE);CString strTitle = "标题";
    Cex5View *pView = NULL;
    POSITION posView;
    CDocument* pDoc;for(int i = DocLen-1; i>=0; i--)
    {
       pDoc = pDocTemplate->OpenDocumentFile(NULL);
       pDoc->SetTitle(strTitle);
       posView = pDoc->GetFirstViewPosition();
       while(posView != NULL)
       {
          pView = (Cex5View *)(pDoc->GetNextView(posView));
          if(pView->IsKindOf(RUNTIME_CLASS(Cex5View)))
          {
    HICON hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);// 设置图标
    pView->GetParentFrame()->SetIcon(hIcon, FALSE);
    pView->OnInitialUpdate();
          }
       }
    }