我在OnDraw()里画出要打印的数据,不管又多少数据,一直画完为止,预览和打印时只能看到一页,我怎么样才能预览和打印多页,我查过论坛中的有关分页打印的帖子,说得不太清楚,好像要自己计算,可怎么计算呢,谁能详细说一下。
    我是今年三月才开始用VC的,对VC不太了解,希望大虾们多指点。谢谢!

解决方案 »

  1.   

    关于打印的可以覆盖的6个虚函数(以FrameWork调用次序列出):
    CMyView::OnPreparePrinting    
    设定Document长度,调用CView::DoPreparePrinting以显示打印对话框并产生打印机DC
    CMyView::OnBeginPrinting
    设定以DC为准的Document长度(可以计算页数并设置),配置GDI资源
    CMyView::OnPrepareDC 
    改变viewport原点、剪截区以及其他DC属性
    CMyView::OnPrint
    CMyView::OnDraw
    打印页眉、页脚等等
    CMyView::OnEndPrinting
    释放GDI资源基于此,可以知道如:动态计算页码并决定是否继续打印应该在CMyView::OnPrepareDC中、设置页码数应该在CMyView::OnBeginPrinting中。
      

  2.   

    void CMyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
    // TODO: add extra initialization before printing
    }BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo)
    {
    pInfo->SetMaxPage(2);   // the document is two pages long:
    // the first page is the title page
    // the second is the drawing
    BOOL bRet = DoPreparePrinting(pInfo); // default preparation
    pInfo->m_nNumPreviewPages = 2;  // Preview 2 pages at a time
    // Set this value after calling DoPreparePrinting to override
    // value read from .INI file
    return bRet;
    }void CMyView::OnPrint(CDC* pDC, CPrintInfo* pInfo) 
    {
    if (pInfo->m_nCurPage == 1)  // page no. 1 is the title page
    {
    PrintTitlePage(pDC, pInfo);
    return; // nothing else to print on page 1 but the page title
    }
    CString strHeader = _T("My Print Header"); PrintPageHeader(pDC, pInfo, strHeader);
    // PrintPageHeader() subtracts out from the pInfo->m_rectDraw the
    // amount of the page used for the header. pDC->SetWindowOrg(pInfo->m_rectDraw.left,-pInfo->m_rectDraw.top); // Now print the rest of the page
    OnDraw(pDC);
    }void CMyView::PrintPageHeader(CDC* pDC, CPrintInfo* pInfo,
    CString& strHeader)
    {
    // Print a page header consisting of the name of
    // the document and a horizontal line
    pDC->SetTextAlign(TA_LEFT);
    pDC->TextOut(0,-25, strHeader);  // 1/4 inch down // Draw a line across the page, below the header
    TEXTMETRIC textMetric;
    pDC->GetTextMetrics(&textMetric);
    int y = -35 - textMetric.tmHeight;          // line 1/10th inch below text
    pDC->MoveTo(0, y);                          // from left margin
    pDC->LineTo(pInfo->m_rectDraw.right, y);    // to right margin // Subtract out from the drawing rectange the space used by the header.
    y -= 25;    // space 1/4 inch below (top of) line
    pInfo->m_rectDraw.top += y;
    }void CMyView::PrintTitlePage(CDC* pDC, CPrintInfo* pInfo)
    {
    // Prepare a font size for displaying the file name
    LOGFONT logFont;
    memset(&logFont, 0, sizeof(LOGFONT));
    logFont.lfHeight = 75;  //  3/4th inch high in MM_LOENGLISH
    // (1/100th inch)
    CFont font;
    CFont* pOldFont = NULL;
    if (font.CreateFontIndirect(&logFont))
    pOldFont = pDC->SelectObject(&font); // Get the file name, to be displayed on title page
    CString strPageTitle = _T("My Print Header"); // Display the file name 1 inch below top of the page,
    // centered horizontally
    pDC->SetTextAlign(TA_CENTER);
    pDC->TextOut(pInfo->m_rectDraw.right/2, -100, strPageTitle); if (pOldFont != NULL)
    pDC->SelectObject(pOldFont);
    }void CMyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
    // TODO: add cleanup after printing
    }