Click here to Skip to main content
15,891,136 members
Articles / Desktop Programming / MFC
Article

Quick Print

Rate me:
Please Sign up or sign in to vote.
4.86/5 (24 votes)
5 May 2006CPOL4 min read 160.1K   7.2K   89   52
An easy way to print without the doc/view architecture.

Sample Image

Introduction

This printing class is based on the article by Rob A. Fraydl, "Creating a Printing Class". In fact, it's just an extension of the original class. What exactly has been extended? Well, first of all, I renamed the class to QPrint (stands for quick print). Big achievement, eh? OK, OK, this feature is not an extension at all, but anyway it's a start.

The second extension was to add the ability to manage big strings that extend beyond the right border of the physical paper size. Now, the big strings are not clipped at the end of the right border, but instead they continue to the next line.

Last but not the least, the new printing class supports tables as well as page orientation.

Background

As already said in the beginning, this piece of code did not start from scratch. You can always refer to the original printing class named CPrivatePrint. Please feel free to use and improve this code. Other than the additions I have made, I also corrected some bugs. I am not sure in some points if they were bugs, especially the part with the logical units, using the macros CALCF(x), CALCY(y), and CALCX(x). I deleted CALCX(x) completely, and the CALCY(x) partially, from the source code. So, if you meet "strange" results, please let me know. The others were safe point bugs that got corrected successfully.

I tested this class using the JawsPDF Creator (to save paper, time, patience, and finally money!) and different ink jet printers. It worked flawlessly.

Using the code

I chose this printing class as a base to build QuickPrint, because it was compact and easy to use. So, the only thing that you have to do is to declare the CQPrint prt and start using it. The following example shows the basic steps that you have to do:

void TestDlg::OnPrintButton ()
{
CQPrint prt;
HPRIVATEFONT   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("Arial Greek",8,8);     
  
  //Step 4 : Start Page
  prt.StartPage(); 
  
  //Step 5 : The actual printing goes here
  prt.Print(hFont,"Set Font Arial Size = 8",FORMAT_NORMAL);   
  
  //Step 6 :  now end the page
  prt.EndPage();

  //Step 7 :  close the print document
  //          and release it in the spooler
  prt.EndPrint();  
}

You can see a live example (like the picture in this tutorial) in the following code:

CQPrint  prt;
HPRIVATEFONT   hFont,hFont1,hFont2;
CSize  dim;
CRect  margins;



 // initializing the class and printer selection
 if (prt.Dialog() == -1)
     return;

 // create the print document in the spooler
 prt.SetPageOrientation (DMORIENT_PORTRAIT);
 prt.StartPrint();


 // Adding the arial font to the class
 hFont = prt.AddFontToEnvironment("Arial Greek",8,8);
 hFont1 = prt.AddFontToEnvironment("Arial Greek",9,9);
 hFont2 = prt.AddFontToEnvironment("Arial Greek",10,10);

 prt.SetMargins(40,160,200,200);
 prt.SetDistance(5);

 prt.GetDrawDimension(dim);
 prt.GetMargins(margins);
 prt.SetHPos(dim.cy-margins.top);
 // activate a header and footer line
 prt.SetActiveFont (hFont);
 prt.ActivateHF(HeaderFooter);



 // set margins and line spacing

 prt.StartPage();
 // start printing the lines


 prt.Print(hFont,"Set Font Arial Size = 8",FORMAT_NORMAL);
 prt.Print(hFont,"My name is George Papaioannou",FORMAT_NORMAL);
 prt.Line (PS_SOLID);


 prt.Print(hFont1,"Set Font Arial Size = 9",FORMAT_NORMAL);
 prt.Print(hFont1,"My name is  George Papaioannou",FORMAT_NORMAL);
 prt.Line (PS_SOLID);


 prt.Print(hFont2,"Set Font Arial Size = 10",FORMAT_NORMAL);
 prt.Print(hFont2,"My name is  George Papaioannou",FORMAT_NORMAL);
 prt.Line (PS_SOLID);



 // ===================== TABLE 1 ==========================

 prt.lf(hFont2);
 prt.lf(hFont2);
 prt.Print(hFont2,"TABLE 1",FORMAT_CENTER);
 prt.lf(hFont2);
 prt.lf(hFont2);


 int size[5];
 size[0]=800;
 size[1]=400;
 size[2]=400;
 size[3]=400;


 prt.SetTableColumns (3);
 prt.SetTableColumnsSize(size);
 prt.SetTableHorLine (TRUE);
 prt.SetTableVerLine (TRUE);
 prt.SetTableBorderSize (200);
 prt.SetTableJustify(FORMAT_CENTER);
 prt.SetTableBorderLine(5);
 //set the headers
 prt.SetFace (hFont,FACE_NORMALBOLD);
 prt.AddTableRecord (hFont,"NAME|GRADE|CITY",FORMAT_CENTER);
 //set the records
 prt.SetFace (hFont,FACE_NORMAL);
 prt.AddTableRecord (hFont,"George Papaioannou|Grade"
     " A|Lives at Athens (Greece)",FORMAT_CENTER);
 prt.AddTableRecord (hFont,"Kostas Papadimitriou|"
     "Grade B|Lives at Larissa (Greece)",FORMAT_CENTER);

 prt.lf(hFont2);
 prt.lf(hFont2);

 prt.Print(hFont2,"AppWizard has created this PrintJob"
                  " application for you. This application "
                  "not only demonstrates the basics"
                  " of using the Microsoft Foundation classes "
                  "but is also a starting point for"
                  " writing your application.",FORMAT_NORMAL);

 // ===================== TABLE 2 ==========================
 prt.lf(hFont2);
 prt.lf(hFont2);
 prt.Print(hFont2,"TABLE 2",FORMAT_CENTER);
 prt.lf(hFont2);
 prt.lf(hFont2);


 size[0]=800;
 size[1]=400;
 size[2]=600;
 size[3]=600;

 prt.SetTableReset();
 prt.SetTableColumns (4);
 prt.SetTableColumnsSize(size);

 //set the headers
 prt.SetFace (hFont,FACE_NORMALBOLD);
 prt.AddTableRecord (hFont,"NAME|GRADE|CITY|ADDRESS",
                     FORMAT_CENTER);
 //set the records
 prt.SetFace (hFont,FACE_NORMAL);
 prt.AddTableRecord (hFont,"George Papaioannou|Grade A|"
     "Athens (Greece)|23 Octobriou",FORMAT_CENTER);
 prt.AddTableRecord (hFont,"Kostas Papadimitriou|Grade B|"
     "Larissa (Greece)|Irron Polytechneiou",FORMAT_CENTER);


