Tutorials

 

Home
About
How it Works
Documentation
Tutorials

Examples

Links
Contact

 

 

Menu of tutorials

Tutorial 1:   The Simplest Window
Tutorial 2:   Using Classes and Inheritance
Tutorial 3:   Using Messages to Create a Scribble Window
Tutorial 4:   Repainting the Window
Tutorial 5:   Wrapping a Frame around our Scribble Window
Tutorial 6:   Customising Window Creation
Tutorial 7:   Customising the ToolBar
Tutorial 8:   Loading and Saving Files
Tutorial 9:   Printing
Tutorial 10: Print Previewing
Tutorial 11: Finishing Touches

Tutorial 9:  Printing

In this tutorial we will demonstrate how to send a bitmap to a printer. The bitmap in this case will be the one we've been drawing on in the view window. The resulting printout will be resized to match the size of the original drawing. This task can be broken down into several steps:

  • Extract the bitmap from the view window
  • Choose the printer
  • Start the print job
  • Extract the bitmap image data from the bitmap
  • Copy the resized image data to the printer's device context
  • End the print job

Extract the bitmap from the view window 

When we draw on the view window, we are actually drawing on to a bitmap attached to view window's device context. Here we will copy this bitmap to a compatible bitmap.

   // Copy the bitmap from the View window.
  CClientDC viewDC(GetView());
  CMemDC memDC(viewDC);
  memDC.CreateCompatibleBitmap(viewDC, width, height);
  memDC.SelectObject(viewBitmap);
  memDC.BitBlt(0, 0, width, height, viewDC, 0, 0, SRCCOPY);
  CBitmap bmView = memDC.DetachBitmap();

Choose a printer

This step is fairly straight forward. We declare a PRINTDLG struct and use this in the PrintDlg function. The PrintDlg function brings up a dialog, allowing us to choose the printer, and stores its device context in the PRINTDLG struct.

   CPrintDialog printDlg;

  // Bring up a dialog to choose the printer.
  if (printDlg.DoModal(GetView()) == IDOK) // throws exception if there is no default printer

Note: CPrintDialog::DoModal will throw a CResourceException if there is no printer configured, or if the dialog can't be created. A try/catch block should be used to catch an exception thrown by DoModal.

Start the Print Job

The StartDoc function should be called before sending output to a printer. This function ensures that multipage documents are not interspersed with other print jobs. StartPage (and a corresponding EndPage) is then called for each page of printout.

   // Zero and then initialize the members of a DOCINFO structure.
  DOCINFO di;
  memset(&di, 0, sizeof(DOCINFO));
  di.cbSize = sizeof(DOCINFO);
  di.lpszDocName = _T("Scribble Printout");

  // Begin a print job by calling the StartDoc function.
  StartDoc(pd.hDC, &di);

  // Inform the driver that the application is about to begin sending data.
  StartPage(pd.hDC);

Extract the bitmap image data from the bitmap

In order to use StretchDIBits we first need the bitmap image data. This is retrieved by using the GetDIBits. It is called twice in the following code sample, the first time to get the size byte array to hold the data, and the second to fill the byte array.

   // Get the dimensions of the View window.
  CRect viewRect = GetView().GetClientRect();
  int width = viewRect.Width();
  int height = viewRect.Height();
	
  // Fill the BITMAPINFOHEADER structure.
  BITMAPINFOHEADER bih;
  ZeroMemory(&bih, sizeof(BITMAPINFOHEADER));
  bih.biSize = sizeof(BITMAPINFOHEADER);
  bih.biHeight = height;
  bih.biWidth = width;
  bih.biPlanes = 1;
  bih.biBitCount =  24;
  bih.biCompression = BI_RGB;
  
  // Note: BITMAPINFO and BITMAPINFOHEADER are the same for 24 bit bitmaps.
  // Get the size of the image data.
  BITMAPINFO* pBI = reinterpret_cast<BITMAPINFO*>(&bih);
  memDC.GetDIBits(bmView, 0, height, NULL, pBI, DIB_RGB_COLORS);

  // Retrieve the image data.
  std::vector imageData(bih.biSizeImage, 0);	// a vector to hold the byte array
  byte* byteArray = &imageData.front();
  memDC.GetDIBits(bmView, 0, height, byteArray, pBI, DIB_RGB_COLORS);	

Copy the resized bitmap to the printer's device context

StretchDIBits is the function used here to copy the bitmap information to the printer's device context because the bitmap needs to be resized in order to retain the same dimensions on the printed page as the original.  The following code segment shows how the scaling factors are calculated and the StretchDIBits  function is called.

  // Determine the scaling factors required to print the bitmap and retain its original proportions.
 double viewPixelsX = double(viewDC.GetDeviceCaps(LOGPIXELSX));
 double viewPixelsY = double(viewDC.GetDeviceCaps(LOGPIXELSY));
 double printPixelsX = double(printDC.GetDeviceCaps(LOGPIXELSX));
 double printPixelsY = double(printDC.GetDeviceCaps(LOGPIXELSY));
 double scaleX = printPixelsX / viewPixelsX;
 double scaleY = printPixelsY / viewPixelsY;

 int scaledWidth = int(width * scaleX);
 int scaledHeight = int(height * scaleY);

 // Use StretchDIBits to scale the bitmap and maintain its original proportions.
 printDC.StretchDIBits(0, 0, scaledWidth, scaledHeight, 0, 0, width, height,
                       byteArray, pBI, DIB_RGB_COLORS, SRCCOPY);

End the print job

To finish the print job, EndPage is called to indicate that printing to this page is complete, then EndDoc is called the end the print job.

   // Inform the driver that the page is finished.
  EndPage(pd.hDC);
		
  // Inform the driver that document has ended.
  EndDoc(pd.hDC);

Accessing the CView from within CDoc

The CDoc::Print uses GetView to access the CView class. GetView is defined as follows.

const CView& CDoc::GetView() const
{
  CMainFrame& frame = GetScribbleApp().GetMainFrame();
  return static_cast(frame.GetView());
}

The source code for this tutorial is located within the Tutorial folder of the software available from SourceForge at http://sourceforge.net/projects/win32-framework.