vc设置打印机模式并预览
{
::PrinterProperties(m_hWnd, hPrint);
::ClosePrinter(hPrint);
}
BOOL bl = ::EnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, NULL, 4, NULL, 0,&dwNeeded, &dwReturn);
PRINTER_INFO_4* p4;
p4 = (PRINTER_INFO_4*)malloc(dwNeeded);
::EnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, NULL, 4, (PBYTE)p4, dwNeeded,&dwNeeded, &dwReturn);
for(int i=0;i< div> < dwNeeded;i++)
CString strDefualtDev = TEXT("");
PRINTDLG pd;
LPDEVMODE lpDevMode;
if(AfxGetApp()->GetPrinterDeviceDefaults(&pd))
{
lpDevMode=(LPDEVMODE)GlobalLock(pd.hDevMode);
if(lpDevMode)
{
strDefualtDev = lpDevMode->dmDeviceName;
}
GlobalUnlock(pd.hDevMode);
}
::SetDefaultPrinter("Adobe PDF"); // 在这里我选择的打印机是选定的Adobe虚拟打印机
if(AfxGetApp()->GetPrinterDeviceDefaults(&pd))
{
lpDevMode=(LPDEVMODE)GlobalLock(pd.hDevMode);
if(lpDevMode)
{
lpDevMode->dmPaperSize=DMPAPER_A4; // 设定打印纸张幅面
lpDevMode->dmOrientation=DMORIENT_LANDSCAPE; // 设定横向打印
lpDevMode->dmPrintQuality=600; // 设定打印机分辨率
}
GlobalUnlock(pd.hDevMode);
}
memset( &di, 0, sizeof(DOCINFO) );
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = "Bitmap Printing Test"; // 设定打印文档名称
di.lpszOutput = (LPTSTR) NULL;
di.fwType = 0;
// Begin a print job by calling the StartDoc function.
int nError;
if(hdc != NULL)
{
if((nError = StartDoc(hdc, &di)) > 0)
{
if(StartPage(hdc)>0)
{
int xPage = ::GetDeviceCaps(hdc, HORZRES); // 得到可打印纸张的高度
int yPage = ::GetDeviceCaps(hdc, VERTRES); // 得到可打印纸张的宽度
// 在一下地方进行打印输出
EndDoc(hdc); // 关闭打印文档
}
}
}
::SetDefaultPrinter(strDefualtDev);
/////////////////////////开始调取打印机-operation 001 ////////////////////////////////////
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;
PrintDlg(&printInfo);//调用API拿出默认打印机
DWORD rst = CommDlgExtendedError();//看看出错没有
if(rst != 0)
{//出错了,清空标志再次调用API,此时就会弹出打印设置对话框供用户选择了
MessageBox("默认打印机调取失败,请手动选择!");
printInfo.Flags = 0;
PrintDlg(&printInfo);
}
////////////////////////////001- end ////////////////////////////////////////////
////////////////////自定义纸张大小//////////////////////////////////////////////////
/*
//首先得到DEVMODE结构,同时申请系统锁定,我们要修改它了
LPDEVMODE lpDevMode = (LPDEVMODE)::GlobalLock(printInfo.hDevMode);
if(lpDevMode) //成功得到该结构了
{
lpDevMode->dmPaperSize = DMPAPER_USER; //自定义纸张
//设定自定义纸张标志
lpDevMode->dmFields = lpDevMode ->dmFields | DM_PAPERSIZE | DM_PAPERLENGTH | DM_PAPERWIDTH;
lpDevMode->dmPaperWidth = (short)width; //纸宽多少
lpDevMode->dmPaperLength = (short)height; //纸高多少
//顺便设定纸张走纸方向,横向还是纵向
lpDevMode->dmOrientation = DMORIENT_PORTRAIT;
}
//记住要解锁
GlobalUnlock(printInfo.hDevMode);
//这个也不能忘了,否则更改不会生效
ResetDC(printInfo.hDC, lpDevMode);
*/
HDC printDC=printInfo.hDC; //得到打印DC,输出到打印,
CDC cdc;
cdc.Attach(printDC);
得到了 dc之后就不用我再说了吧,自己搞定就是了,
————————————————————————————————————————————————
void PrintTest()
{
CPrintDialog pd(FALSE, PD_ALLPAGES | PD_COLLATE | PD_NOPAGENUMS |
PD_HIDEPRINTTOFILE);
if(pd.GetDefaults())
{
DOCINFO di;
memset( &di, 0, sizeof(DOCINFO) );
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = "Printing Test";
di.lpszOutput = (LPTSTR) NULL;
di.fwType = 0;
HDC hDC=pd.CreatePrinterDC();
CDC dc;
dc.Attach(hDC);
dc.StartDoc(&di);
dc.StartPage();
dc.MoveTo(0,0);
dc.LineTo(1000,1000);
dc.EndPage();
dc.EndDoc();
dc.DeleteDC();
}
}
HDC hdcPrint = GetDefaultPrinterDC();
CPrintDialog pd(FALSE,PD_ALLPAGES|PD_ALLPAGES|PD_NOPAGENUMS,NULL);
pd.DoModal();
HDC hdcPrint = pd.CreatePrinterDC();
if(hdcPrint == NULL)
{
MessageBox("打印机初始化失败!", "错误", MB_ICONSTOP);
return;
}
/* 设置系统打印模式
#define DMORIENT_PORTRAIT 1 //纵向
#define DMORIENT_LANDSCAPE 2 //横向
*/
/************************************************************************/
void CXTReportApp::SetLandscapeMode(int PrintMode)
{
PRINTDLG pd;
pd.lStructSize=(DWORD)sizeof(PRINTDLG);
BOOL bRet=GetPrinterDeviceDefaults(&pd);
if(bRet)
{
// protect memory handle with ::GlobalLock and ::GlobalUnlock
DEVMODE FAR *pDevMode=(DEVMODE FAR *)::GlobalLock(m_hDevMode);
// set orientation to landscape
if(PrintMode==1)//纵向打印
pDevMode->dmOrientation=DMORIENT_PORTRAIT;
else if(PrintMode==2)//横向打印
pDevMode->dmOrientation=DMORIENT_LANDSCAPE;
::GlobalUnlock(m_hDevMode);
}
}
extern void DrawInfo(CDC &memDC, PRNINFO PrnInfo);
extern UINT m_nCxSelected ;
BOOL PrintSetup(HWND hParent, BOOL bDefault = FALSE);
BOOL Print(HWND hParent, LPCTSTR lpszDocName = "PrintListCtrl", BOOL bDefault = FALSE);
BOOL CPrintListCtrl::PrintSetup(HWND hParent, BOOL bDefault)
{
PRINTDLG stPD;
memset(&stPD, 0, sizeof(stPD));
stPD.lStructSize = sizeof(stPD);
stPD.hwndOwner = hParent;
stPD.Flags = PD_ALLPAGES | PD_DISABLEPRINTTOFILE |
PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
stPD.hDevNames = NULL;
stPD.hDevMode = bDefault ? NULL : m_hDevMode;
if (bDefault)
{
stPD.Flags |= PD_RETURNDEFAULT;
}
BOOL bRet = PrintDlg(&stPD);
if (bRet == FALSE && CommDlgExtendedError()) return FALSE;
if (bRet == FALSE && !CommDlgExtendedError()) return TRUE;
ClearPrintDC();
m_hPrintDC = stPD.hDC;
return (m_hPrintDC != NULL);
}