它源代码是这么画的: Rect := GetClientRect;//获取Panel的矩形区 with Canvas do begin if not ThemeServices.ThemesEnabled or not ParentBackground then begin Brush.Color := Color; FillRect(Rect); end; Brush.Style := bsClear; Font := Self.Font; FontHeight := TextHeight('W');//文字的高度 with Rect do//计算文字的矩形区 begin Top := ((Bottom + Top) - FontHeight) div 2; Bottom := Top + FontHeight; end; Flags := DT_EXPANDTABS or DT_VCENTER or Alignments[FAlignment];//使用了文字居中的标记 Flags := DrawTextBiDiModeFlags(Flags); DrawText(Handle, PChar(Caption), -1, Rect, Flags); end; 从上面源代码可以看出,文字的Rect的Top和Bottom是自己算的,而Left和Right则是用的Panel自身的。 Canvas既然有TextHeight可以测出文本的高度,也有TextWidth可以计算文本的宽度。所以你完全可以自己计算。
Rect := GetClientRect;//获取Panel的矩形区
with Canvas do
begin
if not ThemeServices.ThemesEnabled or not ParentBackground then
begin
Brush.Color := Color;
FillRect(Rect);
end;
Brush.Style := bsClear;
Font := Self.Font;
FontHeight := TextHeight('W');//文字的高度
with Rect do//计算文字的矩形区
begin
Top := ((Bottom + Top) - FontHeight) div 2;
Bottom := Top + FontHeight;
end;
Flags := DT_EXPANDTABS or DT_VCENTER or Alignments[FAlignment];//使用了文字居中的标记
Flags := DrawTextBiDiModeFlags(Flags);
DrawText(Handle, PChar(Caption), -1, Rect, Flags);
end;
从上面源代码可以看出,文字的Rect的Top和Bottom是自己算的,而Left和Right则是用的Panel自身的。
Canvas既然有TextHeight可以测出文本的高度,也有TextWidth可以计算文本的宽度。所以你完全可以自己计算。