I am trying to print a CNiGraph control image from an MFC MDI application. I modified the example in the samples under /MeasurementStudio/VC/Examples/UI/Common/Printing
which was written for a dialog based app.
Both the print preview and the printout is always a blank page. The code seems
simple enough but I can't see where the problem is. I tried several other variations on what I did with no success. Any help on this would be appreciated.
The code in OnPrint() in the CFormView derived class looks like this:
void CApparentZView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: Add your command handler code here
// HDC hdcPrinter;
// CDC cdc;
// CPrintDialog dlg(FALSE);
// DOCINFO info;
// LPDOCINFO pinfo=&info;
pDC->SetMapMode(MM_LOMETRIC);
// get a dc for the screen and attach it to a CDC
CDC dcScreen;
HDC hdcScreen = ::GetDC(NULL); //retrieves the DC for the entire screen
dcScreen.Attach(hdcScreen);
// if (dlg.DoModal()==IDOK)
{
// hdcPrinter=dlg.GetPrinterDC();
// cdc.Attach(hdcPrinter);
// info.lpszDocName="test run";
// info.lpszOutput=NULL;
// info.cbSize=sizeof(info.lpszDocName);
// this should already be done by the MFC MDI framework
// cdc.StartDoc(pinfo);
// cdc.StartPage();
/*
* Retrieve the number of pixels-per-logical-inch
* in the horizontal and vertical directions
* for the display upon which the bitmap
* was created.
*/
long logicalPixelsPerInchX_Screen = GetDeviceCaps(hdcScreen, LOGPIXELSX);
long logicalPixelsPerInchY_Screen = GetDeviceCaps(hdcScreen, LOGPIXELSY);
/*
* Retrieve the number of pixels-per-logical-inch
* in the horizontal and vertical directions
* for the printer upon which the bitmap
* will be printed.
*/
// long logicalPixelsPerInchX_Printer = GetDeviceCaps(hdcPrinter, LOGPIXELSX);
// long logicalPixelsPerInchY_Printer = GetDeviceCaps(hdcPrinter, LOGPIXELSY);
long logicalPixelsPerInchX_Printer = pDC->GetDeviceCaps(LOGPIXELSX);
long logicalPixelsPerInchY_Printer = pDC->GetDeviceCaps(LOGPIXELSY);
CRect rc;
CRect rcGraph;
BOOL bResizeGraph = FALSE;
// get the rectangle of the control in screen coordinates
m_graphControl.GetWindowRect(&rcGraph);
// convert the coordinates into the coordinates of the dialog window
ScreenToClient(&rcGraph);
#if FALSE
// this code gives you a graph that is a certain size
// (in inches). For the graph to look best on the printer,
// the aspect ratio (height to width ratio) needs to remain the same.
const double cInchesWide = 6;
// calculate a height (in inches) that is the same
// aspect ratio as the current graph on the screen
double cInchesTall = cInchesWide * rcGraph.Height() / rcGraph.Width();
// set bResizeGraph to TRUE if you want to resize the graph
// to give you a higher resolution printout
// Otherwise, the graph on the printer will look like the graph
// on the screen.
bResizeGraph = TRUE;
if (bResizeGraph) {
// create a rectangle to resize the graph to (on the screen)
CRect rcNew;
rcNew.left = 0;
rcNew.top = 0;
rcNew.right = (long)(cInchesWide * logicalPixelsPerInchX_Screen);
rcNew.bottom = (long)(cInchesTall * logicalPixelsPerInchY_Screen);
// resize the graph so it will render larger (with more detail)
// the FALSE tells it to not redraw
m_graphControl.MoveWindow(rcNew, FALSE);
}
// Calculate a rectangle (in printer coordinates) to print the
// graph in.
rc.left = 0;
rc.top = 0;
rc.right = (long)(cInchesWide * logicalPixelsPerInchX_Printer);
rc.bottom = (long)(cInchesTall * logicalPixelsPerInchY_Printer);
#else
// this code gives you a graph that is the same size and
// location as it is on the dialog
rc = rcGraph;
// scale rc to printer coordinates from screen coordinates
rc.right = rc.left + rc.Width() * logicalPixelsPerInchX_Printer / logicalPixelsPerInchX_Screen;
rc.bottom = rc.top + rc.Height() * logicalPixelsPerInchY_Printer / logicalPixelsPerInchY_Screen;
rc.OffsetRect(- rc.left + rc.left * logicalPixelsPerInchX_Printer / logicalPixelsPerInchX_Screen,
- rc.top + rc.top * logicalPixelsPerInchY_Printer / logicalPixelsPerInchY_Screen);
#endif
// print each individual ActiveX control
CPictureHolder ph;
// ControlImage returns a metafile of the control
ph.SetPictureDispatch((LPPICTUREDISP)m_graphControl.ControlImage().m_lpDispatch);
ph.Render(pDC, rc, rc);
// restore the graph's size if we resized it.
if (bResizeGraph)
m_graphControl.MoveWindow(rcGraph);
// this should already be done by the MFC MDI framework
// cdc.EndPage();
// cdc.EndDoc();
}
dcScreen.Detach();
::DeleteDC(hdcScreen);
// CMyGraphView::OnPrint(pDC, pInfo);
}