友情提示:如果本网页打开太慢或显示不完整,请尝试鼠标右键“刷新”本网页!
富士康小说网 返回本书目录 加入书签 我的书架 我的书签 TXT全本下载 『收藏到我的浏览器』

深入浅出MFC第2版(PDF格式)-第90部分

快捷操作: 按键盘上方向键 ← 或 → 可快速上下翻页 按键盘上的 Enter 键可回到本书目录页 按键盘上方向键 ↑ 可回到本页顶部! 如果本书没有阅读完,想下次继续接着阅读,可使用上方 "收藏到我的浏览器" 功能 和 "加入书签" 功能!


         return DoPreparePrinting(pInfo); 

 } 



 void CScribbleView::OnBeginPrinting(CDC* /*pDC*/; CPrintInfo* /*pInfo*/) 

 { 

         // TODO: add extra initialization before printing 

 } 



 void CScribbleView::OnEndPrinting(CDC* /*pDC*/; CPrintInfo* /*pInfo*/) 

 { 

         // TODO: add cleanup after printing 

 } 



改写这些函数有助于我们在framework 的打印机制与应用程序的View 对象之间架起 



沟通桥梁。 



为了了解MFC  中的打印机制,我又动用了我的法宝:Visual C++ Debugger 。我发现, 



AppWizard 为我的View 做出这样的Message Map : 



                                                                                    671 


…………………………………………………………Page 734……………………………………………………………

                    第篇    深入  MFC  程式設計    



                    BEGIN_MESSAGE_MAP(CScribbleView; CScrollView) 

                        。。。 

                        // Standard printing mands 

                        ON_MAND(ID_FILE_PRINT; CView::OnFilePrint) 

                        ON_MAND(ID_FILE_PRINT_PREVIEW; CView::OnFilePrintPreview) 

                    END_MESSAGE_MAP() 



                    显然,当【File/Print。。。 】被按下,命令消息将流往CView::OnFilePrint 去处理,于是我以 



                    Debugger 进入该位置并且一步一步执行,得到图12…4 的结果。 



                    // in VIEWPRNT。CPP 

                    #0001  void CView::OnFilePrint() 

                    #0002  { 

                    #0003      // get default print info 

                            

                    #0004      CPrintInfo printInfo; 

                    #0005      ASSERT(printInfo。m_pPD != NULL);    // must be set 

                    #0006 

                    #0007      if (GetCurrentMessage()…》wParam == ID_FILE_PRINT_DIRECT) 

                    #0008      { 

                    #0009          CmandLineInfo* pCmdInfo = AfxGetApp()…》m_pCmdInfo; 

                    #0010 

                    #0011          if (pCmdInfo != NULL) 

                    #0012          { 

                            

                    #0013              if (pCmdInfo…》m_nShellmand == CmandLineInfo::FilePrintTo) 

                    #0014              { 

                    #0015                  printInfo。m_pPD…》m_pd。hDC = ::CreateDC(pCmdInfo…》m_strDriverName; 

                    #0016                      pCmdInfo…》m_strPrinterName; pCmdInfo…》m_strPortName; NULL); 

                    #0017                  if (printInfo。m_pPD…》m_pd。hDC == NULL) 

                    #0018                  { 

                    #0019                      AfxMessageBox(AFX_IDP_FAILED_TO_START_PRINT); 

                    #0020                      return; 

                    #0021                  } 

                    #0022              } 

                    #0023          } 

                    #0024 

                    #0025          printInfo。m_bDirect = TRUE; 

                    #0026      } 

                    #0027 

                            

                    #0028      if (OnPreparePrinting (&printInfo)) 

                    #0029      { 

                    #0030          // hDC must be set (did you remember to call DoPreparePrinting?) 

                    #0031          ASSERT(printInfo。m_pPD…》m_pd。hDC != NULL); 

                    #0032 

                            

                    #0033          // gather file to print to if print…to…file selected 



