我如何用按钮单击[关闭]在一张纸上打印出来

问题描述 投票:-2回答:1

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

让我给你一个例子:

通过按钮OnClick事件,代码将随机地将某些图像彼此相邻放置。每次单击,我都希望将那些图片(相同或不同的按钮)打印在一张纸上。

我尝试在工具栏中搜索与Delphi和打印机的任何连接,但没有运气。

image delphi printing delphi-7
1个回答
1
投票
这是一个非常简单的示例:

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;

这将显示以下页面:

A screenshot of the Microsoft Windows XPS viewer displaying the printed page produced by the above Delphi code snippet.

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

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

当然,您可以在单击按钮时调用此过程:

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

© www.soinside.com 2019 - 2024. All rights reserved.