Delphi Printer.Canvas.TextWidth 属性

发布于 2024-11-27 01:08:33 字数 533 浏览 1 评论 0原文

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

我的代码是

Printer.Canvas.TextWidth('M');

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

编辑:恐怕我不能没有把问题解释清楚,抱歉。 我希望它像这样打印:

在此处输入图像描述

不是这样: 在此处输入图像描述

I'm trying to set the column width for printing with my Delphi application. Whatever I type for the string doesn't make the width fewer. Actually I don't understand why the property returns a string, it should return width in pixels.

My code is

Printer.Canvas.TextWidth('M');

Edit: i understood it doesn't return a string but what does 'M' mean? what i m trying to do is making a column narrower. my code is located at sudrap.org/paste/text/19688

Edit: i m afraid i couldn t explain the problem clearly, i m sorry. i want it to print like this:

enter image description here

not like this:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

﹎☆浅夏丿初晴 2024-12-04 01:08:33

尝试检查 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功能设置href="http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/Graphics_TTextFormats.html" rel="nofollow">格式化标志可以帮助您指定例如文本对齐、自动换行等。例如,如果您希望将文本在指定的矩形 [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]);

Try to check TextRect function. Using this function you can specify the target rectangle where the text should be printed, so you can narrow your column.

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;

Except this you can get with the TextRect function set of the formatting flags which can help you to specify e.g. text alignment, word wrap etc. For instance if you would like to center the text horizontally in the specified rectangle [100;100], [250;117] you can use the following.

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

Or in your case might be more useful word wrap. Here's an example with rectangle [100;100], [200;134] where the text is automatically wrapped by the TextRect function.

Text := 'This is a very long text';
TargetRect := Rect(100, 100, 200, 134);
Printer.Canvas.TextRect(TargetRect, Text, [tfWordBreak]);
陌若浮生 2024-12-04 01:08:33

如果在画布上使用固定宽度字体,则所有单字符字符串应该得到相同的结果。如果您使用可变宽度字体,每个字符将返回不同的宽度。

Printer.Canvas.Font.Name = 'Courier New';
Printer.Canvas.Font.Size = 13;
ColumnWidth := Printer.Canvas.TextWidth('M');

对于不同的字体或不同的字体大小,你会得到不同的结果。

If you use a fixed width font on the canvas, you should get the same result for all single-character strings. If you use a variable width font, each character will return a different width.

Printer.Canvas.Font.Name = 'Courier New';
Printer.Canvas.Font.Size = 13;
ColumnWidth := Printer.Canvas.TextWidth('M');

For different fonts or different font sizes, you will get different results.

小瓶盖 2024-12-04 01:08:33

我不明白你说它如何返回文本。如果它返回文本,您的代码甚至无法编译,当您尝试将数字乘以文本时,您会收到错误。您甚至可以将其转换为字符串以用于显示目的。

您是否被以下事实误导:使用可变宽度字体,您会为不同的字符串得到不同的答案?对于相同的字母,您甚至可以以不同的顺序得到不同的答案。对于某些字体,“WAM”将产生与“WMA”不同的答案,因为 W 和 A 的组合方式不同。

另外,您只是假设您的标签比 15 M 窄。虽然通常是这种情况,但这不是良好的编程实践。相反,您应该询问每个标签的宽度并使用略高于最大答案的宽度。

最后,您对 LineHeight 的处理非常糟糕。如果您确实想要的话,只需将 300 添加到 y 即可,尽管它应该是行高的倍数,而不是固定值。在具有不同 DPI 设置的打印机上,您的代码会得到非常不同的结果。

您是否尝试过使用调试器单步执行此代码以查看内部发生了什么?您将位置输出到打印输出表明您没有使用调试器。

I don't see how you're saying it returns text. If it were returning text your code wouldn't even compile, you would be getting errors when you tried to multiply a number by text. You even convert it to a string for display purposes.

Are you being mislead by the fact that with a variable-width font that you'll get different answers for different strings? You can even get different answers for the same letters in a different order. For some fonts "WAM" will produce a different answer than "WMA" because of how the W and A fit together.

Also, you're simply assuming that your labels are narrower than 15 M's. While this is generally the case it's not good programming practice. Instead, you should be asking for the width of each label and using something a bit above the biggest answer.

Finally, your handling of LineHeight is atrocious. Simply add 300 to y if that's what you really want although it should be some multiple of your line height, not a fixed value. You'll get VERY different results from your code off printers with different DPI settings.

Have you even tried stepping through this code with the debugger to see what's going on internally? Your output of the position to the printout suggests you aren't using the debugger.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文