问题描述:
我的Panel为自动生成,能自动调整Width,Height,Font.Size Caption中显示的字符有多有少,有时Caption中字符过多,则不能完全一行显示出来,我不希望它换行再显示,如果不能完全一行显示出来,则调整当前Panel的字体大小,其它Panel的字体大小不变.直到完全显示出来.
我的Panel为自动生成,能自动调整Width,Height,Font.Size Caption中显示的字符有多有少,有时Caption中字符过多,则不能完全一行显示出来,我不希望它换行再显示,如果不能完全一行显示出来,则调整当前Panel的字体大小,其它Panel的字体大小不变.直到完全显示出来.
procedure AutoAdjustFont(Font: TFont; const Width: Integer;
const Caption: string);
var
FTextWidth: Integer;
FTemp: TForm;
begin
FTemp := TForm.Create(Application);
FTemp.Canvas.Font.Assign(Font);
try
FTextWidth := FTemp.Canvas.TextWidth(Caption);
while FTextWidth > Width do
begin
FTemp.Font.Size := FTemp.Font.Size - 1;
FTextWidth := FTemp.Canvas.TextWidth(Caption);
end;
Font.Assign(FTemp.Canvas.Font);
finally
FTemp.Free;
end;
end;
你應該用前一種字體才能完成你的要求,調整Font.Height 而不是調Size ;
試試這個:(Panel1 的字體就選無限縮放的,如:在繁體Windows是有一個"新明細體")
procedure TForm1.Button2Click(Sender: TObject);
var
str: string ;
aFont : TFont ;
begin
str :='WWWDASDFASDFASDFASEWRWQERWQER';
Panel1.Caption := str ;
aFont := Tfont.Create ;
aFont.Assign(canvas.Font);
canvas.Font.Assign(Panel1.Font) ;
canvas.Font.Height := 100 ;
while canvas.TextWidth(str)>Panel1.Width do
begin
canvas.Font.Height :=canvas.Font.Height-1 ;
if canvas.Font.Height<=0 then Break ;
end ;
Panel1.Font.Assign(canvas.Font) ;
canvas.Font.Assign(afont) ;
aFont.Free ;
end;