TextFont example for C++ Builder
This example, in an OnMouseUp 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.
void __fastcall TForm1::Lead1MouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
const AnsiString OutputString = "This is my text.";
int TextWidth, TextHeight;
/*Specify the font, and styles*/
Lead1->TextFont->Name = "Times New Roman";
Lead1->TextFont->Size = 16;
Lead1->TextFont->Color = (TColor)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 = (TColor)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);
}