672 


…………………………………………………………Page 735……………………………………………………………

                                                              第 12 章    列印與預樱А�



#0034          CString strOutput; 

#0035          if (printInfo。m_pPD…》m_pd。Flags & PD_PRINTTOFILE) 

#0036          { 

#0037              // construct CFileDialog for browsing 

#0038              CString strDef(MAKEINTRESOURCE(AFX_IDS_PRINTDEFAULTEXT)); 

#0039              CString strPrintDef(MAKEINTRESOURCE(AFX_IDS_PRINTDEFAULT)); 

#0040              CString strFilter(MAKEINTRESOURCE(AFX_IDS_PRINTFILTER)); 

#0041              CString strCaption(MAKEINTRESOURCE(AFX_IDS_PRINTCAPTION)); 

#0042              CFileDialog dlg(FALSE; strDef; strPrintDef; 

#0043                  OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT; strFilter); 

#0044              dlg。m_ofn。lpstrTitle = strCaption; 

#0045 

#0046              if (dlg。DoModal() != IDOK) 

#0047                  return; 

#0048 

#0049              // set output device to resulting path name 

#0050              strOutput = dlg。GetPathName(); 

#0051          } 

#0052 

#0053        // set up document info and start the document printing process 

#0054          CString strTitle; 

#0055          CDocument* pDoc = GetDocument(); 

#0056          if (pDoc != NULL) 

#0057                  strTitle = pDoc…》GetTitle(); 

#0058          else 

#0059                  GetParentFrame()…》GetWindowText(strTitle); 

#0060          if (strTitle。GetLength() 》 31) 

#0061                  strTitle。ReleaseBuffer(31); 

#0062          DOCINFO docInfo; 

#0063          memset(&docInfo; 0; sizeof(DOCINFO)); 

#0064          docInfo。cbSize = sizeof(DOCINFO); 

#0065          docInfo。lpszDocName = strTitle; 

#0066          CString strPortName; 

#0067          int nFormatID; 

#0068          if (strOutput。IsEmpty()) 

#0069          { 

#0070              docInfo。lpszOutput = NULL; 

#0071              strPortName = printInfo。m_pPD…》GetPortName(); 

#0072              nFormatID = AFX_IDS_PRINTONPORT; 

#0073          } 

#0074          else 

#0075          { 

#0076              docInfo。lpszOutput = strOutput; 

#0077              AfxGetFileTitle(strOutput; 

#0078                  strPortName。GetBuffer(_MAX_PATH); _MAX_PATH); 

#0079              nFormatID = AFX_IDS_PRINTTOFILE; 



                                                                                      673 


