|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis set of classes enables printing without using doc/view architecture. This allows you to print directly from your dialog or from any part of your MFC-based application. The code was based on A. Fraydl's "Creating a Printing Class" and George Papaioannou's "Quick Print." Improvements include:
Using the codeThe only thing that you have to do is declare the Void CLitePrintDemoDlg::OnPrintButton ()
{
CLitePrint prt;
HLTFONT hFont;
// Step 1 : call the CPrintDialog
if (prt.Dialog() == -1)
return;
//Step 2 : Start the Print
prt.StartPrint();
//Step 3 : Create a printing font
hFont = prt.AddFontToEnvironment(_T("Arial"),8,8);
//Step 4 : Start Page
prt.StartPage();
//Step 5 : The actual printing goes here
prt.PrintText(hFont,_T("Example Text"),FORMAT_NORMAL);
//Step 6 : now end the page
prt.EndPage();
//Step 7 : close the print document
// and release it in the spooler
prt.EndPrint();
}
An external, user-defined header/footer function is called each time the printing reaches the beginning or the end of a page. An example that prints a logo image, lines and page numbers is given bellow: void FHeadFoot(CLitePrint* lp,int sides, bool hf)
{
HLTFONT af = lp->SetHFFont();
COLORREF tc = lp->GetTextColor();
lp->SetTextColor(RGB(0,0,0));
if (hf) //if header
{
lp->LF();
CSize sz;
sz.cx = 100;
sz.cy = 50;
lp->PrintBitmap(IDB_BITMAP2, FORMAT_LEFT, &sz);
lp->Line(PS_SOLID);
lp->PrintText(lp->GetActiveFont(),
_T("Report Document"),FORMAT_CENTER);
CString nmb;
nmb.Format(_T("Page %d"),sides);
lp->PrintText(lp->GetActiveFont(),nmb,FORMAT_RIGHT);
lp->Line(PS_SOLID,5,RGB(230,200,20));
lp->LF();
}
else //if footer
{
CString>nmb;
nmb.Format(_T("Page %d"),sides);
lp->PrintText(lp->GetActiveFont(),nmb,FORMAT_CENTER);
lp->LF(lp->GetActiveFont());
lp->LF(lp->GetActiveFont());
}
lp->SetActiveFont(af);
lp->SetTextColor(tc);
}
To use tables, one has to define the column widths and alignment. Some helper code has been developed for this purpose. Just fill in the vector with the column's data and pass it to the vColData vd;
vd.push_back(Col(300,FORMAT_CENTER));
vd.push_back(Col(100,FORMAT_LEFT));
vd.push_back(Col(300,FORMAT_CENTER));
...
prt.SetTableColumns(vd);
//prt.SetTableColumns(vd,TRUE); //fit width
Points of interestMore details about all of the functions can be found in the demo project. Note that print preview is not developed yet. Please, if you see any problems with these classes, feel free to send questions. Good luck! History
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||