Printing Lines from a TMemo object

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
LVL 2
John FistereEngineer, retiredAsked:
Who is Participating?
 
TheRealLokiConnect With a Mentor Senior DeveloperCommented:
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
 
John FistereEngineer, retiredAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.