…………………………………………………………Page 736……………………………………………………………

                   第篇    深入  MFC  程式設計    



                    #0080          } 

                    #0081 

                            

                    #0082          // setup the printing DC 

                    #0083          CDC dcPrint; 

                    #0084          dcPrint。Attach(printInfo。m_pPD…》m_pd。hDC);  // attach printer dc 

                    #0085          dcPrint。m_bPrinting = TRUE; 

                            

                    #0086          OnBeginPrinting (&dcPrint; &printInfo); 

                            

                    #0087          dcPrint。SetAbortProc(_AfxAbortProc); 

                    #0088 

                    #0089          // disable main window while printing & init printing status dialog 

                            

                    #0090          AfxGetMainWnd()…》EnableWindow(FALSE); 

                    #0091          CPrintingDialog dlgPrintStatus(this); 

                    #0092 

                    #0093          CString strTemp; 

                    #0094          dlgPrintStatus。SetDlgItemText(AFX_IDC_PRINT_DOCNAME; strTitle); 

                    #0095 

                    #0096          dlgPrintStatus。SetDlgItemText(AFX_IDC_PRINT_PRINTERNAME; 

                    #0097              printInfo。m_pPD…》GetDeviceName()); 

                    #0098          AfxFormatString1(strTemp; nFormatID; strPortName); 

                    #0099          dlgPrintStatus。SetDlgItemText(AFX_IDC_PRINT_PORTNAME; strTemp); 

                    #0100 

                    #0101          dlgPrintStatus。ShowWindow(SW_SHOW); 

                    #0102          dlgPrintStatus。UpdateWindow(); 

                    #0103 

                    #0104          // start document printing process 

                            

                    #0105          if (dcPrint。StartDoc(&docInfo) == SP_ERROR) 

                    #0106          { 

                    #0107              // enable main window before proceeding 

                    #0108              AfxGetMainWnd()…》EnableWindow(TRUE); 

                    #0109 

                    #0110              // cleanup and show error message 

                    #0111              OnEndPrinting(&dcPrint; &printInfo); 

                    #0112              dlgPrintStatus。DestroyWindow(); 

                    #0113              dcPrint。Detach();   // will be cleaned up by CPrintInfo destructor 

                    #0114              AfxMessageBox(AFX_IDP_FAILED_TO_START_PRINT); 

                    #0115              return; 

                    #0116          } 

                    #0117 

                    #0118          // Guarantee values are in the valid range 

                    #0119          UINT nEndPage = printInfo。GetToPage(); 

                    #0120          UINT nStartPage = printInfo。GetFromPage(); 

                    #0121 

                    #0122          if (nEndPage 《 printInfo。GetMinPage()) 

                    #0123              nEndPage = printInfo。GetMinPage(); 

                    #0124          if (nEndPage 》 printInfo。GetMaxPage()) 

                    #0125              nEndPage = printInfo。GetMaxPage(); 



674 


…………………………………………………………Page 737……………………………………………………………

                                                               第 12 章    列印與預樱А�



#0126 

#0127          if (nStartPage 《 printInfo。GetMinPage()) 

#0128              nStartPage = printInfo。GetMinPage(); 

#0129          if (nStartPage 》 printInfo。GetMaxPage()) 

#0130              nStartPage = printInfo。GetMaxPage(); 

#0131 

#0132          int nStep = (nEndPage 》= nStartPage) ? 1 : …1; 

#0133          nEndPage = (nEndPage == 0xffff) ? 0xffff : nEndPage + nStep; 

#0134 

#0135          VERIFY(strTemp。LoadString(AFX_IDS_PRINTPAGENUM)); 

#0136 

#0137          // begin page printing loop 

#0138          BOOL bError = FALSE; 

        

#0139          for (printInfo。m_nCurPage = nStartPage; 

#0140               printInfo。m_nCurPage != nEndPage; printInfo。m_nCurPage += nStep) 

#0141          { 

        

#0142              OnPrepareDC (&dcPrint; &printInfo); 

#0143 

#0144              // check for end of print 

#0145              if (!printInfo。m_bContinuePrinting) 

#0146                  break; 

#0147 

#0148              // write current page 

#0149              TCHAR szBuf'80'; 

#0150              wsprintf(szBuf; strTemp; printInfo。m_nCurPage); 

        

#0151              dlgPrintStatus。SetDlgItemText(AFX_IDC_PRINT_PAGENUM; szBuf); 

#0152 

#0153              // set up drawing rect to entire page (in logical coordinates) 

#0154              printInfo。m_rectDraw。SetRect(0; 0; 

#0155                      dcPrint。GetDeviceCaps(HORZRES); 

#0156                      dcPrint。GetDeviceCaps(VERTRES)); 

#0157              dcPrint。DPtoLP(&printInfo。m_rectDraw); 

#0158 

#0159              // attempt to start the current page 

        

#0160              if (dcPrint。StartPage() 《 0) 

#0161              { 

#0162                  bError = TRUE; 

#0163                  break; 

#0164              } 

#0165 

#0166              // must call OnPrepareDC on newer versions of Windows because 

#0167              // StartPage now resets the device attributes。 

