MFC 打印程序的编制

转载 2012年03月27日 10:11:19

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);

}

 

以上打印过程没有考虑打印预览功能,只是实现直接打印功能,现在的虚拟打印机软件( pdffactory )已经很多了,个人感觉没必要打印预览功能。如果要要想实现打印预览功能,那么我们就要考虑借用 MFC 的文档-视图结构或者对话框结构,原理是一样的。介绍此类的文档网络上也很多,我刚开始是看 http://www.vckbase.com/document/viewdoc/?id=1618 才打算写上面这么一篇的。后来发现 http://www.vckbase.com/document/viewdoc/?id=1270 ,个人觉得很值得一读的。

 

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

1.OnPreparePrinting

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

2.OnBeingPrinting

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

3.OnPrepareDC

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

4.OnPrint

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

5.OnEndPrinting

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

 

这几个函数中,OnPreparePrinting、OnBeginPrinting、OnEndPrinting函数在一次打印过程中将只被调用一次,不管这一次打印内容有多少页,而OnPrepareDC、OnPrint函数则每打印一页都将被调用一次,这种调用次序对于打印是很有用的。

 

好了,关于打印就总结到这里,最后还推荐一篇文章,对打印总括性比较好,如果觉得看我的代码太累就看看http://www.vckbase.com/document/viewdoc/?id=1041

相关文章推荐

MFC 打印程序的编制

 MFC 在实现打印编程工作时已经建立了一个已有的框架,我们只要往这个框架里面填东西即可。 打印工作其实就是在 DC 上绘图,不过这里的 DC 是打印机的 DC ,明确这一点之后,想...

使用 MFC 编写打印程序

摘要   本文首先介绍了利用MFC提供的文档视图框架来实现一个打印程序,并实现打印预览,在此基础上,同时通过对MFC源代码的深入探讨,提出了利用该方法在对话框上用MFC实现打印功能,结果表明,利...

MFC做的打印的例子程序

  • 2015-03-17 15:38
  • 13.23MB
  • 下载

使用mfc编写的打印程序

  • 2009-05-06 22:21
  • 176KB
  • 下载

MFC应用程序中处理消息的顺序

转载自:http://hi.baidu.com/zxchao/blog/item/fd96648db87ae711b31bbac2.htmlMFC应用程序中处理消息的顺序1.AfxWndProc() ...

MFC打印程序

  • 2012-03-29 10:21
  • 182KB
  • 下载

MFC首先要知道的--程序执行顺序

MFC的程序执行顺序 很多刚学MFC的人都会被MFC给弄的晕头转向。以前传统的C语言中的main()不见了,window sdk api 中的WinMain()函数也不见了,到底用M...

使用MFC编写打印程序

  • 2013-04-12 09:27
  • 394KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)