wangnanjing
September 30th, 2002, 10:19 PM
Hi,everyone:
I want to print a picture drawn in one of my windows.
The picture's base class is a CChart named m_chart.
The print programme is as following:
CWnd *pWnd = &m_chart;
CWindowDC dcWnd(pWnd);
CDC memDC;
CRect rect;
HBITMAP bmp;
CBitmap bitmap0;
memDC.CreateCompatibleDC(&dcWnd);
pWnd->GetWindowRect(rect);
bitmap0.CreateCompatibleBitmap(&dcWnd, rect.Width(),rect.Height() );
CBitmap* pOldBitmap = memDC.SelectObject(&bitmap0);
CPrintDialog dlg(FALSE);
if (dlg.DoModal() ==IDOK)
{
// Create a CDC object for the device context we got
CDC dcPrinter;
dcPrinter.Attach(dlg.m_pd.hDC);
// Determine width, height and orientation
LPDEVMODE pDM = dlg.GetDevMode();
int dmOrientation = pDM->dmOrientation;
int Width = dcPrinter.GetDeviceCaps(HORZRES) - 100;
int Height = dcPrinter.GetDeviceCaps(VERTRES) - 100;
// Describe print job to print spooler
DOCINFO docInfo;
docInfo.cbSize = sizeof(docInfo);
docInfo.fwType = 0;
docInfo.lpszDocName = "Print Thermal Schedule!";
docInfo.lpszOutput = NULL;
docInfo.lpszDatatype = NULL;
// Start a document
if (0 < dcPrinter.StartDoc(&docInfo))
{
// Start a page; you might call StartPage/EndPage more than once;
// you'll have one call pair for each page you print.
if (0 < dcPrinter.StartPage())
{
// measure the printable page
int nOffsetX = dcPrinter.GetDeviceCaps(PHYSICALOFFSETX);
int nOffsetY = dcPrinter.GetDeviceCaps(PHYSICALOFFSETY);
int nHorzRes = dcPrinter.GetDeviceCaps(HORZRES);
int nVertRes = dcPrinter.GetDeviceCaps(VERTRES);
// load bitmap from file
bmp = NULL;
//bmp = (HBITMAP)bitmap0.GetSafeHandle();
//bmp = (HBITMAP) bitmap0;
HDC hdc;
bmp = (HBITMAP) dcWnd.GetCurrentBitmap()->m_hObject;
dcWnd.SelectGdiObject(hdc,bmp);
CBitmap bitmap;
bitmap.Attach (bmp);
// copy bitmap into memory DC
CDC dcMemory;
dcMemory.CreateCompatibleDC(&dcPrinter);
CBitmap* pOldBitmap = dcMemory.SelectObject(&bitmap);
BITMAP bm;
bitmap.GetBitmap(&bm);
// determine magnification
double Magnification;
double MagnificationW = (double)Width / (double)bm.bmWidth;
double MagnificationH = (double)Height / (double)bm.bmHeight;
Magnification = MagnificationW >= MagnificationH ? MagnificationH :
MagnificationW;
int iBitMap_X = (int)((double)bm.bmWidth * Magnification);
int iBitMap_Y = (int)((double)bm.bmHeight * Magnification);
int iOffset_X = (dcPrinter.GetDeviceCaps(HORZRES) - iBitMap_X - 50) / 2;
int iOffset_Y;
if (dmOrientation == 1) iOffset_Y = 50;
else iOffset_Y = (dcPrinter.GetDeviceCaps(VERTRES) - iBitMap_Y) / 2;
// stretch memory DC into printer DC
dcPrinter.StretchBlt(iOffset_X, iOffset_Y, iBitMap_X,
iBitMap_Y,&dcMemory,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);//dcMemory
dcMemory.SelectObject(pOldBitmap);
// close the page
dcPrinter.EndPage();
}
else
{
MessageBox("Couldn't start new page!");
}
// close the document. Printing actually starts now!
dcPrinter.EndDoc();
}
else
{
MessageBox("Couldn't start document.");
}
}
else
{
MessageBox("You have no default printer. You must set a default printer before priting.");
}
When I degugged,I can see the value of bmp and the length of iBitMap_X and so so.
I likes that I have got the handle to the bmp.
But I print nothing out.
Can you tell me why?
thanks a lot.
weiwei
I want to print a picture drawn in one of my windows.
The picture's base class is a CChart named m_chart.
The print programme is as following:
CWnd *pWnd = &m_chart;
CWindowDC dcWnd(pWnd);
CDC memDC;
CRect rect;
HBITMAP bmp;
CBitmap bitmap0;
memDC.CreateCompatibleDC(&dcWnd);
pWnd->GetWindowRect(rect);
bitmap0.CreateCompatibleBitmap(&dcWnd, rect.Width(),rect.Height() );
CBitmap* pOldBitmap = memDC.SelectObject(&bitmap0);
CPrintDialog dlg(FALSE);
if (dlg.DoModal() ==IDOK)
{
// Create a CDC object for the device context we got
CDC dcPrinter;
dcPrinter.Attach(dlg.m_pd.hDC);
// Determine width, height and orientation
LPDEVMODE pDM = dlg.GetDevMode();
int dmOrientation = pDM->dmOrientation;
int Width = dcPrinter.GetDeviceCaps(HORZRES) - 100;
int Height = dcPrinter.GetDeviceCaps(VERTRES) - 100;
// Describe print job to print spooler
DOCINFO docInfo;
docInfo.cbSize = sizeof(docInfo);
docInfo.fwType = 0;
docInfo.lpszDocName = "Print Thermal Schedule!";
docInfo.lpszOutput = NULL;
docInfo.lpszDatatype = NULL;
// Start a document
if (0 < dcPrinter.StartDoc(&docInfo))
{
// Start a page; you might call StartPage/EndPage more than once;
// you'll have one call pair for each page you print.
if (0 < dcPrinter.StartPage())
{
// measure the printable page
int nOffsetX = dcPrinter.GetDeviceCaps(PHYSICALOFFSETX);
int nOffsetY = dcPrinter.GetDeviceCaps(PHYSICALOFFSETY);
int nHorzRes = dcPrinter.GetDeviceCaps(HORZRES);
int nVertRes = dcPrinter.GetDeviceCaps(VERTRES);
// load bitmap from file
bmp = NULL;
//bmp = (HBITMAP)bitmap0.GetSafeHandle();
//bmp = (HBITMAP) bitmap0;
HDC hdc;
bmp = (HBITMAP) dcWnd.GetCurrentBitmap()->m_hObject;
dcWnd.SelectGdiObject(hdc,bmp);
CBitmap bitmap;
bitmap.Attach (bmp);
// copy bitmap into memory DC
CDC dcMemory;
dcMemory.CreateCompatibleDC(&dcPrinter);
CBitmap* pOldBitmap = dcMemory.SelectObject(&bitmap);
BITMAP bm;
bitmap.GetBitmap(&bm);
// determine magnification
double Magnification;
double MagnificationW = (double)Width / (double)bm.bmWidth;
double MagnificationH = (double)Height / (double)bm.bmHeight;
Magnification = MagnificationW >= MagnificationH ? MagnificationH :
MagnificationW;
int iBitMap_X = (int)((double)bm.bmWidth * Magnification);
int iBitMap_Y = (int)((double)bm.bmHeight * Magnification);
int iOffset_X = (dcPrinter.GetDeviceCaps(HORZRES) - iBitMap_X - 50) / 2;
int iOffset_Y;
if (dmOrientation == 1) iOffset_Y = 50;
else iOffset_Y = (dcPrinter.GetDeviceCaps(VERTRES) - iBitMap_Y) / 2;
// stretch memory DC into printer DC
dcPrinter.StretchBlt(iOffset_X, iOffset_Y, iBitMap_X,
iBitMap_Y,&dcMemory,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);//dcMemory
dcMemory.SelectObject(pOldBitmap);
// close the page
dcPrinter.EndPage();
}
else
{
MessageBox("Couldn't start new page!");
}
// close the document. Printing actually starts now!
dcPrinter.EndDoc();
}
else
{
MessageBox("Couldn't start document.");
}
}
else
{
MessageBox("You have no default printer. You must set a default printer before priting.");
}
When I degugged,I can see the value of bmp and the length of iBitMap_X and so so.
I likes that I have got the handle to the bmp.
But I print nothing out.
Can you tell me why?
thanks a lot.
weiwei