With The Case Of  Release The Power OF  Visual C++ !   HomeProducts | PurchaseSupport | Downloads  
Download Evaluation
Pricing & Purchase?
E-XD++Visual C++/ MFC Products
Features Tour 
Electronic Form Solution
Visualization & HMI Solution
Power system HMI Solution
CAD Drawing and Printing Solution

Bar code labeling Solution
Workflow Solution

Coal industry HMI Solution
Instrumentation Gauge Solution

Report Printing Solution
Graphical modeling Solution
GIS mapping solution

Visio graphics solution
Industrial control SCADA &HMI Solution
BPM business process Solution

Industrial monitoring Solution
Flowchart and diagramming Solution
Organization Diagram Solution

Graphic editor Source Code
UML drawing editor Source Code
Map Diagramming Solution

Architectural Graphic Drawing Solution
Request Evaluation
ActiveX COM Products
Technical Support
  General Q & A
Discussion Board
Contact Us


Get Ready to Unleash the Power of UCanCode .NET


UCanCode Software focuses on general application software development. We provide complete solution for developers. No matter you want to develop a simple database workflow application, or an large flow/diagram based system, our product will provide a complete solution for you. Our product had been used by hundreds of top companies around the world!

"100% source code provided! Free you from not daring to use components because of unable to master the key technology of components!"

MFC Example: Print Preview in MFC - Dialog

Danang Suharno
January 30, 2000

VC++ Source Codes
DocView architecture in MFC is not at all the fastest way to create a simple print preview. This article will explain How to create your own print preview. MFC DocView is designed to make the MSVC programmers get their works done in a minutes including print preview. Sometimes we need not to use DocView framework but we really need its print preview power. The key of this Print Preview is laying on the following function.

  1. CreateCompatibleBitmap
  2. CreateCompatibleDC
  3. GetDeviceCaps
  4. GetTextExtent
Beside those four functions, Print Preview needs a function that work for scaling and painting on the screen DC. We just lucky to have StretchBlt and BitBlt ready to do the dirty works but sometimes StretchBlt should be replaced by your favourite routine and then BitBlt comes into play. The reason is (maybe) dithering, filtering, anti aliasing etc..
void CDlgAneh::OnButton1()
// TODO: Add your control notification handler code here
// Design and implementation
// 13 Jan 2000 15:14

CString strWords;

// This is a rectangle control
CWnd* pWnd = GetDlgItem(IDC_STATIC_PREVIEW);

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
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

// Get the resolution of Screen and Current Default
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
rectMemory.Width(), rectMemory.Height());
pOldBMP = memoriDC.SelectObject(&memBMP);

// Clear memory DC or in other words
// paint the bitmap with white colour and transparent
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*(,

// 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




Copyright ?1998-2009 UCanCode.Net Software , all rights reserved.
Other product and company names herein may be the trademarks of their respective owners.

Please direct your questions or comments to