Click here to Skip to main content
13,349,502 members (49,576 online)
Click here to Skip to main content
Add your own
alternative version


34 bookmarked
Posted 8 Feb 2000

Very Simple Print Preview without the Doc/View architecture

, 9 Aug 2001
Rate this:
Please Sign up or sign in to vote.
A very simple way of displaying a print preview in a simple window
<!-- Article Starts -->

The Doc/View architecture in MFC is not the fastest way to create a simple print preview. This article will explain how to create your own print preview.

MFC Doc/View is designed to make the MSVC programmers get their work done in minutes, including print preview. Sometimes we do not need to use Doc/View framework but we really do need its print-preview power.

The key to Print Preview is in the following functions

  • CreateCompatibleBitmap
  • CreateCompatibleDC
  • GetDeviceCaps
  • GetTextExtent

Beside those four functions Print Preview needs a function that work for scaling and painting on the screen DC.

Luckily we have StretchBlt and BitBlt ready to do the dirty work, but sometimes StretchBlt might need to be replaced by your favourite routine (for dithering, filtering, antialiasing etc.).

The function to perform print preview in a window is as follows.

// pWnd is the window you wish to show the preview
// Design and implementation 13 Jan 2000 15:14
//	Danang Suharno
//	Ngadinegaran MJ 3/156
//	Yogyakarta 55143
//	Indonesia
void PrintPreview(CWnd *pWnd) 
	CString strWords;
	CWnd* pWnd = GetDlgItem(IDC_STATIC_PREVIEW); // This is a rectangle control
	CDC* dc;
	CDC memoriDC;
	CBitmap memBMP;
	CBitmap* pOldBMP;
	CFont fnt;
	CFont* pOldFnt;
	CRect rect;
	CRect rectMemory;
	CSize zMetrix;

	CPrintDialog pdlg(FALSE);
	CDC prnDC;

	di.cbSize = sizeof(DOCINFO);
	di.lpszDocName = "This string will appear in Printer Queue";
	di.lpszOutput = NULL;     
	di.lpszDatatype = NULL;
	di.fwType = 0;

	// Get current printer setting

	dc = pWnd->GetDC();

	// DC printer???
	if( !prnDC.Attach(pdlg.GetPrinterDC()) )
		AfxMessageBox("Invalid Printer DC");

	memoriDC.CreateCompatibleDC(&prnDC); // Create DC for Preview

	// Get the resolution of Screen and Current Default Printer
	int iPrnX = prnDC.GetDeviceCaps(HORZRES);
	int iPrnY = prnDC.GetDeviceCaps(VERTRES);
	int iMonX = dc->GetDeviceCaps(HORZRES); // Device Target is Monitor
	int iMonY = dc->GetDeviceCaps(VERTRES); = 0;
	rectMemory.left = 0;
	rectMemory.bottom = iPrnY;
	rectMemory.right = iPrnX;

	// Create a Memory Bitmap that is compatible with the Printer DC
	// then select or make the bitmap as current GDI active object
	memBMP.CreateCompatibleBitmap(&prnDC, rectMemory.Width(), rectMemory.Height());
	pOldBMP = memoriDC.SelectObject(&memBMP);

	// Clear memory DC or in other words
	// paint the bitmap with white colour and transparent text
	memoriDC.SetTextColor(RGB(0, 0, 0));
	memoriDC.PatBlt(0, 0, rectMemory.Width(), rectMemory.Height(), WHITENESS);

	// Prepare the font
	int iPointz = 100;
	fnt.CreatePointFont(iPointz, "OCR A", &memoriDC);
	strWords.Format("This is line number    ");        // Test string
	pOldFnt = memoriDC.SelectObject(&fnt);
	zMetrix = memoriDC.GetTextExtent(strWords);
	int iPos = 0;

	// Write string or Paint something
	int iMaksimal = 0;
	int iLineHeight = 1;
	int iLoop;
	CString strPuncak;

	// Calculate how many lines we could fit
	for(iLoop = 1; iLoop < 100; iLoop++)
		if( ((*iLoop) < iPrnY ) 

	strPuncak.Format("Maximum Amount of line(s) for %d points are %d lines", 
		iPointz, iMaksimal);
	for(iLoop = 0; iLoop < iMaksimal; iLoop++)
		strWords.Format("This is line %d", iLoop);
		memoriDC.TextOut(0, iLoop*(, strWords);
	// Reseting font

	// Calculate ratio
	float fXRatio = (float) iMonX/iPrnX;
	float fYRatio = (float) iMonY/iPrnY;
	//  iLebar = Width
	//  iTinggi = Height
	//  iXPosisiPreview = horisontal location of preview
	//  iYPosisiPreview = vertical location of preview
	int iLebar = rect.Width()*fXRatio;
	int iTinggi = rect.Height()*fYRatio;
	int iXPosisiPreview = (rect.Width() - iLebar)/2;
	int iYPosisiPreview = (rect.Height() - iTinggi)/2;
	CPen pen(PS_SOLID, 2, RGB(255, 0, 0));
	CPen* pOldPen;

	// Create an outline
	pOldPen = dc->SelectObject(&pen);
	dc->Rectangle(iXPosisiPreview, iYPosisiPreview,iXPosisiPreview + iLebar + 2,
		iYPosisiPreview + iTinggi + 2);

	// Put in the box
	dc->StretchBlt(iXPosisiPreview , iYPosisiPreview, iLebar, iTinggi,
		&memoriDC, 0, 0, rectMemory.Width(), rectMemory.Height(), SRCCOPY);
	// Cleaning Up



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Danang Suharno
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralPrinting it Pin
Adeel Mirza3-Feb-10 21:29
memberAdeel Mirza3-Feb-10 21:29 
Generaldoesn't work under Vista SP1 Pin
Beiley10-May-08 10:44
memberBeiley10-May-08 10:44 
General[Message Removed] Pin
nompel4-Oct-08 3:18
membernompel4-Oct-08 3:18 
GeneralRe: doesn't work under Vista SP1 Pin
Member 193415013-May-09 20:16
memberMember 193415013-May-09 20:16 
GeneralRe: doesn't work under Vista SP1 Pin
Beiley14-May-09 7:07
memberBeiley14-May-09 7:07 
Generalreading data from file when list control is very first shown Pin
Win32.sality10-Mar-08 23:43
memberWin32.sality10-Mar-08 23:43 
QuestionHow To Print listView items on A4 paper from VC++ Express Edition? Pin
chrisliando14-Nov-07 14:26
memberchrisliando14-Nov-07 14:26 
Hi. I am still a beginner in Visual C++. I use Visual C++ 2005 Express Edition. My application consists of listView and treeView, then it should be able to print the items in the listView, which display employee data, like company name, employee name, address, etc.

How to print all the data in the listView on a A4 paper? How to set the paper to A4?

I've read the help about using printDocument and printPreview component but I still don't understand.

Do you have a simple example in code?

Thank you very much.

QuestionGetting Data from IP Camera using VC++ Pin
Kirubanandam6-Jan-06 23:54
memberKirubanandam6-Jan-06 23:54 
Generalimprovement Pin
Franz Brunner8-Jan-03 23:48
memberFranz Brunner8-Jan-03 23:48 
GeneralGetDlgItem does take 1 argument, if ... Pin
Arnt Witteveen13-Apr-00 23:12
sussArnt Witteveen13-Apr-00 23:12 
GeneralGetDlgItem does not take 1 argument Pin
Arnt Witteveen12-Apr-00 6:07
sussArnt Witteveen12-Apr-00 6:07 
GeneralCWnd* pWnd defined twice Pin
Christian10-Feb-00 8:29
sussChristian10-Feb-00 8:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 10 Aug 2001
Article Copyright 2000 by Danang Suharno
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid