// 打印按钮
void CDlgPrintDlg::OnPrint()
{
int err = -1;
CPrintDialog dlg(FALSE);
if (!dlg.GetDefaults())
AfxMessageBox(_T("You have no default printer!"));
else
{
// attach to the DC we were given
CDC dc;
dc.Attach(dlg.m_pd.hDC);
// for StartDoc
DOCINFO di;
ZeroMemory(&di, sizeof(di));
di.cbSize = sizeof(di);
di.fwType = 0;
di.lpszDatatype = NULL;
di.lpszDocName = "Printing";
err = StartDoc( dc, &di);
if(err <= 0)
return;
err = StartPage( dc);
if(err <= 0)
return;
dc.SetMapMode(MM_HIENGLISH);
CRect rectPrint(0, 0,
dc.GetDeviceCaps(HORZRES),//返回设备的.以毫米为单位的物理显示宽度
dc.GetDeviceCaps(VERTRES));//返回设备的.以毫米为单位的物理显示高度
dc.DPtoLP(&rectPrint);//设备物理单位转化为逻辑单位
dc.SetWindowOrg(-2150, -rectPrint.bottom);//设置原点
// Load picture
HBITMAP hBitmap=(HBITMAP)LoadImage(NULL, "D:\\good.bmp", IMAGE_BITMAP, 0,0,LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
if(hBitmap == NULL)
{
err = GetLastError();
}
CBitmap bitmap;
bitmap.Attach(hBitmap); //关联位图对象
CDC dcimage;
err = dcimage.CreateCompatibleDC(&dc); //内存DC
if(err == 0)
return;
//
dc.CreateCompatibleDC(&dcimage);
BITMAP bmp;
err = bitmap.GetBitmap(&bmp); //获取位图信息
if(err == 0)
return;
dcimage.SelectObject(&bitmap); //选取位图对象
//
err = dc.BitBlt(0,0,bmp.bmWidth,bmp.bmHeight,&dcimage,0,0,SRCCOPY); //显示
err = dc.StretchBlt(/*dc.m_hDC,*/ 100, 1000,4096,4096*1.5, &dcimage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
if(err == 0)
{
err = GetLastError();
return;
}
//end load picture
//
dc.TextOut(500,500, "Printer:张世龙");
dc.EndPage(); //通知打印机驱动程序页结束
dc.EndDoc();//通知打印机驱动程序打印完毕
DeleteDC(dc.Detach());
}
}