// =================== TABLE 3 ==============================
 prt.lf(hFont2);
 prt.lf(hFont2);
 prt.Print(hFont2,"TABLE 3",FORMAT_CENTER);
 prt.lf(hFont2);
 prt.lf(hFont2);

 size[0]=800;
 size[1]=400;
 size[2]=600;
 size[3]=600;

 prt.SetTableReset();
 prt.SetTableColumns (4);
 prt.SetTableColumnsSize(size);
 prt.SetTableHorLine (TRUE);
 prt.SetTableBorderSize (200);
 prt.SetTableJustify(FORMAT_RIGHT);


 //set the headers
 prt.SetFace (hFont,FACE_NORMALBOLD);
 prt.AddTableRecord (hFont,"NAME|GRADE|CITY|ADDRESS",FORMAT_CENTER);
 //set the records
 prt.SetFace (hFont,FACE_NORMAL);
 prt.AddTableRecord (hFont,"George Papaioannou|Grade A|"
     "Athens (Greece)|23 Octobriou",FORMAT_CENTER);
 prt.AddTableRecord (hFont,"Kostas Papadimitriou|Grade B|"
     "Larissa (Greece)|Irron Polytechneiou",FORMAT_CENTER);

 // now end the page
 prt.EndPage();

 // close the print document and release it in the spooler
 prt.EndPrint();

You can call the following table methods:

CQPrint::SetTableColumns(int Cols)

SetTableColumns sets the table columns.

prt.SetTableColumns(3);

CQPrint::SetTableBorderSize(int Border)

SetTableBorderSize sets the table border space. Default is 50.

prt.SetTableBorderSize(50);

CQPrint::SetTableColumnsSize(int Cols,int size[])

SetTableColumnsSize sets the table column size. The maximum number of columns permitted is 20.

int size[4];
size[0]=800;
size[1]=400;
size[2]=600;
size[3]=600;

prt.SetTableColumnsSize(4,size);

CQPrint::SetTableHorLine(BOOL HLine)

SetTableHorLine sets the horizontal lines. Default is OFF (no lines).

prt.SetTableHorLine(TRUE);

CQPrint::SetTableVerLine(BOOL HLine)

SetTableVerLine sets the vertical lines. Default is OFF (no lines).

prt.SetTableVerLine(TRUE);

CQPrint::SetTableBorderLine(int Border)

SetTableBorderLine sets the width of the border line. Default is 0 (thin line).

prt.SetTableBorderLine(10);

CQPrint::SetTableJustify(int Justification)

SetTableJustify justifies the table. Default is FORMAT_NORMAL. You can also use FORMAT_CENTER and FORMAT_RIGHT.

prt.SetTableJustify(FORMAT_CENTER);

CQPrint::SetTableReset()

SetTableReset resets all the table parameters to the default state. Use this if you don't want to change values when using consecutive tables with different visual parameters.

prt.SetTableReset();

CQPrint::AddTableRecord(HPRIVATEFONT font, CString strText, UINT Format)

AddTableRecord inserts values to the current table. Use the | symbol to change the column in strText.

prt.AddTableRecord (hFont,"Column 1|Column 2|" 
           "Column 3|Column 4",FORMAT_CENTER);

