ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Charts in a printout
Posted by Dave on Aug-10-2008 19:10
Hi all

Is there any example code on how to display a graph in a printout e.g. not only display the graph in a CView derived class but also in the printout? In the examples I have seen i.e. mfcdemo when I press PrintPreview I get an empty printout.

Dave

  Re: Charts in a printout
Posted by Peter Kwan on Aug-12-2008 00:42
Hi Dave,

I think you can print the graph just like printing any other images or other things in MFC. There is no special code needed, apart from normal MFC printing code. You just need to send the bitmap to the printer device context (usually in the MFC OnPrint method).

For example, the following is some code to print a general bitmap (CStatic control), which should work with the CChartViewer control tool. You may try this in the MFC OnPrint method.

//
// get the bitmap to print
//
BITMAPINFO stBitmapInfo;
memset(&stBitmapInfo, 0, sizeof(stBitmapInfo));
stBitmapInfo.bmiHeader.biSize = sizeof(stBitmapInfo.bmiHeader);
::GetDIBits(viewer->GetDC()->m_hDC, viewer->GetBitmap(), 0, 0, 0, &stBitmapInfo, DIB_RGB_COLORS);
stBitmapInfo.bmiHeader.biBitCount = 24;
stBitmapInfo.bmiHeader.biCompression = BI_RGB;
stBitmapInfo.bmiHeader.biSizeImage = ((((stBitmapInfo.bmiHeader.biWidth * 24) + 31) & ~31) >> 3) * stBitmapInfo.bmiHeader.biHeight;
void *data = malloc(stBitmapInfo.bmiHeader.biSizeImage);
::GetDIBits(viewer->GetDC()->m_hDC, viewer->GetBitmap(), 0, stBitmapInfo.bmiHeader.biHeight, data, &stBitmapInfo, DIB_RGB_COLORS);

//
// send the bitmap to the printer device context
//
double xScaleFactor = pDC->GetDeviceCaps(LOGPIXELSX) / 96.0;
double yScaleFactor = pDC->GetDeviceCaps(LOGPIXELSY) / 96.0;

::StretchDIBits (pDC->m_hDC,
  (int)(40 * xScaleFactor),
  (int)(40 * yScaleFactor),
  (int)(stBitmapInfo.bmiHeader.biWidth * xScaleFactor),
  (int)(stBitmapInfo.bmiHeader.biHeight * yScaleFactor),
  0,
  0,
  stBitmapInfo.bmiHeader.biWidth,
  stBitmapInfo.bmiHeader.biHeight,
  data,
  (LPBITMAPINFO)&stBitmapInfo,
  DIB_RGB_COLORS,
  SRCCOPY);

free(data);

Hope this can help.

Regards
Peter Kwan

  Re: Charts in a printout
Posted by Dave on Aug-12-2008 01:20
Many thanks Kwan. I simply changed

viewer

to

m_ChartViewer[0]

in the MFC demo and it worked like a dream!

Dave