2016-09-12 124 views
1

是否有我可以在Pascal腳本中獲取字符串的寬度和高度?在Inno Setup中獲取字符串的寬度和高度Pascal腳本

例如:

var 
    S: String; 

S := 'ThisIsMyStringToBeChecked' 

在這裏,我需要根據其當前字體大小和字體返回其高度和寬度。

我讀How to get TextWidth of string (without Canvas)?,但無法將其轉換爲Inno Setup Pascal代碼。

我想要這個測量(寬度和高度),以當它的標題的字符串的寬度超過TLabel.Width改變TLabel.Caption'Too Long To Display'clRed

在此先感謝。

回答

1

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; 
+0

輝煌.........效果很好。但我想知道爲什麼這種方法不適用於TLabel? – GTAVLover

+0

因爲'TLabel'不是'TWinControl',所以它沒有'.Handle'。 「TLabel」的代碼需要有所不同。也許,可以使用'WizardForm.Handle',或者'nil'可以傳遞給'GetDC'。 –

+0

再次感謝您....... ;-) – GTAVLover

相關問題