按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
#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 = printIn