16,472
社区成员
void CImageTextDlg:: PrintJpegImage()
{
TCHAR szPrinter[_MAX_PATH];
int cntChars = _MAX_PATH;
GetDefaultPrinter(szPrinter,cntChars);
BOOL b = SetDefaultPrinter(szPrinter);
CPrintDialog printDlg(FALSE);
printDlg.GetDefaults();
// is a default printer set up?
HDC hdcPrinter = printDlg.GetPrinterDC();
if (hdcPrinter == NULL)
{
MessageBox(_T("Buy a printer!"));
}
CDC prtrDC;
BOOL b11 = prtrDC.Attach(printDlg.GetPrinterDC());
prtrDC.m_bPrinting = TRUE;
DOCINFO imageInfo;
::ZeroMemory(&imageInfo,sizeof(DOCINFO));
imageInfo.cbSize = sizeof(DOCINFO);
imageInfo.lpszDocName = "d:\\test.bmp"; // 要打印的文件名
BOOL bPrintingOK = prtrDC.StartDoc(&imageInfo);
CPrintInfo oInfo;
oInfo.SetMaxPage(1); // 只有一页
int nMaxWidth = prtrDC.GetDeviceCaps(HORZRES);
int nMaxHeight = prtrDC.GetDeviceCaps(VERTRES);
oInfo.m_rectDraw.SetRect(0 ,0,nMaxWidth,nMaxHeight) ;
for (UINT page = oInfo.GetMinPage(); page<= oInfo.GetMaxPage()&& bPrintingOK; page++)
{
prtrDC.StartPage(); // 开始新页
oInfo.m_nCurPage = page;
CBitmap bitmap;
HANDLE hd = ::LoadImage(::GetModuleHandle(NULL),"d:\\test.bmp",
IMAGE_BITMAP, 0, 0 ,LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
if (!bitmap.Attach(hd))
{
AfxMessageBox(_T("加载位图时出错"));
return ;
}
BITMAP bm;
bitmap.GetBitmap(& bm);
int nWidth = bm.bmWidth;
int nHeight = bm.bmHeight;
// 创建内存设备上下文
CDC memDC;
memDC.CreateCompatibleDC(&prtrDC);
CBitmap * pBmp = memDC.SelectObject(&bitmap);
memDC.SetMapMode(prtrDC.GetMapMode());
prtrDC.SetStretchBltMode(HALFTONE);
prtrDC.StretchBlt(0 ,0,nMaxWidth,nMaxHeight,&memDC,0,0,nWidth,nHeight,SRCCOPY);
memDC.SelectObject(pBmp);
bPrintingOK =(prtrDC.EndPage()> 0 ); // 结束页
}
if (bPrintingOK)
prtrDC.EndDoc(); // 结束打印作业
else
prtrDC.AbortDoc(); // 中止作业。
SetDefaultPrinter(szPrinter);
}