The study of chapter 13 in programming windows with mfc-printing with document and views

来源:互联网 发布:阿里云服务器退款 编辑:程序博客网 时间:2024/04/28 06:24

1.The base architecture:

    CDC dc(this);
    CPrintDialog dlg(False);
    //dlg.GetDefault();
    //if(dlg.DoModal()==IDOK)
    dc.Attach(dlg.GetPrintDC())

    DOCINFO di;
    ::ZeroMemory(&di,sizeof(DOCINFO));
    di.cbSize=sizeof(DOCINFO)
    di.lpszDocName=_T("ddddddddd");
    if(dc.StatDoc(&di))
    int nContinue=true;

    for(int i=0;i<nPage&&nContinue;i++)
    {
            dc.StartPage();
            //
            if(dc.EndPage()<0)
             nContinue=false;
    }
    if(nContinue)
        dc.EndDoc();
    else
        dc.Abort();

 

2.Abort processure:[CView]

    int CALLBACK AbortProc(HDC hDC,UNIT nCode)

    {

           MSG msg;

           while(!bUserAbort&&::PeekMessage(&msg,0,0,PM_NOREMOVE)

                    AfxGetThread()->PumpMessage();

           return !bUserAbort;

    }

 

3.MFC print Architecture

   OnPreparePrinting(CDC *pDC,CPrintInfo *pf)  

   {

          pf->SetMaxPage(3);

          return DoPreparePrinting(pDC,pf);

   } 

   OnBeginPrinting(CDC *pDC,CPrintInfo *pf)

   {

          int nPageHeight=pf->GetDeviceCaps(VERTRES);

          int nDocLength=pf->GetDocLength();

          int nMaxPage=max(1,(nDocLength+nPageHeight-1)/nPageHeight);

          pf->SetMaxPage(nMaxPage);

   }

If it's impossible (or simply inconvenient) to determine the document length before printing begins, you can perform print-time pagination by overriding OnPrepareDC and setting CPrintInfo::m_bContinuePrinting to TRUE or FALSE each time OnPrepareDC is called. An m_bContinuePrinting value equal to FALSE terminates the print job. If you don't call SetMaxPage, the framework assumes the document is only one page long. Therefore, you must override OnPrepareDC and set m_bContinuePrinting to print documents that are more than one page long if you don't set the maximum page number with SetMaxPage.

 

   OnPrepareDC(CDC *pDC,CPrintInfo *pf)  //you need to override OnPrepareDC only if you use OnDraw to draw to both the screen and the printed page

   {

         CView::OnPrepareDC(pDC,pf);

         if(pDC->isPrinting())

         {

            //.........

            int y=(pf->m_nCurPage-1)/nPageHeight;

            pDC->SetViewOrg(0,-y);

         }           

   }

   OnPrint(CDC *pDC,CPrintInfo *pf)         //It's usually more practical to put printer output logic in OnPrint and screen logic in OnDraw.

   {

        PrintHeader(pDC);

        PrintPageNumber(pDC,pf->m_nCurPage);

        // Set the viewport origin and/or clipping region before

        OnDraw(pDC);

   }

   OnEndPrinting(CDC *pDC,CPrintInfo *pf)

  

4.PrintPreview

    CView::OnFilePrintPreview       ID=ID_FILE_PRINT_PREVIEW

    void CMyView::OnEndPrintPreview (CDC* pDC, CPrintInfo* pInfo,    POINT point, CPreviewView* pView)    {        UINT nPage = pInfo->m_nCurPage;        POINT pt;         // Convert nPage into a scroll position in pt.        ScrollToPosition (pt);        CScrollView::OnEndPrintPreview (pDC, pInfo, point, pView);     }

 

原创粉丝点击