Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 615
Svein Bringsli Avatar asked Sep 06 '10 18:09

Svein Bringsli


1 Answers

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 look at Windows DrawText function you will see this restriction. If you need to vertical center you have to do your own maths

like image 159
Daniel Luyo Avatar answered Oct 19 '22 14:10

Daniel Luyo