各位,请帮忙看下以下代码:
打印预览可以用,但是到打印机上却打印不出来,是空白的纸。
CDC* pDC = (CDC*)wParam;
CPrintInfo* pInfo = (CPrintInfo *)lParam;
int nPageNumber = pInfo->m_nCurPage;
HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, "res\\temp.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
BITMAP bitmap;
::GetObject(hBitmap,sizeof(BITMAP),&bitmap);
double dScale=(double)m_cxWidth/bitmap.bmWidth;
int nScaledWidth=m_cxWidth;
int nScaledHeight=(int)(bitmap.bmHeight*dScale);
HDC dcMem;
dcMem=::CreateCompatibleDC(pDC->m_hDC);
HBITMAP hOldBmp=(HBITMAP)::SelectObject(dcMem,hBitmap);
int nVertCenterPos = pDC->GetDeviceCaps (VERTRES) / 2;
::StretchBlt(pDC->m_hDC,m_cxOffset,(4 + PRINTMARGIN)*m_cyPrinter,nScaledWidth,nScaledHeight,dcMem,0,0,bitmap.bmWidth,bitmap.bmHeight,SRCCOPY); ::SelectObject(dcMem,hOldBmp);
::DeleteDC(dcMem);
::DeleteObject(hBitmap);
打印预览可以用,但是到打印机上却打印不出来,是空白的纸。
CDC* pDC = (CDC*)wParam;
CPrintInfo* pInfo = (CPrintInfo *)lParam;
int nPageNumber = pInfo->m_nCurPage;
HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, "res\\temp.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
BITMAP bitmap;
::GetObject(hBitmap,sizeof(BITMAP),&bitmap);
double dScale=(double)m_cxWidth/bitmap.bmWidth;
int nScaledWidth=m_cxWidth;
int nScaledHeight=(int)(bitmap.bmHeight*dScale);
HDC dcMem;
dcMem=::CreateCompatibleDC(pDC->m_hDC);
HBITMAP hOldBmp=(HBITMAP)::SelectObject(dcMem,hBitmap);
int nVertCenterPos = pDC->GetDeviceCaps (VERTRES) / 2;
::StretchBlt(pDC->m_hDC,m_cxOffset,(4 + PRINTMARGIN)*m_cyPrinter,nScaledWidth,nScaledHeight,dcMem,0,0,bitmap.bmWidth,bitmap.bmHeight,SRCCOPY); ::SelectObject(dcMem,hOldBmp);
::DeleteDC(dcMem);
::DeleteObject(hBitmap);
你申请的memDC不对,你要打印就不能使用屏幕DC
应该在OnPrint里通过打印机DC来StretchBlt另外就是映射比例和映射模式也很重要
int nCaps = GetDeviceCaps(pDC->m_hDC, RASTERCAPS);
if ( !(nCaps & RC_STRETCHBLT) )
MessageBox(hwnd, "Printer does not support StretchBlt.", "Device Error", MB_OK);
如果不支持,换StretchDIBits试试
CClientDC dc(this);
int bmpWidth = 814;
int bmpHeight = 653;
memDC.CreateCompatibleDC(&dc);
CBitmap * bitmap = new CBitmap();
HBITMAP hBitmap = NULL;
hBitmap = (HBITMAP)LoadImage(NULL, imagename, IMAGE_BITMAP, 0, 0,
LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
bitmap->Attach(hBitmap);
CBitmap * pOldBitmap = (CBitmap *) memDC.SelectObject( bitmap );
if (pOldBitmap == NULL) // if bitmap is very big, better check this !
{
memDC.DeleteDC();
delete bitmap;
return;
}
CDC prtDC;
CPrintInfo printInfo;
CSize size;
DOCINFO di;
CString szPortName, szAppName, szPrintError;
szAppName.LoadString(AFX_IDS_APP_TITLE);
szPrintError = "";
CSize paper_size; //printer paper size in mm
int xLogPPI = 0;
int yLogPPI = 0;
if(AfxGetApp()->GetPrinterDeviceDefaults(&printInfo.m_pPD->m_pd))
{
HDC hDC = printInfo.m_pPD->m_pd.hDC;
if (hDC == NULL)
hDC = printInfo.m_pPD->CreatePrinterDC();
if(hDC !=NULL)
{
prtDC.Attach(hDC);
paper_size.cx = prtDC.GetDeviceCaps(HORZSIZE);
paper_size.cy = prtDC.GetDeviceCaps(VERTSIZE);
xLogPPI = prtDC.GetDeviceCaps(LOGPIXELSX);
yLogPPI = prtDC.GetDeviceCaps(LOGPIXELSY);
}
else
{
AfxMessageBox("找不到打印机!");
return;
}
}
int scr_xLogPPI = dc.GetDeviceCaps(LOGPIXELSX);
int scr_yLogPPI = dc.GetDeviceCaps(LOGPIXELSY);
int paper_width = (int) ((double) paper_size.cx * (double) xLogPPI / 25.4); //width of a printed page in pixels
int paper_height = (int) ((double) paper_size.cy * (double) yLogPPI / 25.4);
double ratio_x = (double) xLogPPI / (double) scr_xLogPPI;
double ratio_y = (double) yLogPPI / (double) scr_yLogPPI;
CString strPageNumber = "";
int page_info_left = (int) ( (double) paper_width * 0.9 );
int page_info_right = paper_width;
int page_info_top = (int) ( (double) paper_height * 0.99);
int page_info_bottom = paper_height;
CRect page_info_rect = CRect(page_info_left, page_info_top,
page_info_right,page_info_bottom );
int printed_pages = 0;
int total_print_pages = 0;
BOOL bAbort_print = FALSE;
// calculate pages
int total_pages = 1;// (bmpWidth * ratio_x + paper_width - 1 ) / paper_width;
//pop up printer dialog
CPrintDialog prtDlg(FALSE, PD_PAGENUMS);
if(prtDlg.DoModal() == IDOK )
{
prtDlg.m_pd.nMinPage = 1;
prtDlg.m_pd.nMaxPage = total_pages;
prtDlg.m_pd.nFromPage = 1;
prtDlg.m_pd.nToPage = total_pages;
memset(&di,0,sizeof(DOCINFO));
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = szAppName;
szPortName = prtDlg.GetPortName();
di.lpszOutput = szPortName;
prtDC.m_bPrinting = TRUE;
}
else
return; //Cancel button pressed, don't forget this!
if(prtDC.StartDoc(&di) == -1)
{
AfxMessageBox("Printing error occured. Unable to find printer.");
prtDC.Detach();
prtDC.DeleteDC();
return;
}
prtDC.SetMapMode(MM_TEXT);
int i = 0;
prtDC.StartPage();
BITMAP bm;
::GetObject(hBitmap,sizeof(BITMAP),&bm);
double dScale=(double)m_cxWidth/bm.bmWidth;
int nScaledHeight=(int)(bm.bmHeight*dScale);
int last_bmpWidth = bmpWidth - paper_width / ratio_x * i;
prtDC.StretchBlt(m_cxOffset,(4 + PRINTMARGIN)*m_cyPrinter, m_cxWidth*1.5,nScaledHeight*1.5,&memDC,0,0,bmpWidth,bmpHeight,SRCCOPY);
prtDC.EndPage();
memDC.SelectObject(pOldBitmap);
delete bitmap;
memDC.DeleteDC();
prtDC.EndDoc();
prtDC.Detach();
prtDC.DeleteDC();
return;