MFC 打印程序的编制

MFC 在实现打印编程工作时已经建立了一个已有的框架,我们只要往这个框架里面填东西即可。

打印工作其实就是在 DC 上绘图,不过这里的 DC 是打印机的 DC ,明确这一点之后,想当然的,我们打印的任务可以分为获取打印机 DC 、根据打印机驱动让用户交互设置打印参数、开始打印、打印的具体过程(分页、绘制等)、结束打印、释放 DC 。以下通过一段示列代码来说明:

// 打印控制函数

void print()

{

       CPrintDialog dlg(FALSE,PD_ALLPAGES);// 如为 TRUE 则不能实现打印功能, // 而是建立打印机,祥查 msdn

       // Get the default printer.

       dlg.m_pd.nMinPage = 1;

       dlg.m_pd.nMaxPage = 15;

       dlg.m_pd.nCopies = 1;

       dlg.m_pd.nFromPage = 1;

       dlg.m_pd.nToPage = 2;

       if (dlg.DoModal() == IDOK)

       {// 如果仅打印一份则可以用 dlg.GetDefaults();

              HDC hdcPrinter = dlg.GetPrinterDC();

              if (hdcPrinter == NULL) // Is a default printer set up?

              {

                     AfxMessageBox(_T("Buy a printer!"));

              }

              else

              {

                     CDC dcPrinter;

                     dcPrinter.Attach(hdcPrinter);

                     // 通知打印机驱动程序接受打印文档

                     DOCINFO docinfo;

                     memset(&docinfo, 0, sizeof(docinfo));

                     docinfo.cbSize = sizeof(docinfo);

                     docinfo.lpszDocName = _T("CDC::StartDoc() Code Fragment");

                     DEVMODE * dm = dlg.GetDevMode();

                     float rato = dm->dmPrintQuality / 25.4;// 转化到 mm 模式下

                     // If it fails, complain and exit gracefully.

                     if (dcPrinter.StartDoc(&docinfo) < 0)

                     {

                            AfxMessageBox(_T("Printer would not initalize"));

                     }

                     else

                     {

                            // Do printing.

                            BOOL bPrintOK = TRUE;

                            int m_nCopies = dlg.m_pd.nCopies;

                            int page_Min(0),page_Max(0);

                            if (m_nCopies > 1)

                            {

                                   // 是否逐份打印

                                   if (dlg.PrintCollate() == TRUE)

                                   {

                                          // 是否全部打印

                                          if (dlg.PrintAll() == TRUE)

                                          {

                                                 // 全部打印

                                                 page_Min = dlg.m_pd.nMinPage;

                                                 page_Max = dlg.m_pd.nMaxPage;

                                          }

                                          else if (dlg.PrintRange() == TRUE)

                                          {

                                                 // 选定页码范围

                                                 page_Min = dlg.m_pd.nFromPage;

                                                 page_Max = dlg.m_pd.nToPage;

                                          }

                                          else if (dlg.PrintSelection() == TRUE)

                                          {

                                                 // 选定的选择区域

                                                 // 暂时不知道怎么处理的

                                          }

                                          for(int i = 0; i < m_nCopies; i++)

                                          {

                                                 for(UINT j = page_Min; j < page_Max + 1; j++)

                                                 {

                                                        if (dcPrinter.StartPage() < 0)// 只起到判断作用,不 // 加不会影响正常打印

                                                        {

                                                               AfxMessageBox(_T("Could not start page"));

                                                               bPrintOK = FALSE;

                                                               break;

                                                        }                                

                                                        OnPrint(dcPrinter, j, rato);

                                                        if (dcPrinter.EndPage() <= 0)  // 告诉打印驱动换 // 页,如果不加则将所有页打在一张纸上

                                                        {

                                                               AfxMessageBox(_T("Could not end page"));

                                                               bPrintOK = FALSE;

                                                               break;

                                                        }

                                                 }

                                                 if (bPrintOK == FALSE)

                                                 {

                                                        dcPrinter.AbortDoc();

                                                        break;

                                                 }

                                          }

                                   }

                                   else

                                   {

                                          // 是否全部打印

                                          if (dlg.PrintAll() == TRUE)

                                          {

                                                 // 全部打印

                                                 page_Min = dlg.m_pd.nMinPage;

                                                 page_Max = dlg.m_pd.nMaxPage;

                                          }

                                          else if (dlg.PrintRange() == TRUE)

                                          {

                                                 // 选定页码范围

                                                 page_Min = dlg.m_pd.nFromPage;

                                                 page_Max = dlg.m_pd.nToPage;

                                          }

                                          else if (dlg.PrintSelection() == TRUE)

                                          {

                                                 // 选定的选择区域

                                                 // 暂时不知道怎么处理的

                                          }

                                          for(UINT j = page_Min; j < page_Max + 1; j++)

                                          {

                                                 for(int i = 0; i < m_nCopies; i++)

                                                 {

                                                        if (dcPrinter.StartPage() < 0)// 只起到判断作用,不 // 加不会影响正常打印

                                                        {

                                                               AfxMessageBox(_T("Could not start page"));

                                                               bPrintOK = FALSE;

                                                               break;

                                                        }                                

                                                        OnPrint(dcPrinter, j, rato);

                                                        if (dcPrinter.EndPage() <= 0)  // 告诉打印驱动换 // 页,如果不加则将所有页打在一张纸上

                                                        {

                                                               AfxMessageBox(_T("Could not end page"));

                                                               bPrintOK = FALSE;

                                                               break;

                                                        }

                                                 }

                                                 if (bPrintOK == FALSE)

                                                 {

                                                        dcPrinter.AbortDoc();

                                                        break;

                                                 }

                                          }

                                   }

                            }

                            else

                            {

                                   // 是否全部打印

                                   if (dlg.PrintAll() == TRUE)

                                   {

                                          // 全部打印

                                          page_Min = dlg.m_pd.nMinPage;

                                          page_Max = dlg.m_pd.nMaxPage;

                                   }

                                   else if (dlg.PrintRange() == TRUE)

                                   {

                                          // 选定页码范围

                                          page_Min = dlg.m_pd.nFromPage;

                                          page_Max = dlg.m_pd.nToPage;

                                   }

                                   else if (dlg.PrintSelection() == TRUE)

                                   {

                                          // 选定的选择区域

                                          // 暂时不知道怎么处理的

                                   }

                                   for(UINT i = page_Min; i < page_Max + 1; i++)

                                   {

                                          if (dcPrinter.StartPage() < 0)// 只起到判断作用,不加不会 // 影响正常打印

                                          {

                                                 AfxMessageBox(_T("Could not start page"));

                                                 bPrintOK = FALSE;

dcPrinter.AbortDoc();

                                                 break;

                                          }                                

                                          OnPrint(dcPrinter, i, rato);

                                          if (dcPrinter.EndPage() <= 0)// 告诉打印驱动换页,如果不 // 加则将所有页打在一张纸上

                                          {

                                                 AfxMessageBox(_T("Could not end page"));

                                                 bPrintOK = FALSE;

dcPrinter.AbortDoc();

                                                 break;

                                          }

                                   }

                            }

                            if (bPrintOK == TRUE)

                            {

                                   dcPrinter.EndDoc();// 结束打印

                            }

                            dcPrinter.Detach();// 释放 DC

                     }           

              }

       }    

}

 

