Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
How to printing a Memo Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Reporting /Printing
Delphi 2.x
User Rating
No Votes
# Votes
DSP, Administrator
Reference URL:
			Author: Tomas Rutkauskas

I have a simple editor unit with a TMemo component whose text I want to send to the 
printer. How can I do this?


This is actually much easier that most people think, though you can get pretty 
fancy. With the procedure that I'll show you below, I will take advantage of the 
TMemo's Lines property, which is of type TStrings. The procedure will parse each 
line in the memo, and use Canvas.TextOut to print to the printer. After you see 
this code, you'll see how simple it is. Let's take a look at the code:
2   procedure PrintTStrings(Lst: TStrings);
3   var
4     I, Line: Integer;
5   begin
6     I := 0;
7     Line := 0;
8     Printer.BeginDoc;
9     for I := 0 to Lst.Count - 1 do
10    begin
11      Printer.Canvas.TextOut(0, Line, Lst[I]);
13      {Font.Height is calculated as -Font.Size * 72 / Font.PixelsPerInch which returns
14       a negative number. So Abs() is applied to the Height to make it a non-negative
15       value}
16      Line := Line + Abs(Printer.Canvas.Font.Height);
17      if (Line >= Printer.PageHeight) then
18        Printer.NewPage;
19    end;
20    Printer.EndDoc;
21  end;

Basically, all we're doing is sequentially moving from the beginning of the 
TStrings object to the end with the for loop. At each line, we print the text using 
Canvas.TextOut then perform a line feed and repeat the process. If our line number 
is greater than the height of the page, we go to a new page. Notice that I 
extensively commented before the line feed. That's because feeding a line was the 
only tricky part of the code. When I first wrote this, I just added the Font height 
to the line, and thus the code would generate a smaller and smaller negative 
number. The net result was that I'd only print one line of the memo. Actually 
TextOut would output to the printer, but it essentially printed from the first line 
up, not down. So, after carefully reading the help file, I found that Height is the 
result of the calculation of a negative font size, so I used the Abs() function to 
make it a non-negative number.

For more complex operations, I suggest you look at the help file under Printer or TPrinter, and also study the TextOut procedure. Now, what is Printer? Well, when you make a call to Printer, it creates a global instance of TPrinter, which is Delphi's interface into the Windows print functions. With TPrinter, you can define everything which describes the page(s) to print: Page Orientation, Font (through the Canvas property), the Printer to print to, the Width and Height of the page, and many more things.

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC