建一个多文档的MFC程序,不需任何编程,就能自动实现对文档的打印预览,可是我在自动产生的程序找不到其实现过程。
  可以详细介绍一下它的实现过程吗?

解决方案 »

  1.   

    实现过程在基类CView中
    void CView::OnFilePrintPreview()
    {
    // In derived classes, implement special window handling here
    // Be sure to Unhook Frame Window close if hooked. // must not create this on the frame.  Must outlive this function
    CPrintPreviewState* pState = new CPrintPreviewState; // DoPrintPreview's return value does not necessarily indicate that
    // Print preview succeeded or failed, but rather what actions are necessary
    // at this point.  If DoPrintPreview returns TRUE, it means that
    // OnEndPrintPreview will be (or has already been) called and the
    // pState structure will be/has been deleted.
    // If DoPrintPreview returns FALSE, it means that OnEndPrintPreview
    // WILL NOT be called and that cleanup, including deleting pState
    // must be done here. if (!DoPrintPreview(AFX_IDD_PREVIEW_TOOLBAR, this,
    RUNTIME_CLASS(CPreviewView), pState))
    {
    // In derived classes, reverse special window handling here for
    // Preview failure case TRACE0("Error: DoPrintPreview failed.\n");
    AfxMessageBox(AFX_IDP_COMMAND_FAILURE);
    delete pState;      // preview failed to initialize, delete State now
    }
    }BOOL CView::DoPrintPreview(UINT nIDResource, CView* pPrintView,
    CRuntimeClass* pPreviewViewClass, CPrintPreviewState* pState)
    {
    ASSERT_VALID_IDR(nIDResource);
    ASSERT_VALID(pPrintView);
    ASSERT(pPreviewViewClass != NULL);
    ASSERT(pPreviewViewClass->IsDerivedFrom(RUNTIME_CLASS(CPreviewView)));
    ASSERT(pState != NULL); CFrameWnd* pParent = STATIC_DOWNCAST(CFrameWnd, AfxGetMainWnd());
    ASSERT_VALID(pParent); CCreateContext context;
    context.m_pCurrentFrame = pParent;
    context.m_pCurrentDoc = GetDocument();
    context.m_pLastView = this; // Create the preview view object
    CPreviewView* pView = (CPreviewView*)pPreviewViewClass->CreateObject();
    if (pView == NULL)
    {
    TRACE0("Error: Failed to create preview view.\n");
    return FALSE;
    }
    ASSERT_KINDOF(CPreviewView, pView);
    pView->m_pPreviewState = pState;        // save pointer pParent->OnSetPreviewMode(TRUE, pState);    // Take over Frame Window // Create the toolbar from the dialog resource
    pView->m_pToolBar = new CDialogBar;
    if (!pView->m_pToolBar->Create(pParent, MAKEINTRESOURCE(nIDResource),
    CBRS_TOP, AFX_IDW_PREVIEW_BAR))
    {
    TRACE0("Error: Preview could not create toolbar dialog.\n");
    pParent->OnSetPreviewMode(FALSE, pState);   // restore Frame Window
    delete pView->m_pToolBar;       // not autodestruct yet
    pView->m_pToolBar = NULL;
    pView->m_pPreviewState = NULL;  // do not delete state structure
    delete pView;
    return FALSE;
    }
    pView->m_pToolBar->m_bAutoDelete = TRUE;    // automatic cleanup // Create the preview view as a child of the App Main Window.  This
    // is a sibling of this view if this is an SDI app.  This is NOT a sibling
    // if this is an MDI app. if (!pView->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
    CRect(0,0,0,0), pParent, AFX_IDW_PANE_FIRST, &context))
    {
    TRACE0("Error: couldn't create preview view for frame.\n");
    pParent->OnSetPreviewMode(FALSE, pState);   // restore Frame Window
    pView->m_pPreviewState = NULL;  // do not delete state structure
    delete pView;
    return FALSE;
    } // Preview window shown now pState->pViewActiveOld = pParent->GetActiveView();
    CView* pActiveView = pParent->GetActiveFrame()->GetActiveView();
    if (pActiveView != NULL)
    pActiveView->OnActivateView(FALSE, pActiveView, pActiveView); if (!pView->SetPrintView(pPrintView))
    {
    pView->OnPreviewClose();
    return TRUE;            // signal that OnEndPrintPreview was called
    } pParent->SetActiveView(pView);  // set active view - even for MDI // update toolbar and redraw everything
    pView->m_pToolBar->SendMessage(WM_IDLEUPDATECMDUI, (WPARAM)TRUE);
    pParent->RecalcLayout();            // position and size everything
    pParent->UpdateWindow(); return TRUE;
    }
      

  2.   

    我在程序中这样用:出现打印预览窗口后程序就死了,怎么回事?
    pV->OnFilePrintPreview();
    //pV是已获取的视图指针
      

  3.   

    在CZslView中重载OnFilePrintPreview():
    void CZslView::OnFilePrintPreview()
    {
    CEditView::OnFilePrintPreview();
    }
    我对话框的按钮事件里:
             CZslDoc* pD = NULL;
    pD = (CZslDoc*)(((CZslApp*)AfxGetApp())->pMyDoc);//获取文档指针。pMyDoc是我在ZslApp加的一个CDocument成员变量,在CZslDoc的构造函数中有((CZslApp*)AfxGetApp())->pMyDoc = this;         pD->OnOpenDocument("f:\\temp.txt");         CZslView* pV = (CZslView*)(((CZslApp*)AfxGetApp())->pMyView);//类似pD的做法
             pV->OnFilePrintPreview();
      

  4.   

    建议看一下侯sir的深入浅出MFC,里面有比较详细的介绍
      

  5.   

    建一个多文档的MFC程序,不需任何编程,就能自动实现对文档的打印预览。
    在基于对话框的vc应用程序中一样可以实现相同的功能。
      

  6.   

    我的菜单是自定义的,不能有打印预览这项,我现在想借MFC的自动打印预览完成这样的功能:
    用户在给定一个文件名后,按下对话框的一个按钮后,实现打印这个文件的打印预览功能。程序用WIZARD建成多文档的,不过只提供给用户一些对话框用。
      

  7.   

    我一个实例,可实现你所提出的功能,如果要,请给出e-mail.
      

  8.   

    TO:thomas_xj(农民) 
        我的EMAIL地址已留言给你,谢谢!