#0168              if (afxData。bMarked4) 

#0169                  OnPrepareDC(&dcPrint; &printInfo); 

#0170 

#0171              ASSERT(printInfo。m_bContinuePrinting); 



                                                                                       675 


…………………………………………………………Page 738……………………………………………………………

                   第篇    深入  MFC  程式設計    



                   #0172 

                   #0173              // page successfully started; so now render the page 

                           

                   #0174              OnPrint (&dcPrint; &printInfo); 

                           

                   #0175              if (dcPrint。EndPage() 《 0 || !_AfxAbortProc(dcPrint。m_hDC; 0)) 

                   #0176              { 

                   #0177                      bError = TRUE; 

                   #0178                      break; 

                   #0179              } 

                   #0180          } 

                   #0181 

                   #0182          // cleanup document printing process 

                   #0183          if (!bError) 

                           

                   #0184              dcPrint。EndDoc(); 

                   #0185          else 

                   #0186              dcPrint。AbortDoc(); 

                   #0187 

                   #0188          AfxGetMainWnd()…》EnableWindow();    // enable main window 

                   #0189 

                           

                   #0190          OnEndPrinting (&dcPrint; &printInfo);    // clean up after printing 

                           

                   #0191          dlgPrintStatus。DestroyWindow(); 

                   #0192 

                           

                   #0193          dcPrint。Detach();   // will be cleaned up by CPrintInfo destructor 

                   #0194      } 

                   #0195  } 



                          图12…4 CView::OnFilePrint 源代码,这是打印命令的第一战场。标出号码 



                                  的是重要动作, 稍后将有补充说明。 



                   以下是CView::OnFilePrint 函数之中重要动作的说明。你可以将这份说明与上一节「列 



                   印动作的背景原理」做一比对,就能够明白MFC 在什么地方为我们做了什么事情,也 



                   才因此能够体会,究竟我们该在什么地方改写虚拟函数,放入我们自己的补强程序代码。 



676 


…………………………………………………………Page 739……………………………………………………………

                                                              第 12 章    列印與預樱А�



  OnFilePrint 首先在堆栈中产生一个CPrintInfo 对象,并构造之,使其部份成员变量拥 



   有初值。CPrintInfo 是一个用来记录打印机资料的结构,其构造式配置了一个Win32 通 



   用打印对话框(mon print dialog)并将它指定给m_pPD : 



 // in AFXEXT。H 

 struct CPrintInfo // Printing information structure 

 { 

         CPrintDialog* m_pPD;     // pointer to print dialog 

         BOOL m_bPreview;         // TRUE if in preview mode 

         BOOL m_bDirect;          // TRUE if bypassing Print Dialog 

         。。。 

 }; 



上述的成员变量m_bPreview 如果是TRUE,表示处于预览模式,FALSE 表示处于打印 



模式;成员变量m_bDirect 如果是TRUE,表示省略【打印】对话框,FALSE 表示需显 



示【打印】对话框。 



上面出现过的CPrintDialog,用来更贴近描述打印对话框: 



 class CPrintDialog : public CmonDialog 

 { 

 public: 

         PRINTDLG& m_pd; 



         BOOL GetDefaults(); 

         LPDEVMODE GetDevMode() const;   // return DEVMODE 

         CString GetDriverName() const;  // return driver name 

         CString GetDeviceName() const;  // return device name 

     
返回目录 上一页 下一页 回到顶部 赞(9) 踩(10)
快捷操作: 按键盘上方向键 ← 或 → 可快速上下翻页 按键盘上的 Enter 键可回到本书目录页 按键盘上方向键 ↑ 可回到本页顶部!
温馨提示: 温看小说的同时发表评论,说出自己的看法和其它小伙伴们分享也不错哦!发表书评还可以获得积分和经验奖励,认真写原创书评 被采纳为精评可以获得大量金币、积分和经验奖励哦!