Home

MFC Topics: Printing Fundamentals

 

Fundamentals of Printing

 

Introduction

To make it possible to print from a document, that is, from a CDocument object, the MFC provides printing at the CView level. Since CView is the common ancestor to the view-based classes, the derived objects inherit this functionality, using the Document/View architecture.

WYSIWYG Not Exactly

When introducing the process of creating a new project, we saw that you could use the MFC Application wizard to generate a semi-ready made application equipped with a view. When studying GDI, we saw that you could use the OnDraw() event of the view to draw something on the monitor (of course, if you manually create your application and apply the document/view architecture, you can still implement this event on your own).

If you use the MFC Application to create a view-based application, the wizard would give you to option to support printing:

If you accept this default to provide printing in your application, the wizard would create a ready-made menu:

Notice that the toolbar is also equipped with a button that displays a printer. This indicates that you should be able to print something you would have drawn using the OnDraw() event.

For example, you can draw a picture or bitmap to display to the user. Consider the following image:

Here is a way of displaying this picture in a view:

void CDecorationView::OnDraw(CDC* pDC)
{
	CDecorationDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	CBitmap bmpFlower;
	CDC memDCFlower;

	bmpFlower.LoadBitmap(IDB_FLOWER1);
	memDCFlower.CreateCompatibleDC(pDC);
	CBitmap *bmpPrevious = memDCFlower.SelectObject(&bmpFlower);

	pDC->BitBlt(10, 10, 450, 563, &memDCFlower, 0, 0, SRCCOPY);

	pDC->SelectObject(bmpPrevious);
	// TODO: add draw code for native data here
}

This would produce:

Decoration

To print this, on the main menu, you would click File -> Print… and you would notice that only a small picture prints

This indicates that, when it comes to default printing, What You See on the monitor is not exactly What You Get on the Printer.

 

Home Copyright © 2006-2007 FunctionX, Inc. Next