打印TMemo的内容到打印机

Canvas.TextOut真是好用,Printer也实在好用:

procedure PrintTStrings(Lst : TStrings) ;
var
  I,
  Line : Integer;
begin
  I := 0;
  Line := 0 ;
  Printer.BeginDoc ;
  // TMemo会按照当前宽度自动换行,所以粘帖不换行的文字也没有用
  for I := 0 to Lst.Count - 1 do
  begin
    Printer.Canvas.TextOut(0, Line, Lst[I]); // 以(0,Line)为起点坐标,输出一行文字,真是好用

    {Font.Height is calculated as -Font.Size * 72 / Font.PixelsPerInch which returns
     a negative number. So Abs() is applied to the Height to make it a non-negative value}
    Line := Line + Abs(Printer.Canvas.Font.Height); // 计算行高
    if (Line >= Printer.PageHeight) then
      Printer.NewPage;
  end;
  Printer.EndDoc;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  PrintTStrings(Memo1.Lines);
end;

 

你可能感兴趣的:(打印)