第一种
CPrintDialog m_PrintDlg(FALSE); CDC *pPrintDC; if (AfxGetApp()->GetPrinterDeviceDefaults(&m_PrintDlg.m_pd) == 0) { AfxMessageBox(L"没有安装打印机驱动程序或安装的打印机驱动程序出现问题."); pPrintDC = NULL; return; } pPrintDC = new CDC; pPrintDC->Attach(m_PrintDlg.CreatePrinterDC()); if (pPrintDC){ MessageBox(L"成功。"); }第二种
CString str; CString strPrinter[3]; LPWSTR szprinter = (LPWSTR)(LPCTSTR)str; // 定义一个设备环境句柄 HDC hdcprint; //定义一个打印作业 static DOCINFO di = { sizeof(DOCINFO), L"printer", NULL }; // 得到设备字符串存入数组szprinter中 GetProfileStringW(L"windows", L"device", L",,,",szprinter, 80); //拆分字符串 CString output = L""; for (int i = 0; i<3; i++) { AfxExtractSubString(output, str, i, ','); strPrinter[i]=output; } MessageBox(str); MessageBox(strPrinter[0]); MessageBox(strPrinter[1]); MessageBox(strPrinter[2]); //创建DC if ((hdcprint = CreateDC(strPrinter[1], strPrinter[0], strPrinter[2], NULL)) != 0) { MessageBox(L"设备环境获取成功。"); } 第三种
CPrintDialog print(false); if (print.DoModal() == IDOK) { CDC printed; printed.Attach(print.GetPrinterDC()); if (printed){ MessageBox(L"成功"); } printed.Detach(); } 第四种 //选择打印机对话框 CDC Dc; HDC hdcPrint = NULL; CPrintDialog dlg(FALSE); if (0)
//打印按钮,不弹出选择对话框,获取默认打印设备 { PRINTDLG printInfo; ZeroMemory(&printInfo, sizeof(printInfo));
//清空该结构 printInfo.lStructSize = sizeof(printInfo); printInfo.hwndOwner = 0; printInfo.hDevMode = 0; printInfo.hDevNames = 0; //这个是关键,PD_RETURNDC 如果不设这个标志,就拿不到hDC了 //PD_RETURNDEFAULT 这个就是得到默认打印机,不需要弹出设置对话框 printInfo.Flags = PD_RETURNDC | PD_RETURNDEFAULT | PD_ALLPAGES; PrintDlg(&printInfo);//调用API拿出默认打印机 DWORD rst = CommDlgExtendedError();//看看出错没有 if (rst != 0) {//出错了,清空标志再次调用API,此时就会弹出打印设置对话框供用户选择了 printInfo.Flags = 0; PrintDlg(&printInfo); } hdcPrint = printInfo.hDC; //得到打印DC,输出到打印 } else //弹出对话框选择打印设备 { dlg.DoModal(); hdcPrint = dlg.GetPrinterDC(); } if (hdcPrint == NULL) { MessageBox(_T("打印机初始化失败!")); return; } Dc.Attach(hdcPrint); if (Dc){ MessageBox(L"ok"); }
|
|