我正在尝试使用 Delphi 应用程序设置打印的列宽。无论我为字符串输入什么,都不会减少宽度。实际上我不明白为什么该属性返回一个字符串,它应该返回以像素为单位的宽度。
我的代码是
Printer.Canvas.TextWidth('M');
编辑:我知道它不返回字符串,但是“M”是什么意思?我想做的是使列变窄。我的代码位于 sudrap.org/paste/text/19688
编辑:恐怕我无法清楚地解释问题,对不起。 我希望它像这样打印:
不是这样的:
最佳答案
尝试检查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/