//第一种重载的声明: 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( var Rect: TRect; {矩形范围} var Text: string; {文本} TextFormat: TTextFormat = [] {选项; 集合类型, 默认值是个空的集合} ); {参数三是 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; //效果图: