Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print a form?

Status
Not open for further replies.

pierrotsc

Programmer
Nov 25, 2007
358
US
Does anybody know where i can find codes on how to enable print functions? I want to be able to print the form with that is currently on the form.
Thanks
 
Code:
procedure TForm1.PrintButtonClick(Sender: TObject);
var
  i, j: integer;
  linesize: integer;
  textsize: integer;
  pagesize: integer;
  hmargin, vmargin: integer;
begin
  if PrintDialog1.Execute then begin
    Printer.Title := 'Sample Text File Printer';
    Printer.Canvas.Font := Memo1.Font;
    pagesize := Printer.PageHeight;
    for j := 1 to Printer.Copies do
      begin
        Printer.BeginDoc;
        hmargin := GetDeviceCaps(Printer.Canvas.Handle, LOGPIXELSX) -
                   GetDeviceCaps(Printer.Canvas.Handle, PHYSICALOFFSETX);
        vmargin := GetDeviceCaps(Printer.Canvas.Handle, LOGPIXELSY) -
                   GetDeviceCaps(Printer.Canvas.Handle, PHYSICALOFFSETY);
        StatusLabel.Caption := 'Printing Page ' + IntToStr(Printer.PageNumber);
        linesize := vmargin;
        for i := 0 to Memo1.Lines.Count-1 do
          begin
            textsize := Printer.Canvas.TextHeight(Memo1.Lines.Strings[i]);
            if linesize+textsize+vmargin >= pagesize then
              begin
                Printer.NewPage;
                StatusLabel.Caption := 'Printing Page ' + IntToStr(Printer.PageNumber);
                linesize := vmargin;
              end;
            Printer.Canvas.TextOut(hmargin, linesize, Memo1.Lines.Strings[i]);
            inc(linesize, textsize);
          end;
        Printer.EndDoc;
      end;
    StatusLabel.Caption := 'File Printed.';
  end;
end;

will be useful for working with printers. The object involved is accessed using the Printers unit. As you will see in the code, it is drawing to a canvas like TImage and TForm. Copy the canvas from either of those things to the printer canvas and then use the functions involved and you'll get your form printed. GetDeviceCaps returns the printing area of the printer so you can devise margins and so forth in your code.

LOGPIXELSX - number of pixels per inch, horizontal.
PHYSICALOFFSETX - number of pixels outside the printable area, horizontally.
LOGPIXELSY - number of pixels per inch, vertical.
PHYSICALOFFSETY - number of pixels outside the printable area, vertically.
 
Thanks I get an error at the line that starts with Printer. I tried to declare a variable printer:tprinter; but i get an error on tprinter.
What am i doing wrong?
Thanks.
 
Sorry, i forgot to add the uses printers clause. But what about memo1 and statuslabel? What clause should I add?
Thanks.
 
do you have components on your form named memo1 and statuslabel? StatusLabel is just a label of some kind showing the user what the current page is that is printing...

I think Glenn was just trying to give you an idea...looks like he's printing what is in the memo box...

Leslie

In an open world there's no need for windows and gates
 
The defined objects in the form this code runs on:

Printer comes defined when you put "printers" in as a unit.
Memo1 is a TMemo component added to the code's associated form.
StatusLabel is a TLabel component added to the same form.
PrintDialog1 is a printer dialog component.

But for your form example you'll have to rework this to match what you are doing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top