{
LPCTSTR filename=_T(
"
D:\\1.bmp
");
CPrintDialog printDlg(FALSE);
printDlg.GetDefaults();
//
Or get from user:
//
if (printDlg.DoModal() == IDCANCEL)
//
return;
CDC dc;
if (!dc.Attach(printDlg.GetPrinterDC())) {
AfxMessageBox(_T(
"
No printer founhh nn nd!
"));
return;
}
HFONT hFont,hOldFont;
hFont = CreateFont(
36,
18,
0,
0,FW_NORMAL,FALSE,FALSE, FALSE,GB2312_CHARSET, OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_MODERN,_T(
"
黑体
"));
hOldFont = (HFONT)SelectObject(dc,hFont);
dc.m_bPrinting = TRUE;
DOCINFO di;
//
Initialise print document details
::ZeroMemory (&di,
sizeof (DOCINFO));
di.cbSize =
sizeof (DOCINFO);
di.lpszDocName = filename;
BOOL bPrintingOK = dc.StartDoc(&di);
//
Begin a new print job
//
Get the printing extents
//
and store in the m_rectDraw field of a
//
CPrintInfo object
CPrintInfo Info;
Info.SetMaxPage(
1);
//
just one page
int maxw = dc.GetDeviceCaps(HORZRES);
int maxh = dc.GetDeviceCaps(VERTRES);
Info.m_rectDraw.SetRect(
0,
0, maxw, maxh);
for (UINT page = Info.GetMinPage(); page <=
Info.GetMaxPage() && bPrintingOK; page++) {
dc.StartPage();
//
begin new page
Info.m_nCurPage = page;
CBitmap bitmap;
if(!bitmap.Attach(::LoadImage(
::GetModuleHandle(NULL), filename, IMAGE_BITMAP,
0,
0,
LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE))) {
AfxMessageBox(_T(
"
Error loading bitmap!
"));
return;
}
BITMAP bm;
bitmap.GetBitmap(&bm);
int w = bm.bmWidth;
int h = bm.bmHeight;
//
create memory device context
CDC memDC;
memDC.CreateCompatibleDC(&dc);
CBitmap *pBmp = memDC.SelectObject(&bitmap);
memDC.SetMapMode(dc.GetMapMode());
dc.SetStretchBltMode(HALFTONE);
//
now stretchblt to maximum width on page
dc.StretchBlt(
15,
20,
300,
110, &memDC,
0,
0, w, h, SRCCOPY);
//
textout
HPEN hPen = CreatePen(PS_SOLID,
3, RGB(
0,
0,
0));
HGDIOBJ hOldPen = SelectObject(dc,hPen);
CStringW text;
text =
"
资源编码
";
TextOut(dc,
15,
144+
80,text,text.GetLength());
text=
"
设计单位
";
TextOut(dc,
15,
210+
80,text,text.GetLength());
text=
"
施工单位
";
TextOut(dc,
15,
276+
80,text,text.GetLength());
text=
"
验收单位
";
TextOut(dc,
15,
342+
80,text,text.GetLength());
text=
"
竣工日期
";
TextOut(dc,
15,
408+
80,text,text.GetLength());
//
Line
MoveToEx(dc,
160,
180+
80,NULL);
LineTo(dc,
516,
180+
80);
MoveToEx(dc,
160,
246+
80,NULL);
LineTo(dc,
516,
246+
80);
MoveToEx(dc,
160,
312+
80,NULL);
LineTo(dc,
516,
312+
80);
MoveToEx(dc,
160,
378+
80,NULL);
LineTo(dc,
516,
378+
80);
MoveToEx(dc,
160,
444+
80,NULL);
LineTo(dc,
516,
444+
80);
SelectObject(dc,hOldPen);
DeleteObject(hPen);
//
clean up
memDC.SelectObject(pBmp);
bPrintingOK = (dc.EndPage() >
0);
//
end page
}
if (bPrintingOK)
dc.EndDoc();
//
end a print job
else dc.AbortDoc();
//
abort job.
}