Delphi Printer.Canvas.TextWidth 属性
我正在尝试使用 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:
not like this:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试检查 TextRect 函数。使用此函数,您可以指定应打印文本的目标矩形,以便缩小列的范围。
除此之外,您可以通过 TextRect功能设置href="http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/Graphics_TTextFormats.html" rel="nofollow">格式化标志可以帮助您指定例如文本对齐、自动换行等。例如,如果您希望将文本在指定的矩形 [100;100] 中水平居中, [250;117] 您可以使用以下内容。
或者在你的情况下可能是更有用的自动换行。这是一个矩形 [100;100], [200;134] 的示例,其中文本由 TextRect 函数。
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.
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.
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.
如果在画布上使用固定宽度字体,则所有单字符字符串应该得到相同的结果。如果您使用可变宽度字体,每个字符将返回不同的宽度。
对于不同的字体或不同的字体大小,你会得到不同的结果。
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.
For different fonts or different font sizes, you will get different results.
我不明白你说它如何返回文本。如果它返回文本,您的代码甚至无法编译,当您尝试将数字乘以文本时,您会收到错误。您甚至可以将其转换为字符串以用于显示目的。
您是否被以下事实误导:使用可变宽度字体,您会为不同的字符串得到不同的答案?对于相同的字母,您甚至可以以不同的顺序得到不同的答案。对于某些字体,“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.