单击按钮如何在纸上打印 [英] How do I print out on a piece of paper with a button click

查看:83
本文介绍了单击按钮如何在纸上打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找使用按钮 OnClick 事件打印页面的解决方案。

I'm searching for a solution on printing out a page using a button OnClick event.

让我举一个例子:

使用按钮 OnClick 事件,代码会将某些图像随机放置在每个图像旁边其他。每次单击时,我都希望将那些图片(相同或不同的按钮)打印在一张纸上。

With a button OnClick event, the code will randomly place certain images next to each other. With every click, I want those pictures (either the same or a different button) printed out on a piece of paper.

我尝试搜索与Delphi和打印机的任何连接

I tried searching for any connection with Delphi and printer in toolbar, but had no luck.

推荐答案

原则上,使用Delphi进行打印非常容易。基本上,就像使用VCL(即使用Windows GDI)在屏幕上的画布上一样,在页面的画布上进行绘制。

In principle, it is very easy to print using Delphi. You basically draw on the page's canvas as you'd draw on an on-screen canvas using the VCL (that is, using the Windows GDI).

简单的例子:

procedure PrintRects;
const
  Offset = 100;
  RectCountY = 8;
  RectCountX = 4;
var
  S: string;
  TitleRect: TRect;
  MainRect: TRect;
  j: Integer;
  i: Integer;
  RectWidth,
  RectHeight: Integer;
  R: TRect;

  function GetRectRect(X, Y: Integer): TRect;
  begin
    Result := Rect(
      MainRect.Left + X * RectWidth,
      MainRect.Top + Y * RectHeight,
      MainRect.Left + (X + 1) * RectWidth,
      MainRect.Top + (Y + 1) * RectHeight
    );
  end;

begin

  with TPrintDialog.Create(nil) do
    try
      if not Execute then
        Exit;
    finally
      Free;
    end;


  Printer.BeginDoc;
  try

    Printer.Canvas.Font.Size := 42;
    S := 'My Collection of Rects';

    TitleRect := Rect(
      Offset,
      Offset,
      Printer.PageWidth - Offset,
      Offset + 2 * Printer.Canvas.TextHeight(S)
    );

    MainRect := Rect(
      Offset,
      TitleRect.Bottom + Offset,
      Printer.PageWidth - Offset,
      Printer.PageHeight - Offset
    );

    RectWidth := MainRect.Width div RectCountX;
    RectHeight := MainRect.Height div RectCountY;

    Printer.Canvas.TextRect(TitleRect, S, [tfSingleLine, tfCenter, tfVerticalCenter]);

    for j := 0 to RectCountY - 1 do
      for i := 0 to RectCountX - 1 do
      begin
        R := GetRectRect(i, j);
        Printer.Canvas.Brush.Color := RGB(Random(255), Random(255), Random(255));
        Printer.Canvas.FillRect(R);
      end;

  finally
    Printer.EndDoc;
  end;

end;

这将产生以下页面:

不用说,而不是固定的彩色矩形,您可以在此网格中在此处打印图像。

Needless to say, instead of solid-colour rectangles, you could print your images here in this grid.

因此,如果您知道如何在表单上绘制内容(使用 TCanvas ,即Windows GDI),您可以使用相同的方法在打印的页面上绘制。

Hence, if you know how to draw things on a form (using TCanvas, that is, Windows GDI), you can use the same methods to draw on a printed page.

当然,您可以调用单击按钮时执行以下过程:

And of course, you can call this procedure when you click a button:

procedure TForm1.Button1Click(Sender: TObject);
begin
  PrintRects;
end;

这篇关于单击按钮如何在纸上打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