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

PageOutputStream causing java.lang.OutOfMemory

java tomcat struts2 heap out-of-memory

We have a struts2 web application running in Tomcat 7 that is intermittently experiencing the same problem:

Hibernate SELECT on Composite Key gives unexpected char error

java hibernate composite-key

I have a table called printer with 3 varchar2 fields making a composite id, a Printer.java class, and a PrinterID.java class (for the composite ID).

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.