Why don't tfVerticalCenter and tfBottom work as expected in TCanvas.TextRect?

不羁岁月 提交于 2019-12-12 10:58:09

问题


Consider the following code:

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;

I would expect something like this

+--------+
|        |
|        |
|This is |
|  some  |
|  text  |
|        |
|        |
+--------+

but I get this

+--------+
|This is |
|  some  |
|  text  |
|        |
|        |
|        |
|        |
+--------+

The same is true for the tfBottom format. The horizontal text formats (tfLeft, tfRight, tfCenter) work as expected, but the vertical formats doesn't. Can anyone explain this?


回答1:


I'm not pretty sure but VerticalCenter is only allowed if singleline is set. This is because Canvas relies on native windows functions. If you llok at Windows DrawText function you will see this restriction. If you need to vertical center you have to do your own maths



来源:https://stackoverflow.com/questions/3653632/why-dont-tfverticalcenter-and-tfbottom-work-as-expected-in-tcanvas-textrect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!