// 具体的页面绘制函数

void OnPrint(CDC &dc, UINT &nCurPage, float &rato)

{

       CFont font;

       font.CreatePointFont(100 * rato, " 宋体 ");

       CGdiObject* pOldFont = dc.SelectObject(&font);

       CString str;

       str.Format("Hello World! -- %d",nCurPage);

       dc.TextOut(50 * rato, 50 * rato, str);

       dc.SelectObject(pOldFont);

}

 

 

以下针对微软封装的几个相应函数按调用顺序做一简单描述:

1.OnPreparePrinting

OnPreparePrinting 函数最先被调用,用来初始化打印机等。比如,若没有安装打印机,则该函数将提示用户安装打印机。用户程序可以向其中加入别的初始化代码,比如,计算打印你的文档所需要的总页数,然后调用视类中的打印机初始化函数BOOL   DoPreparePrinting(CPrintInfopInfo)即可。而用AppWizard生成的代码中,OnPreparePrinting函数将只是调用函数DoPreparePrinting,并传递参数。

2.OnBeingPrinting

OnBeingPrinting 函数是开始打印文档前调用的函数,用户可以在其中加入另一些对于打印过程的初始化代码,比如分配打印过程中将要使用的“笔”(CPen)、“刷子”(CBrush)等,默认的代码中该函数将直接返回。

3.OnPrepareDC

OnPrepareDC 函数用于在打印前准备打印设备场,如窗口大小、原点,视图大小、原点等。同时该函数在视类显示文档内容时也被调用,默认的代码中该函数调用基类中的OnPrepareDC函数。

4.OnPrint

OnPrint 函数则是具体的打印过程,它利用前面准备好的设备场进行打印。

5.OnEndPrinting

OnEndPrinting 函数是与OnBeginPrinting函数相对应的函数,它在打印完成后由应用框架调用,用于释放在OnBeginPrinting中分配的“对象”,如“笔”、“刷子”等,其默认的代码中该函数将直接返回。

 SetViewportOrg(0,originy);    实现奖 CView OnDraw内容分页。

 

 

CPreviewView 默认是使用onDraw实现绘制和打印,显示,默认使用主窗口作为打印窗口。

 CPrintPreviewState* pState = new CPrintPreviewState;
 CWnd* pMainWnd = GetParentFrame();
 CWinThread *pThread = AfxGetThread();
 pThread->m_pMainWnd = pMainWnd;
 pThread->m_pActiveWnd = pMainWnd;
 CFrameWnd *pParent = (CFrameWnd*)pMainWnd;
 if (DYNAMIC_DOWNCAST(CFrameWnd, pMainWnd) == NULL)
 {
 }
 // Create the preview view object
 CPrintPreView* pView = (CPrintPreView*)RUNTIME_CLASS(CPrintPreView)->CreateObject();
 if (pView == NULL)
 {
  return ;
 }
 if(pView->SetConfigCWnd(pParent,this,AFX_IDD_PREVIEW_TOOLBAR,pState))
 {
  delete pView;
  delete pState;
  return ;
 }
 if (!pView->SetPrintView(this))
 {
  delete pView;
  delete pState;
  return ;            // signal that OnEndPrintPreview was called
 }
 pParent->SetActiveView(pView);  // set active view - even for MDI
 pParent->RecalcLayout();            // position and size everything
 pParent->UpdateWindow();/*
 CPrintPreviewState* pState = new CPrintPreviewState;
 CWinThread *pThread = AfxGetThread();
 pThread->m_pMainWnd = this->GetParent(); 
 // pThread->m_pMainWnd = this->GetParentFrame(); 
 TRY
 {
  if (!DoPrintPreview(AFX_IDD_PREVIEW_TOOLBAR, this,
   RUNTIME_CLASS(CPrintPreView), pState))
  {
   AfxMessageBox(AFX_IDP_COMMAND_FAILURE);
   delete pState;     
  }
 }
 CATCH_ALL(e)
 {
  delete pState;
  THROW_LAST();
 }
 END_CATCH_ALL*/