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.
- CreateCompatibleBitmap
- CreateCompatibleDC
- GetDeviceCaps
- 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()
{
CString strWords;
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);
DOCINFO di;
CDC prnDC;
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = "This string will appear in Printer
Queue";
di.lpszOutput = NULL;
di.lpszDatatype = NULL;
di.fwType = 0;
pdlg.GetDefaults();
dc = pWnd->GetDC();
pWnd->GetClientRect(&rect);
if( !prnDC.Attach(pdlg.GetPrinterDC()) )
AfxMessageBox("Invalid Printer DC");
memoriDC.CreateCompatibleDC(&prnDC);
Preview
Printer
int iPrnX = prnDC.GetDeviceCaps(HORZRES);
int iPrnY = prnDC.GetDeviceCaps(VERTRES);
int iMonX = dc->GetDeviceCaps(HORZRES);
Target is Monitor
int iMonY = dc->GetDeviceCaps(VERTRES);
rectMemory.top = 0;
rectMemory.left = 0;
rectMemory.bottom = iPrnY;
rectMemory.right = iPrnX;
Printer DC
active object
memBMP.CreateCompatibleBitmap(&prnDC,
rectMemory.Width(), rectMemory.Height());
pOldBMP = memoriDC.SelectObject(&memBMP);
text
memoriDC.SetBkMode(TRANSPARENT);
memoriDC.SetTextColor(RGB(0, 0, 0));
memoriDC.PatBlt(0, 0, rectMemory.Width(),
rectMemory.Height(), WHITENESS);
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;
int iMaksimal = 0;
int iLineHeight = 1;
int iLoop;
CString strPuncak;
for(iLoop = 1; iLoop < 100; iLoop++)
{
if( ((zMetrix.cy+iLineHeight)*iLoop) < iPrnY )
iMaksimal++;
}
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*(zMetrix.cy+iLineHeight),
strWords);
}
memoriDC.SelectObject(pOldFnt);
float fXRatio = (float) iMonX/iPrnX;
float fYRatio = (float) iMonY/iPrnY;
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;
pOldPen = dc->SelectObject(&pen);
dc->Rectangle(iXPosisiPreview, iYPosisiPreview ,
iXPosisiPreview + iLebar + 2 , iYPosisiPreview +
iTinggi + 2);
dc->SelectObject(pOldPen);
dc->StretchBlt(iXPosisiPreview , iYPosisiPreview ,
iLebar, iTinggi,
&memoriDC, 0, 0, rectMemory.Width(),
rectMemory.Height(), SRCCOPY);
fnt.DeleteObject();
memoriDC.SelectObject(pOldBMP);
memoriDC.DeleteDC();
memBMP.DeleteObject();
prnDC.Detach();
pWnd->ReleaseDC(dc);
}