//第一種重載的聲明:TextRect(
{如果要輸出的文本超出指定的矩形將不被顯示}
Rect: TRect; {矩形}
X, Y: Integer; {相對於窗體的位置}
const Text: string {文本}
);
//第一種重載的舉例:procedure TForm1.Button1Click(Sender: TObject);
//效果圖:
const
R: TRect = (Left:0; Top:0; Right:60; Bottom:60);
s = '萬一的 Delphi 博客';
begin
Canvas.Brush.Color := clWhite;
Canvas.FillRect(R);
Canvas.TextRect(R, 10, 10, s);
end;
//第二種重載的聲明:TextRect(
{參數三是 TTextFormat 類型的集合, 它定義如下:}
var Rect: TRect; {矩形范圍}
var Text: string; {文本}
TextFormat: TTextFormat = [] {選項; 集合類型, 默認值是個空的集合}
);TTextFormat = set of TTextFormats;
{所以參數三可以是下列值之一或它們的組合}
{TTextFormats 是個枚舉, 定義如下}
TTextFormats = (tfBottom, tfCalcRect, tfCenter, tfEditControl, tfEndEllipsis,
tfPathEllipsis, tfExpandTabs, tfExternalLeading, tfLeft, tfModifyString,
tfNoClip, tfNoPrefix, tfRight, tfRtlReading, tfSingleLine, tfTop,
tfVerticalCenter, tfWordBreak);tfBottom
//效果圖:
tfCalcRect
tfCenter
tfEditControl
tfEndEllipsis
tfPathEllipsis
tfExpandTabs
tfExternalLeading
tfLeft
tfModifyString
tfNoClip
tfNoPrefix
tfRight
tfRtlReading
tfSingleLine
tfTop
tfVerticalCenter
tfWordBreak
//這個函數是 API 函數 DrawTextEx 的簡化, 可以參見: DrawTextEx
//第二種重載的舉例1:
{默認是頂對齊、左對齊; 前兩個參數都是傳址的, 不能用常量}
procedure TForm1.FormPaint(Sender: TObject);
var
R: TRect;
S: string;
begin
R := ClIEntRect;
InflateRect(R,-10,-10); {把矩形縮小一圈}
S := 'Delphi';
Canvas.Brush.Color := clWhite;
Canvas.FillRect(R);
Canvas.TextRect(R, S);
end;
//第二種重載的舉例2:
{單行文本、水平居中、垂直居中}procedure TForm1.FormPaint(Sender: TObject);
//效果圖:
var
R: TRect;
S: string;
begin
R := ClIEntRect;
InflateRect(R,-10,-10); {把矩形縮小一圈}
S := 'Delphi';
Canvas.Brush.Color := clWhite;
Canvas.FillRect(R);
Canvas.TextRect(R, S , [tfSingleLine, tfCenter, tfVerticalCenter]);
end;