问题是这样的,我希望主对话框是基于对话框的,所以我用 MFC AppWizard 生成了一个基于 单文档/视图结构 的框架,然后改写 InitInstance 中的代码,如下:pDocTemplate1 = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CMyDoc1),
RUNTIME_CLASS(CMyFrame1),       // main SDI frame window
RUNTIME_CLASS(CMyView1));
AddDocTemplate(pDocTemplate1);pDocTemplate2 = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CMyDoc2),
RUNTIME_CLASS(CMyFrame2),       // main SDI frame window
RUNTIME_CLASS(CMyView2));
AddDocTemplate(pDocTemplate2);
...CMainDlg MainDlg;
m_pMainWnd = &MainDlg;
MainDlg.DoModal();
然后视图在 Dialog 中增加按钮之类的命令,进行初始化并打开一个基于 单文档/视图结构 的窗口,比如这样:theApp.pDocTemplate1->OpenDocumentFile(NULL);
打开这个我想要的窗口,窗口的创建和打开都是没有问题的,问题是怎样获取新建的这个窗口框架的指针,而且最好我想只在起初的时候保留一份副本,不需要每次都创建并打开一次,该怎么做啊!

解决方案 »

  1.   

    1) 在View中获得Doc指针 
    2) 在App中获得MainFrame指针 
    3) 在View中获得MainFrame指针 
    4) 获得View(已建立)指针 
    5) 获得当前文档指针 
    6) 获得状态栏与工具栏指针 
    7) 获得状态栏与工具栏变量 
    8) 在Mainframe获得菜单指针 
    9) 在任何类中获得应用程序类 
    10) 从文档类取得视图类的指针(1) VC中编程对于刚刚开始学习的同学,最大的障碍和问题就是消息机制和指针获取与 
    操作。其实这些内容基本上是每本VC学习工具书上必讲的内容,而且通过MSDN很多 
    问题都能解决。下面文字主要是个人在编程中指针使用的一些体会,说的不当的地 
    方请指正。一般我们使用的框架是VC提供的Wizard生成的MFC App Wizard(exe)框架, 
    无论是多文档还是单文档,都存在指针获取和操作问题。下面这节内容主要是一般 
    的框架,然后再讲多线程中的指针使用。使用到的类需要包含响应的头文件。首先 
    一般获得本类(视,文档,对话框都支持)实例指针this,用this的目的,主要可以通 
    过类中的函数向其他类或者函数中发指针,以便于在非本类中操作和使用本类中的 
    功能。 1) 在View中获得Doc指针 CYouSDIDoc *pDoc=GetDocument();一个视只能有一个文 
    档。 
    2) 在App中获得MainFrame指针 
    CWinApp 中的 m_pMainWnd变量就是MainFrame的指针 
    也可以: CMainFrame *pMain =(CMainFrame *)AfxGetMainWnd(); 
    3) 在View中获得MainFrame指针 CMainFrame *pMain=(CmaimFrame *)AfxGetApp()->m_pMainWnd; 
    4) 获得View(已建立)指针 CMainFrame *pMain=(CmaimFrame *)AfxGetApp()->m_pMainWnd; 
    CyouView *pView=(CyouView *)pMain->GetActiveView(); 
    5) 获得当前文档指针 CDocument * pCurrentDoc =(CFrameWnd *)m_pMainWnd->GetActiveDocument(); 
    6) 获得状态栏与工具栏指针 CStatusBar * pStatusBar=(CStatusBar *)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_STATUS_BAR); 
    CToolBar * pToolBar=(CtoolBar *)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_TOOLBAR); 7) 如果框架中加入工具栏和状态栏变量还可以这样 
    (CMainFrame *)GetParent()->m_wndToolBar; 
    (CMainFrame *)GetParent()->m_wndStatusBar; 8) 在Mainframe获得菜单指针 CMenu *pMenu=m_pMainWnd->GetMenu(); 
    9) 在任何类中获得应用程序类 
    用MFC全局函数AfxGetApp()获得。 10) 从文档类取得视图类的指针 
    我是从http://download.cqcnc.com/soft/program/article/vc/vc405.html学到的, 
    从文档获得视图类指针目的一般为了控制同一文档的多个视图的定位问题,我的体会 
    特别是文字处理CEditView当产生多个视图类时,这个功能是非常需要的。 
    CDocument类提供了两个函数用于视图类的定位: 
    GetFirstViewPosition()和GetNextView() 
    virtual POSITION GetFirstViewPosition() const; 
    virtual CView* GetNextView(POSITION& rPosition) const;从该对象 如何访问其他对象 
    全局函数 调用全局函数AfxGetApp可以得到CWinApp应用类指针 
    应用 AfxGetApp()->m_pMainWnd为框架窗口指针;用CWinApp::GetFirstDocTemplatePostion、CWinApp::GetNextDocTemplate来遍历所有文档模板 
    文档 调用CDocument::GetFirstViewPosition,CDocument::GetNextView来遍历所有和文档关联的视图;调用CDocument:: GetDocTemplate 获取文档模板指针 
    文档模板 调用CDocTemplate::GetFirstDocPosition、CDocTemplate::GetNextDoc来遍历所有对应文档 
    视图 调用CView::GetDocument 得到对应的文档指针; 调用CView::GetParentFrame 获取框架窗口 
    文档框架窗口 调用CFrameWnd::GetActiveView 获取当前得到当前活动视图指针; 调用CFrameWnd::GetActiveDocument 获取附加到当前视图的文档指针 
    MDI 框架窗口 调用CMDIFrameWnd::MDIGetActive 获取当前活动的MDI子窗口(CMDIChildWnd)   我们列举一个例子,综合应用上表中的函数,写一段代码,它完成遍历文档模板、文档和视图的功能:CMyApp *pMyApp = (CMyApp*)AfxGetApp(); //得到应用程序指针
    POSITION p = pMyApp->GetFirstDocTemplatePosition();//得到第1个文档模板
    while (p != NULL) //遍历文档模板
    {
     CDocTemplate *pDocTemplate = pMyApp->GetNextDocTemplate(p);
     POSITION p1 = pDocTemplate->GetFirstDocPosition();//得到文档模板对应的第1个文档
     while (p1 != NULL) //遍历文档模板对应的文档
     {
      CDocument *pDocument = pDocTemplate->GetNextDoc(p1);
      POSITION p2 = pDocument->GetFirstViewPosition(); //得到文档对应的第1个视图
      while (p2 != NULL) //遍历文档对应的视图
      {
       CView *pView = pDocument->GetNextView(p2);
      }
     }

     
      

  2.   

    你说的这些基本上我都是知道的啊,我的问题是怎样在 InitInstance 建立模板而先不创建窗口,先创建住对话框,SDI 的创建在以后进行,而且能获得所创建的 SDI 的指针啊,就像下面这样:
    pDocTemplateBrowse = new CSingleDocTemplate(
    IDR_MAINFRAME,
    RUNTIME_CLASS(CBrowseDoc),
    RUNTIME_CLASS(CBrowseFrame),       // main SDI frame window
    RUNTIME_CLASS(CBrowseView));
    AddDocTemplate(pDocTemplateBrowse); pDocTemplateAdd = new CSingleDocTemplate(
    IDR_MAINFRAME,
    RUNTIME_CLASS(CAddNewDoc),
    RUNTIME_CLASS(CAddNewFrame),       // main SDI frame window
    RUNTIME_CLASS(CAddNewView));
    AddDocTemplate(pDocTemplateAdd);

    // Parse command line for standard shell commands, DDE, file open
    /* CCommandLineInfo cmdInfo;
    ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line
    if (!ProcessShellCommand(cmdInfo))
    return FALSE; // The one and only window has been initialized, so show and update it.
    m_pMainWnd->ShowWindow(SW_SHOW);
    m_pMainWnd->UpdateWindow();
    */ CMainDlg MainDlg;
    m_pMainWnd = &MainDlg;
    MainDlg.DoModal();
    而在对话框中按钮消息响应时,产生一个 SDI 并获取其框架指针啊,问题时这个时候 m_pMainWnd 是 CMainDlg 对象,要获取这个产生的 SDI 框架指针似乎没办法啊theApp.pDocTemplateAdd->OpenDocumentFile(NULL);//如何获取这个创建的 SDI 的框架指针
    pMainDlg->ShowWindow(SW_HIDE);
      

  3.   

    问题已经解决,谢谢,得到了很大的提示!CBrowseDoc *pDoc = (CBrowseDoc *)theApp.pDocTemplateBrowse->OpenDocumentFile(NULL);
    POSITION Pos = pDoc->GetFirstViewPosition();
    CBrowseView *pView = (CBrowseView *)pDoc->GetNextView(Pos);
    pBrowseFrm = (CBrowseFrame *)pView->GetParentFrame();
    pMainDlg->ShowWindow(SW_HIDE);
    这样子多次获得就可以了!