If you want to change the page orientation, use SetPageOrientation before StartPrint. Use the values DMORIENT_PORTRAIT or DMORIENT_LANDSCAPE.

prt.SetPageOrientation (DMORIENT_PORTRAIT);
prt.StartPrint();

You can always use the "original" methods of the class like SetMargins(), Line(), lf() etc. More details about these functions can be found in the demo. You have to experiment a little before you use it. Don't forget that this class also supports bitmap printing with the InsertBitmap() method. I was wondering if there is someone who can extend this class to support other image formats using GDI+. Maybe the next version of QPrint will support print preview as well as image printing (JPG, GIF, PCX etc.), but it's a little bit complicated, since my wife Natasa is already negative to this perspective. So, don't count on this.

Don't forget that you can always use the callback function ActivateHF() that is used to print headers and footers. I have also included this feature in my demo. Anyway, there are more methods in QPrint that you can find in this short tutorial, so start playing with the demo to get an idea of how this stuff works, and then try to discover the more advanced capabilities that this class supports.

Points of Interest

Please, if you see problems with this class, especially with the (LOGPIXELSY, LOGPIXELSX) width and height of the letters, feel free to contact with me.

History

There is no history for the Quick Print (QPrint) class. This is the first and the only version. The base and the original code for this class was from Rob A. Fraydl and is called CPrivatePrint.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer Paradox Interactive
Greece Greece
George Papaioannou, Greece
Application & Multimedia Programmer
http://www.paradoxinteractive.gr

Comments and Discussions

 
GeneralFile saves... Pin
dubu8416-Dec-07 23:50
dubu8416-Dec-07 23:50 
Generalabout Quick Print version 2 Pin
robbinzou31-May-07 14:51
robbinzou31-May-07 14:51 
GeneralRe: about Quick Print version 2 Pin
sdancer753-Jun-07 23:43
sdancer753-Jun-07 23:43 
QuestionSomebody with it an example of printing of a bitmap ? Pin
luckyluche10-May-07 23:29
luckyluche10-May-07 23:29 
Questioncolumn's format? Pin
xiaoyu731110-May-07 20:08
xiaoyu731110-May-07 20:08 
GeneralPrinting on A3 Paper and in color Pin
kristina_carter8-Jan-07 11:22
kristina_carter8-Jan-07 11:22 
GeneralRe: Printing on A3 Paper and in color Pin
sdancer759-Jan-07 2:42
sdancer759-Jan-07 2:42 
GeneralRe: Printing on A3 Paper and in color Pin
kristina_carter11-Jan-07 15:13
kristina_carter11-Jan-07 15:13 
QuestionDon't show the printer properties dialog Pin
sinesis14-Dec-06 0:01
sinesis14-Dec-06 0:01 
AnswerRe: Don't show the printer properties dialog Pin
sw19womble14-Dec-06 1:25
sw19womble14-Dec-06 1:25 
GeneralRe: Don't show the printer properties dialog Pin
sdancer7514-Dec-06 3:13
sdancer7514-Dec-06 3:13 
AnswerRe: Don't show the printer properties dialog Pin
eladelad1122-Feb-10 2:53
eladelad1122-Feb-10 2:53 
GeneralPrinting more than 20 Columns and Printer Set Up Pin
sw19womble13-Dec-06 1:23
sw19womble13-Dec-06 1:23 
GeneralRe: Printing more than 20 Columns and Printer Set Up Pin
sdancer7513-Dec-06 2:51
sdancer7513-Dec-06 2:51 
GeneralRe: Printing more than 20 Columns and Printer Set Up Pin
sw19womble14-Dec-06 1:12
sw19womble14-Dec-06 1:12 
GeneralThanks for you share~ Pin
ituse18-Nov-06 21:23
ituse18-Nov-06 21:23 
Generalprinting bitmap from clipboard Pin
cheheltani3-Oct-06 6:34
cheheltani3-Oct-06 6:34 
QuestionBitmap Printing Pin
Graeme Townsend20-Jul-06 1:40
Graeme Townsend20-Jul-06 1:40 
AnswerRe: Bitmap Printing Pin
sdancer7520-Jul-06 20:06
sdancer7520-Jul-06 20:06 
AnswerRe: Bitmap Printing Pin
sdancer7520-Jul-06 20:18
sdancer7520-Jul-06 20:18 
GeneralRe: Bitmap Printing Pin
fikrip17-Jan-08 16:11
fikrip17-Jan-08 16:11 
GeneralBuild as dll Pin
gehossafats18-Jul-06 21:06
gehossafats18-Jul-06 21:06 
Questionhow about preview? Pin
dhdream0078-Jul-06 19:38
dhdream0078-Jul-06 19:38 
GeneralLandscape Pin
Gerard Lim24-May-06 1:13
Gerard Lim24-May-06 1:13 
GeneralRe: Landscape Pin
sdancer756-Jun-06 22:03
sdancer756-Jun-06 22:03 

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.