的TNewStaticText
(不是TLabel
)以下工作:
type
TSize = record
cx, cy: Integer;
end;
function GetTextExtentPoint32(hdc: THandle; s: string; c: Integer;
var Size: TSize): Boolean;
external '[email protected] stdcall';
function GetDC(hWnd: THandle): THandle;
external '[email protected] stdcall';
function SelectObject(hdc: THandle; hgdiobj: THandle): THandle;
external '[email protected] stdcall';
procedure SmartSetCaption(L: TNewStaticText; Caption: string);
var
hdc: THandle;
Size: TSize;
OldFont: THandle;
begin
hdc := GetDC(L.Handle);
OldFont := SelectObject(hdc, L.Font.Handle);
GetTextExtentPoint32(hdc, Caption, Length(Caption), Size);
SelectObject(hdc, OldFont);
if Size.cx > L.Width then
begin
L.Font.Color := clRed;
L.Caption := 'Too long to display';
end
else
begin
L.ParentFont := True;
L.Caption := Caption;
end;
end;
輝煌.........效果很好。但我想知道爲什麼這種方法不適用於TLabel? – GTAVLover
因爲'TLabel'不是'TWinControl',所以它沒有'.Handle'。 「TLabel」的代碼需要有所不同。也許,可以使用'WizardForm.Handle',或者'nil'可以傳遞給'GetDC'。 –
再次感謝您....... ;-) – GTAVLover