Click here to Skip to main content
6,306,412 members and growing! (18,199 online)
Email Password   helpLost your password?
Desktop Development » Printing » General     Intermediate License: The Code Project Open License (CPOL)

Quick Print

By sdancer75

An easy way to print without the doc/view architecture.
VC6Win2K, WinXP, MFC, Dev
Posted:5 May 2006
Views:52,939
Bookmarked:60 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
18 votes for this article.
Popularity: 5.89 Rating: 4.69 out of 5
1 vote, 5.6%
1

2
1 vote, 5.6%
3
1 vote, 5.6%
4
15 votes, 83.3%
5

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)

About the Author

sdancer75


Member
George Papaioannou, Greece
Application & Multimedia Programmer

He is a graduate of the Automation Department of Technological Education Institute of Thessaloniki (Greece), specialized in the Industrial Automation (SCADA, embedded Microcontrollers & PLCs) & Programmer / Analyzer of the Aristotelian University specialized in Telecommunication & Computer Networks. He has a great experience in Programming and Systems Analysis on a variety of computer languages like Assembly, C++, Cobol, Fortran, Pascal and Java. He has also worked in researching programs on appreciation and improvement algorithms for detection systems through a coordinated range of Satellite Systems (Global Positioning Systems). He is the Development Manager of his own company "Paradox Interactive", http://www.paradoxinteractive.gr

Occupation: Web Developer
Company: Paradox Interactive
Location: Greece Greece

Other popular Printing articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 34 (Total in Forum: 34) (Refresh)FirstPrevNext
Questionproblem in [Insert Bitmap] , [Text utf-8 FONT] print [modified] Pinmembermohammadmot21:11 18 Jan '09  
AnswerRe: problem in [Insert Bitmap] , [Text utf-8 FONT] print Pinmembermohammadmot4:47 1 Feb '09  
GeneralProblem compiling QPrint in VS2005. Pinmemberldsdbomber1:16 2 Jan '09  
GeneralRe: Problem compiling QPrint in VS2005. Pinmembericryu13:45 28 Jan '09  
GeneralMultiple lines in a cell PinmemberMember 381507218:46 20 Nov '08  
GeneralAutomatic set column width of Table ! Pinmember"_$h@nky_"1:05 19 Aug '08  
Generalprinters on c#.net Pinmemberbalu123458:47 30 Jul '08  
QuestionProblem of Print Preview. Pinmember"_$h@nky_"19:09 9 Jul '08  
GeneralFile saves... Pinmemberdubu840:50 17 Dec '07  
Generalabout Quick Print version 2 Pinmemberrobbinzou15:51 31 May '07  
GeneralRe: about Quick Print version 2 Pinmembersdancer750:43 4 Jun '07  
GeneralSomebody with it an example of printing of a bitmap ? Pinmemberluckyluche0:29 11 May '07  
Questioncolumn's format? Pinmemberxiaoyu731121:08 10 May '07  
GeneralPrinting on A3 Paper and in color Pinmemberkristina_carter12:22 8 Jan '07  
GeneralRe: Printing on A3 Paper and in color Pinmembersdancer753:42 9 Jan '07  
GeneralRe: Printing on A3 Paper and in color Pinmemberkristina_carter16:13 11 Jan '07  
QuestionDon't show the printer properties dialog Pinmembersinesis1:01 14 Dec '06  
AnswerRe: Don't show the printer properties dialog Pinmembersw19womble2:25 14 Dec '06  
GeneralRe: Don't show the printer properties dialog Pinmembersdancer754:13 14 Dec '06  
GeneralPrinting more than 20 Columns and Printer Set Up Pinmembersw19womble2:23 13 Dec '06  
GeneralRe: Printing more than 20 Columns and Printer Set Up Pinmembersdancer753:51 13 Dec '06  
GeneralRe: Printing more than 20 Columns and Printer Set Up Pinmembersw19womble2:12 14 Dec '06  
GeneralThanks for you share~ Pinmemberituse22:23 18 Nov '06  
Generalprinting bitmap from clipboard Pinmembercheheltani7:34 3 Oct '06  
QuestionBitmap Printing PinmemberGraeme Townsend2:40 20 Jul '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 5 May 2006
Editor: Smitha Vijayan
Copyright 2006 by sdancer75
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project