最近要做打印,所以需要打印的数据 都会绘制一在个缓冲图上
,在网上找了此资料整理了一下.
随便求大神,
系统有没有这种API.调这个API直接能出现打印预览,我把这个缓冲图传给他,然后就由系统进行打印了?
DWORD dwNeeded, dwReturned;
EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 5, NULL, 0, &dwNeeded, &dwReturned);
PRINTER_INFO_5* prninfo = (PRINTER_INFO_5*)GlobalAlloc(GPTR, dwNeeded);
if (EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 5, (LPBYTE)prninfo, dwNeeded, &dwNeeded, &dwReturned))
{
创建打印DC
m_PrinterDC = CreateDC(NULL,prninfo[0].pPrinterName, NULL, NULL) ;
}
bool IPrinter::PrinteImage(Gdiplus::Image* pImg)
{
DOCINFO docInfo;
docInfo.cbSize = sizeof(docInfo) ;
docInfo.lpszDocName = (LPCWSTR)"NJ3a";
docInfo.lpszOutput = NULL;
docInfo.lpszDatatype = NULL;
docInfo.fwType = 0;
int result = StartDoc(m_PrinterDC, &docInfo) ;
if (result <= 0)
{
MessageBox(0, L"StartDoc() failed" ,
L"Basic Print App" , MB_OK | MB_ICONERROR) ;
}
// 第3步:调用StartPage()
result = StartPage(m_PrinterDC) ;
if (result <= 0)
{
MessageBox(0, L"StartPage() failed" ,
L"Basic Print App" , MB_OK | MB_ICONERROR) ;
return false;
}
// 第4步:打印数据
//Rectangle(m_PrinterDC, 20, 20, 355, 417) ;
//int w=pImg->GetWidth();
//int h=pImg->GetHeight()-40;
//HDC hdcsrc=CreateCompatibleDC(m_PrinterDC);
//SelectObject(hdcsrc,pImg);
//BitBlt(m_PrinterDC,0,0,w,h,hdcsrc,0,0,SRCCOPY);
Gdiplus::Graphics g(m_PrinterDC);
Gdiplus:: ImageAttributes imageAttr;
imageAttr.SetWrapMode(Gdiplus::WrapModeTile);
Gdiplus::RectF destDrawRect(0,0,pImg->GetWidth(),pImg->GetHeight()-40);
Gdiplus::RectF srcRect(0,0,pImg->GetWidth(),pImg->GetHeight()-40);
g.DrawImage(pImg,destDrawRect,srcRect.X,srcRect.Y,srcRect.Width,srcRect.Height,Gdiplus::UnitPixel,&imageAttr);
// 第5步:调用EndPage()
result = EndPage(m_PrinterDC) ;
if (result <= 0)
{
MessageBox(0, L"EndPage() failed" ,
L"Basic Print App" , MB_OK | MB_ICONERROR) ;
}
// 第6步:调用EndDOC()
EndDoc(m_PrinterDC) ;
MessageBox(0, L"Document printed" , L"Basic Print App" ,
MB_OK | MB_ICONINFORMATION) ;
return true;
}