考虑以下代码:
procedure TForm1.PaintBox1Paint(Sender: TObject);
var
vRect : TRect;
vFormat : TTextFormat;
vStr : string;
begin
vStr := 'This is some text';
vRect := rect(10,10,50,130);
vFormat := [tfCenter,tfVerticalCenter,tfWordBreak];
PaintBox1.Canvas.Rectangle(vRect);
PaintBox1.Canvas.TextRect(vRect,vStr,vFormat);
end;
我希望有这样的事情
+--------+
| |
| |
|This is |
| some |
| text |
| |
| |
+--------+
但是我明白了
+--------+
|This is |
| some |
| text |
| |
| |
| |
| |
+--------+
tfBottom 格式也是如此。水平文本格式(tfLeft、tfRight、tfCenter)按预期工作,但垂直格式则不然。谁能解释一下吗?
最佳答案
我不太确定,但仅当设置了单行时才允许使用 VerticalCenter。 这是因为 Canvas 依赖于 native Windows 函数。如果您查看 Windows DrawText 函数,您将看到此限制。 如果你需要垂直居中,你必须自己计算
关于delphi - 为什么 tfVerticalCenter 和 tfBottom 在 TCanvas.TextRect 中不能按预期工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3653632/