Draw centered and middle text with one char per line on canvas with Delphi

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.

Answers


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;

Need Your Help

Can JSON-LD be used to auto update a merchant feed?

feed schema.org json-ld google-product-search

Will Google read Schema.org markup in the form of JSON-LD to automatically update a product feed?

SQL union between NULL and VARCHAR error

sql sql-server-2008 tsql sql-server-2005 null

I have two views with identical columns. One of the columns on the first view is generated 'on the fly' and set as NULL and the same column on the other view has values stored as varchar. I have a ...