Print Preview Without MFC
Posted
by Danang Suharno
on January 30th, 2000
- CreateCompatibleBitmap
- CreateCompatibleDC
- GetDeviceCaps
- GetTextExtent
void CDlgAneh::OnButton1()
{
// TODO: Add your control notification handler code here
//
//
// Design and implementation cinoban@yahoo.com
// 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);
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;
//
// Get current printer setting
//
pdlg.GetDefaults();
//
dc = pWnd->GetDC();
pWnd->GetClientRect(&rect);
// 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);
rectMemory.top = 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.SetBkMode(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( ((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);
}
//
// Reseting font
//
memoriDC.SelectObject(pOldFnt);
//
// 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);
dc->SelectObject(pOldPen);
//
// Put in the box
//
dc->StretchBlt(iXPosisiPreview , iYPosisiPreview ,
iLebar, iTinggi,
&memoriDC, 0, 0, rectMemory.Width(),
rectMemory.Height(), SRCCOPY);
//
// Cleaning Up
//
fnt.DeleteObject();
memoriDC.SelectObject(pOldBMP);
memoriDC.DeleteDC();
memBMP.DeleteObject();
prnDC.Detach();
//
pWnd->ReleaseDC(dc);
}

Comments
Print on pages different from A4
Posted by Legacy on 05/13/2003 12:00amOriginally posted by: Gigo
Trhere is way to make it?
Reply
Print Preview Without MFC
Posted by Legacy on 12/13/2002 12:00amOriginally posted by: Dan
Why is it in MFC if it says 'Without MFC'?
ReplyNot proper Output
Posted by Legacy on 05/23/2001 12:00amOriginally posted by: Bakul
Hi all,
I have used this same code to generate Print Preview. I haven't changed even a bit of this code.
But my problem is that I am getting a blank White Rectangle with Red Outline. Rectangel is also too small. There is nothing written on my screen in any part of output of my dialog box.
So, can any of u suggest me solution to this problem?
Replyprint preview command
Posted by Legacy on 02/06/2000 12:00amOriginally posted by: Anggy
Wah gua kagak ngarti nang, yang paling gampang untuk print preview itu klik file-print preview..... :-)
Reply