void CBCGPRibbonBackstagePagePrint::OnPageChanged() { if (m_wndPreview->GetSafeHwnd () != NULL) { CPrintInfo* pPrintInfo = m_wndPreview->GetPrintInfo (); CString strPage; CString strFmt; if (!AfxExtractSubString (strFmt, pPrintInfo->m_strPageDesc, 0)) { strFmt = _T("%d"); } strPage.Format(strFmt, m_wndPreview->GetCurrentPage ()); m_wndPageNum.SetWindowText (strPage); m_btnPrev.EnableWindow (m_wndPreview->GetCurrentPage () > pPrintInfo->GetMinPage ()); m_btnNext.EnableWindow (m_wndPreview->GetCurrentPage () < pPrintInfo->GetMaxPage ()); } else { m_btnPrev.EnableWindow (FALSE); m_btnNext.EnableWindow (FALSE); m_wndPageNum.SetWindowText (_T("")); m_wndPageNum.EnableWindow (FALSE); } }
BOOL CEdRptDoc::PrintAuto(CPrintDialog *pPntDlg) { // 严禁在不使用省却打印机情况下不指定打印机 ASSERT(m_bDefaultPrint || pPntDlg); // get default print CPrintDialog defaultDlg (FALSE, PD_RETURNDC ); AfxGetApp()->GetPrinterDeviceDefaults( &defaultDlg.m_pd ); if (!pPntDlg) pPntDlg = &defaultDlg; HDC hdc = pPntDlg->CreatePrinterDC(); ASSERT(hdc); CDC dc; dc.Attach( hdc ); dc.m_bPrinting = TRUE; CString strTitle = m_szTitle; if (strTitle.IsEmpty()) strTitle.LoadString(AFX_IDS_APP_TITLE); DOCINFO di; // Initialise print doc details memset(&di, 0, sizeof (DOCINFO)); di.cbSize = sizeof (DOCINFO); di.lpszDocName = strTitle; BOOL bPrintingOK = dc.StartDoc(&di); // Begin a new print job CPrintInfo Info; Info.m_rectDraw.SetRect(0,0, dc.GetDeviceCaps(HORZRES), dc.GetDeviceCaps(VERTRES)); m_Grid.OnBeginPrinting(&dc, &Info); // Initialise printing m_Grid.SetFocus(); for (UINT page = Info.GetMinPage(); page <= Info.GetMaxPage() && bPrintingOK; page++) { dc.StartPage(); // begin new page Info.m_nCurPage = page; m_Grid.OnPrint(&dc, &Info); // Print page bPrintingOK = (dc.EndPage() > 0); // end page } m_Grid.OnEndPrinting(&dc, &Info); // Clean up after printing if (bPrintingOK) dc.EndDoc(); // end a print job else dc.AbortDoc(); // abort job. dc.Detach(); // detach the printer DC DeleteDC(hdc); return TRUE; }
// @pymethod |PyCPrintInfo|GetToPage|The number of the last page to be printed. // @pyseemfc CPrintInfo|GetToPage static PyObject *ui_get_to_page(PyObject * self, PyObject * args) { CHECK_NO_ARGS2(args, GetToPage); CPrintInfo *pInfo = ui_prinfo_object::GetPrintInfo(self); if (!pInfo) return NULL; GUI_BGN_SAVE; UINT ret = pInfo->GetToPage(); GUI_END_SAVE; return Py_BuildValue ("i", ret); }
// @pymethod |PyCPrintInfo|SetMaxPage|Set the number of the last page of the document. // @pyseemfc CPrintInfo|SetMaxPage static PyObject *ui_set_max_page(PyObject * self, PyObject * args) { int var; if (!PyArg_ParseTuple(args,"i:SetMaxPage",&var)) return NULL; CPrintInfo *pInfo = ui_prinfo_object::GetPrintInfo(self); if (!pInfo) return NULL; GUI_BGN_SAVE; pInfo->SetMaxPage(var); GUI_END_SAVE; RETURN_NONE; }
void CChartCtrl::Print(const TChartString& strTitle, CPrintDialog* pPrntDialog) { //AFX_MANAGE_STATE(AfxGetStaticModuleState()); CDC dc; if (pPrntDialog == NULL) { CPrintDialog printDlg(FALSE); if (printDlg.DoModal() != IDOK) // Get printer settings from user return; dc.Attach(printDlg.GetPrinterDC()); // attach a printer DC } else dc.Attach(pPrntDialog->GetPrinterDC()); // attach a printer DC dc.m_bPrinting = TRUE; DOCINFO di; // Initialise print doc details memset(&di, 0, sizeof (DOCINFO)); di.cbSize = sizeof (DOCINFO); di.lpszDocName = strTitle.c_str(); BOOL bPrintingOK = dc.StartDoc(&di); // Begin a new print job CPrintInfo Info; Info.m_rectDraw.SetRect(0,0, dc.GetDeviceCaps(HORZRES), dc.GetDeviceCaps(VERTRES)); OnBeginPrinting(&dc, &Info); // Initialise printing for (UINT page = Info.GetMinPage(); page <= Info.GetMaxPage() && bPrintingOK; page++) { dc.StartPage(); // begin new page Info.m_nCurPage = page; OnPrint(&dc, &Info); // Print page bPrintingOK = (dc.EndPage() > 0); // end page } OnEndPrinting(&dc, &Info); // Clean up after printing if (bPrintingOK) dc.EndDoc(); // end a print job else dc.AbortDoc(); // abort job. dc.Detach(); // detach the printer DC }
void CView::OnFilePrint() { // get default print info CPrintInfo printInfo; ASSERT(printInfo.m_pPD != NULL); // must be set if (LOWORD(GetCurrentMessage()->wParam) == ID_FILE_PRINT_DIRECT) { CCommandLineInfo* pCmdInfo = AfxGetApp()->m_pCmdInfo; if (pCmdInfo != NULL) { if (pCmdInfo->m_nShellCommand == CCommandLineInfo::FilePrintTo) { printInfo.m_pPD->m_pd.hDC = ::CreateDC(pCmdInfo->m_strDriverName, pCmdInfo->m_strPrinterName, pCmdInfo->m_strPortName, NULL); if (printInfo.m_pPD->m_pd.hDC == NULL) { AfxMessageBox(AFX_IDP_FAILED_TO_START_PRINT); return; } } } printInfo.m_bDirect = TRUE; } if (OnPreparePrinting(&printInfo)) { // hDC must be set (did you remember to call DoPreparePrinting?) ASSERT(printInfo.m_pPD->m_pd.hDC != NULL); // gather file to print to if print-to-file selected CString strOutput; if (printInfo.m_pPD->m_pd.Flags & PD_PRINTTOFILE && !printInfo.m_bDocObject) { // construct CFileDialog for browsing CString strDef(MAKEINTRESOURCE(AFX_IDS_PRINTDEFAULTEXT)); CString strPrintDef(MAKEINTRESOURCE(AFX_IDS_PRINTDEFAULT)); CString strFilter(MAKEINTRESOURCE(AFX_IDS_PRINTFILTER)); CString strCaption(MAKEINTRESOURCE(AFX_IDS_PRINTCAPTION)); CFileDialog dlg(FALSE, strDef, strPrintDef, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, strFilter, NULL, 0); dlg.m_ofn.lpstrTitle = strCaption; if (dlg.DoModal() != IDOK) return; // set output device to resulting path name strOutput = dlg.GetPathName(); } // set up document info and start the document printing process CString strTitle; CDocument* pDoc = GetDocument(); if (pDoc != NULL) strTitle = pDoc->GetTitle(); else EnsureParentFrame()->GetWindowText(strTitle); DOCINFO docInfo; memset(&docInfo, 0, sizeof(DOCINFO)); docInfo.cbSize = sizeof(DOCINFO); docInfo.lpszDocName = strTitle; CString strPortName; if (strOutput.IsEmpty()) { docInfo.lpszOutput = NULL; strPortName = printInfo.m_pPD->GetPortName(); } else { docInfo.lpszOutput = strOutput; AfxGetFileTitle(strOutput, strPortName.GetBuffer(_MAX_PATH), _MAX_PATH); } // setup the printing DC CDC dcPrint; if (!printInfo.m_bDocObject) { dcPrint.Attach(printInfo.m_pPD->m_pd.hDC); // attach printer dc dcPrint.m_bPrinting = TRUE; } OnBeginPrinting(&dcPrint, &printInfo); if (!printInfo.m_bDocObject) dcPrint.SetAbortProc(_AfxAbortProc); // disable main window while printing & init printing status dialog // Store the Handle of the Window in a temp so that it can be enabled // once the printing is finished CWnd * hwndTemp = AfxGetMainWnd(); hwndTemp->EnableWindow(FALSE); CPrintingDialog dlgPrintStatus(this); CString strTemp; dlgPrintStatus.SetDlgItemText(AFX_IDC_PRINT_DOCNAME, strTitle); dlgPrintStatus.SetDlgItemText(AFX_IDC_PRINT_PRINTERNAME, printInfo.m_pPD->GetDeviceName()); dlgPrintStatus.SetDlgItemText(AFX_IDC_PRINT_PORTNAME, strPortName); dlgPrintStatus.ShowWindow(SW_SHOW); dlgPrintStatus.UpdateWindow(); // start document printing process if (!printInfo.m_bDocObject) { printInfo.m_nJobNumber = dcPrint.StartDoc(&docInfo); if (printInfo.m_nJobNumber == SP_ERROR) { // enable main window before proceeding hwndTemp->EnableWindow(TRUE); // cleanup and show error message OnEndPrinting(&dcPrint, &printInfo); dlgPrintStatus.DestroyWindow(); dcPrint.Detach(); // will be cleaned up by CPrintInfo destructor AfxMessageBox(AFX_IDP_FAILED_TO_START_PRINT); return; } } // Guarantee values are in the valid range UINT nEndPage = printInfo.GetToPage(); UINT nStartPage = printInfo.GetFromPage(); if (nEndPage < printInfo.GetMinPage()) nEndPage = printInfo.GetMinPage(); if (nEndPage > printInfo.GetMaxPage()) nEndPage = printInfo.GetMaxPage(); if (nStartPage < printInfo.GetMinPage()) nStartPage = printInfo.GetMinPage(); if (nStartPage > printInfo.GetMaxPage()) nStartPage = printInfo.GetMaxPage(); int nStep = (nEndPage >= nStartPage) ? 1 : -1; nEndPage = (nEndPage == 0xffff) ? 0xffff : nEndPage + nStep; VERIFY(strTemp.LoadString(AFX_IDS_PRINTPAGENUM)); // If it's a doc object, we don't loop page-by-page // because doc objects don't support that kind of levity. BOOL bError = FALSE; if (printInfo.m_bDocObject) { OnPrepareDC(&dcPrint, &printInfo); OnPrint(&dcPrint, &printInfo); } else { // begin page printing loop for (printInfo.m_nCurPage = nStartPage; printInfo.m_nCurPage != nEndPage; printInfo.m_nCurPage += nStep) { OnPrepareDC(&dcPrint, &printInfo); // check for end of print if (!printInfo.m_bContinuePrinting) break; // write current page TCHAR szBuf[80]; ATL_CRT_ERRORCHECK_SPRINTF(_sntprintf_s(szBuf, _countof(szBuf), _countof(szBuf) - 1, strTemp, printInfo.m_nCurPage)); dlgPrintStatus.SetDlgItemText(AFX_IDC_PRINT_PAGENUM, szBuf); // set up drawing rect to entire page (in logical coordinates) printInfo.m_rectDraw.SetRect(0, 0, dcPrint.GetDeviceCaps(HORZRES), dcPrint.GetDeviceCaps(VERTRES)); dcPrint.DPtoLP(&printInfo.m_rectDraw); // attempt to start the current page if (dcPrint.StartPage() < 0) { bError = TRUE; break; } // must call OnPrepareDC on newer versions of Windows because // StartPage now resets the device attributes. OnPrepareDC(&dcPrint, &printInfo); ASSERT(printInfo.m_bContinuePrinting); // page successfully started, so now render the page OnPrint(&dcPrint, &printInfo); // If the user restarts the job when it's spooling, all // subsequent calls to EndPage returns < 0. The first time // GetLastError returns ERROR_PRINT_CANCELLED if (dcPrint.EndPage() < 0 && (GetLastError()!= ERROR_SUCCESS)) { HANDLE hPrinter; if (!OpenPrinter(LPTSTR(printInfo.m_pPD->GetDeviceName().GetBuffer()), &hPrinter, NULL)) { bError = TRUE; break; } DWORD cBytesNeeded; if(!GetJob(hPrinter,printInfo.m_nJobNumber,1,NULL,0,&cBytesNeeded)) { if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { bError = TRUE; break; } } JOB_INFO_1 *pJobInfo; if((pJobInfo = (JOB_INFO_1 *)malloc(cBytesNeeded))== NULL) { bError = TRUE; break; } DWORD cBytesUsed; BOOL bRet = GetJob(hPrinter,printInfo.m_nJobNumber,1,LPBYTE(pJobInfo),cBytesNeeded,&cBytesUsed); DWORD dwJobStatus = pJobInfo->Status; free(pJobInfo); pJobInfo = NULL; // if job status is restart, just continue if(!bRet || !(dwJobStatus & JOB_STATUS_RESTART) ) { bError = TRUE; break; } } if(!_AfxAbortProc(dcPrint.m_hDC, 0)) { bError = TRUE; break; } } } // cleanup document printing process if (!printInfo.m_bDocObject) { if (!bError) dcPrint.EndDoc(); else dcPrint.AbortDoc(); } hwndTemp->EnableWindow(); // enable main window OnEndPrinting(&dcPrint, &printInfo); // clean up after printing dlgPrintStatus.DestroyWindow(); dcPrint.Detach(); // will be cleaned up by CPrintInfo destructor } }
void CPrint::OnPrint() { if (ParentWnd == NULL) return; // get default print info CPrintInfo printInfo; ASSERT(printInfo.m_pPD != NULL); // must be set /* if (LOWORD(GetCurrentMessage()->wParam) == ID_FILE_PRINT_DIRECT) { CCommandLineInfo* pCmdInfo = AfxGetApp()->m_pCmdInfo; if (pCmdInfo != NULL) { if (pCmdInfo->m_nShellCommand == CCommandLineInfo::FilePrintTo) { printInfo.m_pPD->m_pd.hDC = ::CreateDC(pCmdInfo->m_strDriverName, pCmdInfo->m_strPrinterName, pCmdInfo->m_strPortName, NULL); if (printInfo.m_pPD->m_pd.hDC == NULL) { AfxMessageBox(AFX_IDP_FAILED_TO_START_PRINT); return; } } } printInfo.m_bDirect = TRUE; } */ if (OnPreparePrinting(&printInfo)) { // hDC must be set (did you remember to call DoPreparePrinting?) ASSERT(printInfo.m_pPD->m_pd.hDC != NULL); // gather file to print to if print-to-file selected CString strOutput; if (printInfo.m_pPD->m_pd.Flags & PD_PRINTTOFILE && !printInfo.m_bDocObject) { // construct CFileDialog for browsing CString strDef(MAKEINTRESOURCE(AFX_IDS_PRINTDEFAULTEXT)); CString strPrintDef(MAKEINTRESOURCE(AFX_IDS_PRINTDEFAULT)); CString strFilter(MAKEINTRESOURCE(AFX_IDS_PRINTFILTER)); CString strCaption(MAKEINTRESOURCE(AFX_IDS_PRINTCAPTION)); CFileDialog dlg(FALSE, strDef, strPrintDef, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, strFilter); dlg.m_ofn.lpstrTitle = strCaption; if (dlg.DoModal() != IDOK) return; // set output device to resulting path name strOutput = dlg.GetPathName(); } // set up document info and start the document printing process DOCINFO docInfo; memset(&docInfo, 0, sizeof(DOCINFO)); docInfo.cbSize = sizeof(DOCINFO); docInfo.lpszDocName = PrintTitle; CString strPortName; int nFormatID; if (strOutput.IsEmpty()) { docInfo.lpszOutput = NULL; strPortName = printInfo.m_pPD->GetPortName(); nFormatID = AFX_IDS_PRINTONPORT; } else { docInfo.lpszOutput = strOutput; strPortName = "Miracle"; /*AfxGetFileTitle(strOutput, strPortName.GetBuffer(_MAX_PATH), _MAX_PATH);*/ nFormatID = AFX_IDS_PRINTTOFILE; } // setup the printing DC CDC dcPrint; if (!printInfo.m_bDocObject) { dcPrint.Attach(printInfo.m_pPD->m_pd.hDC); // attach printer dc dcPrint.m_bPrinting = TRUE; } OnBeginPrinting(&dcPrint, &printInfo); if (!printInfo.m_bDocObject) dcPrint.SetAbortProc(AbortProc); // disable main window while printing & init printing status dialog AfxGetMainWnd()->EnableWindow(FALSE); CPrintingDialog dlgPrintStatus(ParentWnd); ghwndAbort = dlgPrintStatus.m_hWnd; CString strTemp; dlgPrintStatus.SetDlgItemText(AFX_IDC_PRINT_DOCNAME, PrintTitle); dlgPrintStatus.SetDlgItemText(AFX_IDC_PRINT_PRINTERNAME, printInfo.m_pPD->GetDeviceName()); AfxFormatString1(strTemp, nFormatID, strPortName); dlgPrintStatus.SetDlgItemText(AFX_IDC_PRINT_PORTNAME, strTemp); dlgPrintStatus.ShowWindow(SW_SHOW); dlgPrintStatus.UpdateWindow(); // start document printing process if (!printInfo.m_bDocObject && dcPrint.StartDoc(&docInfo) == SP_ERROR) { // enable main window before proceeding AfxGetMainWnd()->EnableWindow(TRUE); // cleanup and show error message OnEndPrinting(&dcPrint, &printInfo); dlgPrintStatus.DestroyWindow(); dcPrint.Detach(); // will be cleaned up by CPrintInfo destructor AfxMessageBox(AFX_IDP_FAILED_TO_START_PRINT); return; } // Guarantee values are in the valid range UINT nEndPage = printInfo.GetToPage(); UINT nStartPage = printInfo.GetFromPage(); if (nEndPage < printInfo.GetMinPage()) nEndPage = printInfo.GetMinPage(); if (nEndPage > printInfo.GetMaxPage()) nEndPage = printInfo.GetMaxPage(); if (nStartPage < printInfo.GetMinPage()) nStartPage = printInfo.GetMinPage(); if (nStartPage > printInfo.GetMaxPage()) nStartPage = printInfo.GetMaxPage(); int nStep = (nEndPage >= nStartPage) ? 1 : -1; nEndPage = (nEndPage == 0xffff) ? 0xffff : nEndPage + nStep; VERIFY(strTemp.LoadString(AFX_IDS_PRINTPAGENUM)); // If it's a doc object, we don't loop page-by-page // because doc objects don't support that kind of levity. BOOL bError = FALSE; // Print Loop // Burada DocObject kýsmý silindi { // begin page printing loop for (printInfo.m_nCurPage = nStartPage; printInfo.m_nCurPage != nEndPage; printInfo.m_nCurPage += nStep) { OnPrepareDC(&dcPrint, &printInfo); // check for end of print if (!printInfo.m_bContinuePrinting) break; // write current page TCHAR szBuf[80]; wsprintf(szBuf, strTemp, printInfo.m_nCurPage); dlgPrintStatus.SetDlgItemText(AFX_IDC_PRINT_PAGENUM, szBuf); // set up drawing rect to entire page (in logical coordinates) printInfo.m_rectDraw.SetRect(0, 0, dcPrint.GetDeviceCaps(HORZRES), dcPrint.GetDeviceCaps(VERTRES)); dcPrint.DPtoLP(&printInfo.m_rectDraw); // attempt to start the current page if (dcPrint.StartPage() < 0) { DebugMessage("Error on print 2"); bError = TRUE; break; } // must call OnPrepareDC on newer versions of Windows because // StartPage now resets the device attributes. //if (afxData.bMarked4) OnPrepareDC(&dcPrint, &printInfo); ASSERT(printInfo.m_bContinuePrinting); // page successfully started, so now render the page OnPrint(&dcPrint, &printInfo); TRACE("on print\n"); int stat = dcPrint.EndPage(); if (stat < 0 || !AbortProc(dcPrint.m_hDC, 0)) { bError = TRUE; CString str; str.Format("End Page = %ld,Abort = %ld",stat,gbAbort); DebugMessage(str); DebugMessage("Error on print"); break; } } } // cleanup document printing process if (!printInfo.m_bDocObject) { if (!bError) dcPrint.EndDoc(); else dcPrint.AbortDoc(); } AfxGetMainWnd()->EnableWindow(); // enable main window OnEndPrinting(&dcPrint, &printInfo); // clean up after printing dlgPrintStatus.DestroyWindow(); dcPrint.Detach(); // will be cleaned up by CPrintInfo destructor } }