Delphi Printer.Canvas.TextWidth 属性

标签 delphi printing canvas text-width

我正在尝试使用 Delphi 应用程序设置打印的列宽。无论我为字符串输入什么,都不会减少宽度。实际上我不明白为什么该属性返回一个字符串,它应该返回以像素为单位的宽度。

我的代码是

Printer.Canvas.TextWidth('M');

编辑:我知道它不返回字符串,但是“M”是什么意思?我想做的是使列变窄。我的代码位于 sudrap.org/paste/text/19688

编辑:恐怕我无法清楚地解释问题,对不起。 我希望它像这样打印:

enter image description here

不是这样的: enter image description here

最佳答案

尝试检查TextRect功能。使用此函数,您可以指定应打印文本的目标矩形,以便缩小列的范围。

uses Graphics;

var
  Text: string;
  TargetRect: TRect;
begin
  Printer.BeginDoc;

  Text := 'This is a very long text';

  // now I'll specify the rectangle where the text will be printed
  // it respects the rectangle, so the text cannot exceed these coordinates
  // with the following values you will get the column width set to 50 px

  TargetRect := Rect(Margin, Y, Margin + 50, Y + LineHeight);

  Printer.Canvas.Font.Size := 11;
  Printer.Canvas.Font.Name := 'Arial';
  Printer.Canvas.Font.Style := [fsBold];
  Printer.Canvas.TextRect(TargetRect, Text);

  Printer.EndDoc;
end;

除此之外,您还可以通过 TextRect 获得formatting flags的功能集这可以帮助您指定例如文本对齐、自动换行等。例如,如果您想将文本在指定的矩形 [100;100]、[250;117] 中水平居中,您可以使用以下内容。

Text := 'Centered text';
TargetRect := Rect(100, 100, 250, 117);
Printer.Canvas.TextRect(TargetRect, Text, [tfCenter]);

或者在你的情况下可能是更有用的自动换行。这是一个矩形 [100;100], [200;134] 的示例,其中文本自动由 TextRect 换行功能。

Text := 'This is a very long text';
TargetRect := Rect(100, 100, 200, 134);
Printer.Canvas.TextRect(TargetRect, Text, [tfWordBreak]);

关于Delphi Printer.Canvas.TextWidth 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6867689/

相关文章:

javascript - pjs 草图和 html Canvas

delphi - 如何将Jedi的帮助文件与Delphi的IDE集成?

c# - 在带预览的纸质表格上打印

javascript - 使用 <link rel ="alternate"media ="print"> 打印外部网页

javascript - Canvas 调整大小甩掉听众 x y

javascript - 根据方向和距离(向量)获取点坐标

oracle - 使用 Delphi 数据感知组件 - 优点和缺点

android - 从 Firemonkey Multi Device Delphi 项目中的函数获取模态结果

flash - 生成大量位图时,创建的位图会变成全黑

python - 如何在 tkinter 中通过本地或网络打印机进行打印