?
Solved

Printing Lines from a TMemo object

Posted on 2006-05-07
2
Medium Priority
?
228 Views
Last Modified: 2010-04-04
I am able to get my printer to print a blank page, but I would rather have it print the text I want.

I have tried a number of variations, but it boils down to this, which prints a blank page.

procedure TfrmHelp.btPrintClick(Sender: TObject);
var
  I,
  Start,
  Stop: integer;

begin
  if dlgPrint.Execute then
    begin
      with Printer do
      begin
        dlgPrint.PrintRange := prAllPages;
        BeginDoc;

        EndDoc;
      end;
    end;
    ShowMessage('Printing?');
end;

Somehow I need to specify that I want to include memHelp.Lines as the text to be printed. memHelp: TMemo.

I am not stuck on any print routine so if there is a better way to do it, that is fine, too.

Thanks,
John Fistere
0
Comment
Question by:John Fistere
2 Comments
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 500 total points
ID: 16626916
here's a couple of ways to use your procedure, and another method


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Printers;

type
  TfrmHelp = class(TForm)
    btPrint: TButton;
    memHelp: TMemo;
    dlgPrint: TPrintDialog;
    btnPrint2: TButton;
    procedure btPrintClick(Sender: TObject);
    procedure btnPrint2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmHelp: TfrmHelp;

IMPLEMENTATION

{$R *.DFM}

procedure TfrmHelp.btPrintClick(Sender: TObject);
    const
        startx = 10;
        starty = 10;
    var
        I: integer;
        rowsdone: integer;
        currenty: integer;
    begin
        if dlgPrint.Execute then
        begin
            with Printer do
            begin
                Canvas.Font := memHelp.Font;  {use the same font as is in the TMemo}
                dlgPrint.PrintRange := prAllPages;
                BeginDoc;
// the following line only will print the TMemo as displayed on screen, but i dont think thats what you really want
   //     memHelp.PaintTo(Handle, 10, 10);
// the following 4 lines will manually print the lines, and do a new page every 20 lines
                rowsdone := 0;
                currenty := starty;
                for i := 0 to pred(memHelp.Lines.Count) do
                begin
                    Canvas.TextOut(startx, currenty, memHelp.Lines[i]); // canvas is printer.canvas, ie the page in the printer! :-)
                    currenty := currenty +
                    canvas.TextHeight(memHelp.Lines[i]) + // get height of the text on screen
                    4; // space of 4 between each row
                    if (i mod 20) = 0 then
                    begin
                        NewPage;
                        currenty := starty;
                    end;
                end;
                EndDoc;
            end;
        end;
    end;

// The following procedure shows a nother very basic way of printing the contents of a TMemo
procedure TfrmHelp.btnPrint2Click(Sender: TObject);
    var
        Line: Integer;
        PrintText: TextFile;   {declares a file variable}
    begin
        if dlgPrint.Execute then
        begin
            AssignPrn(PrintText);   {assigns PrintText to the printer}
            Rewrite(PrintText);     {creates and opens the output file}
            Printer.Canvas.Font := memHelp.Font;  {assigns Font settings to the canvas}
            for Line := 0 to memHelp.Lines.Count - 1 do
             
            Writeln(PrintText, memHelp.Lines[Line]);{writes the contents of the Memo1 to the printer object}
            CloseFile(PrintText); {Closes the printer variable}
        end;
    end;

end.
0
 
LVL 2

Author Comment

by:John Fistere
ID: 16628120
Outstanding, Loki!

I pasted your second routine in place of what I had, and it compiled and worked without me having to touch a thing.

I will have to change your 4-character indent to my 2-character, though.   :-)

Thanks, and cheers,
John Fistere
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Loops Section Overview
Suggested Courses
Course of the Month15 days, 9 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question