I need to draw text in the center of a box. The text must be drawn horizontally aligned with one char on each line.
I have attached an example with the string, "class".
I can draw the single char but I hope there is a faster way to do it.
I need to draw text in the center of a box. The text must be drawn horizontally aligned with one char on each line.
I have attached an example with the string, "class".
I can draw the single char but I hope there is a faster way to do it.
procedure DrawVert(Canvas: TCanvas; Box: TRect; const Text: string);
var
i: Integer;
s: string;
R: TRect;
begin
s := '';
for i := 1 to Length(Text) do
s := s + Text[i] + ' ';
R := Rect(0, 0, 1, 0);
Canvas.TextRect(R, s, [tfCalcRect, tfNoClip, tfWordBreak]);
Box.Left := Box.Left + (Box.Right - Box.Left - R.Right) div 2;
Box.Top := Box.Top + (Box.Bottom - Box.Top - R.Bottom) div 2;
Box.Right := Box.Left + R.Right;
Box.Bottom := Box.Top + R.Bottom;
Canvas.TextRect(Box, s, [tfWordBreak]);
end;
For testing, let PaintBox1 is the box we're painting on
procedure TForm1.Button1Click(Sender: TObject);
begin
DrawVert(PaintBox1.Canvas, PaintBox1.Canvas.ClipRect, 'CLASS TEST');
end;