TextFont example for Delphi
This example, in a MouseUp event, determines the size of a rectangle for a string in the specified font. It then draws the rectangle and draws the text on top of the rectangle.
procedure TForm1.Lead1MouseUp (Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
const
OutputString = 'This is my text.';
var
TextWidth, TextHeight : Integer;
begin
{Specify the font, and styles}
Lead1.TextFont.Name := 'Times New Roman';
Lead1.TextFont.Size := 16;
Lead1.TextFont.Color := RGB(0, 0, 255); {Blue}
Lead1.TextStyle := etsNormal;
Lead1.TextAlign := etaHcenterVcenter;
{Get the width and height of the text to be drawn.}
TextWidth := Lead1.DrawTextWidth(OutputString);
TextHeight := Lead1.DrawTextHeight (OutputString);
{set the location for the text}
Lead1.TextTop := y;
Lead1.TextLeft := x;
Lead1.TextWidth := TextWidth;
Lead1.TextHeight := TextHeight;
{Set the properties for drawing a rectangle behind the text.}
Lead1.DrawPenStyle := psSolid;
Lead1.DrawMode := pmCopy;
Lead1.DrawFillColor := RGB(0, 255, 0); {Green}
{Draw on the screen, not on the bitmap.}
Lead1.DrawPersistence := False;
{Draw the rectangle, then draw the text.}
Lead1.DrawRectangle(x, y, TextWidth, TextHeight);
Lead1.DrawTextStr(OutputString, 0);
end;