views:

82

answers:

1

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?

+5  A: 

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

Daniel Luyo
Not the answer I was hoping for, but it seems correct. When I use tfSingleLine it does indeed place the text in the vertical center, but I loose the word-wrapping. I'll have to do it the hard way then :-/ But thanks anyway :-)
Svein Bringsli