CDocTemplate  中有详细的介绍!MSDN  中……  包括产生过程的图解! 还有 VIEW 的!

解决方案 »

  1.   

    1  在索引中 键入 CDocTemplate  回车
    2  在弹出的3个选项中选 1 
    3  出现 CDocTemplate  的简介
      走道最下面 找到
    For more information on CDocTemplate, see Document Templates and the Document/View Creation Process in Visual C++ Programmer's Guide.4 点击 连接就可以了…… Document Templates and the Document/View Creation Process5 慢慢看了,还有其他的一些连接……
      

  2.   

    我已经明白了一些,在CWinApp类调用OnFileNew()函数就可以新建文档了。
    但如何在CWinApp类得到和使用所新建文档的视图类或者文档类呢?
      

  3.   

    有MFC的源码吧,在你的Application中重载OnFileNew,如下:
    CXXApp::OnFileNew()
    {
        CWinApp::OnFileNew();
    }
    直接Trace进去就可以看到全部过程
      

  4.   

    对了,就是象PowerPoint中向导那样,不是一个空文档,而是有一些数据的文档!
    不知道它是如何实现的,所以我认为应该理解一些其实现过程!
      

  5.   

    CDocument* CDoorCtrlApp::CreateNewFrameViewDoc(CMultiDocTemplate* pDocTemplate)
    {
    POSITION posDoc=pDocTemplate->GetFirstDocPosition();
    CDocument* pDocument =NULL;
    if(posDoc!=NULL){
    pDocument =pDocTemplate->GetNextDoc(posDoc);
    //To get the first view in the list of views: POSITION posView = pDocument ->GetFirstViewPosition();
    while(posView){//modified for viewbar 
    CView* pFirstView = pDocument ->GetNextView(posView);
    ASSERT(pFirstView !=NULL);
    pFirstView ->GetParentFrame()->GetLastActivePopup()->BringWindowToTop();
    }
    return pDocument;
    }
    else{
    pDocument =pDocTemplate->CreateNewDocument();
    if (pDocument == NULL)
    {
    TRACE0("CDocTemplate::CreateNewDocument returned NULL.\n");
    AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
    return NULL;
    }
    ASSERT_VALID(pDocument);
    CString strTitle;
    pDocTemplate->GetDocString(strTitle,CDocTemplate::windowTitle);
    pDocument->SetTitle(strTitle);
    BOOL bAutoDelete = pDocument->m_bAutoDelete;
    pDocument->m_bAutoDelete = FALSE;   // don't destroy if something goes wrong
    CFrameWnd* pFrame = pDocTemplate->CreateNewFrame(pDocument, NULL);
    pDocument->m_bAutoDelete = bAutoDelete;
    if (pFrame == NULL)
    {
    AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
    delete pDocument;       // explicit delete on error
    return NULL;
    }
    ASSERT_VALID(pFrame);
    if (!pDocument->OnNewDocument())
    {
    // user has be alerted to what failed in OnNewDocument
    TRACE0("CDocument::OnNewDocument returned FALSE.\n");
    pFrame->DestroyWindow();
    return NULL;
    }
    // it worked, now bump untitled count
    pDocTemplate->InitialUpdateFrame(pFrame ,pDocument,TRUE);
    pFrame->ShowWindow(SW_SHOWMAXIMIZED);
    }
    return pDocument ;
    }
      

  6.   

    To jiangsheng(蒋晟):
    首先谢谢您的代码!
    但我还是有点不太明白。
    我使用下面的代码调用您写的函数:
    POSITION pos=GetFirstDocTemplatePosition();
           CDocTemplate *pDocTemplate=GetNextDocTemplate(pos);
           CTestDoc *pDocument=(CTestDoc *)CreateNewFrameViewDoc( (CMultiDocTemplate *)pDocTemplate );
    然后使用下面的代码用所得到的文档指针将其变量赋值,
           pDocument->m_fNewPrice=2.0f;
    最后更新视图显示,
           pDocument->UpdateAllViews(NULL);
    最终发现没有任何变化,这是为什么?
    是我的使用方法不对吗?
      

  7.   

    To Suddy(风):
    串行化?不行的!
    我意思好像没有说明白。
    我是象PowerPoint中向导那样,当然也允许创建空文档的,如何直接几下串行化的话,将不能再创建空文档了!
      

  8.   

    设断点看看Document的内容。界面上没有变化不代表数据也没有变化。
      

  9.   

    jiangsheng(蒋晟),我同意你的观点!我想举个例子以便说的更详细一些:
    比如我的视图类CTestView的基类为CFormView类,其中有一个编辑控件,其对应变量为CString m_oPriceEdit;
    文档类为CTestDoc,对应视图类的变量m_fNewPrice;
    现在我想用两种方法建立空文档,一种是直接使用默认的方法建立,另外一种是自定义内容的方法,即新建一个空文档时直接将m_fNewPrice赋值为2.0,同时编辑框中也显示2.0;
    应该如何编写代码?
      

  10.   

    建议不要在视图中存储数据,文档才是存储数据的地方。
    in CMyFormmView::DoDataExchange(CDataExchange* pDX){
        CMyDoc* pDoc=GetDocument();
        ......
        //不要在MFC注释里面写
       DDX_Text(pDX,IDC_PRICE,pDoc->m_fNewPrice);
    }
      

  11.   

    非常谢谢 谢晟老兄!在您的帮助下我已经解决了这个问题!
    由于这两天我的Win2000系统崩溃了,一些数据也丢失了,因此一直忙于修复系统,所以没有来得及给您加分,耽误了几天,非常抱歉!今天系统终于正常了,在下不敢再耽搁了,现在开始加分!
    请查收!
      

  12.   

    最后我还有一个不太明白的问题,想请教一下,就是上面我说的那个问题,
    其内容为:
    [请教简单问题——关于视图更新] --30
    比如我的视图类CTestView的基类为CFormView类,其中有一个编辑控件,其对应变量为CString m_oPriceEdit;
    对应文档类为CTestDoc,其对应视图类的变量double m_fNewPrice;
    现在我想用设置文档类变量m_fNewPrice=2.0f,如何让它的编辑框中也显示2.0呢?应该如何编写更新视图类的代码呢?
    谢谢!
    请访问帖子:
    http://www.csdn.net/expert/topic/229/229096.shtm