【问题标题】:Get width and height of a string in Inno Setup Pascal Script在 Inno Setup Pascal 脚本中获取字符串的宽度和高度
【发布时间】:2017-01-19 10:58:34
【问题描述】:

我是否可以在 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

提前致谢。

【问题讨论】:

    标签: inno-setup pascalscript


    【解决方案1】:

    以下适用于TNewStaticText(不是TLabel):

    type
      TSize = record
        cx, cy: Integer;
      end;
    
    function GetTextExtentPoint32(hdc: THandle; s: string; c: Integer;
      var Size: TSize): Boolean;
      external 'GetTextExtentPoint32W@Gdi32.dll stdcall';
    function GetDC(hWnd: THandle): THandle;
      external 'GetDC@User32.dll stdcall';
    function SelectObject(hdc: THandle; hgdiobj: THandle): THandle;
      external 'SelectObject@Gdi32.dll 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
    • 因为TLabel不是TWinControl,所以它没有.HandleTLabel 的代码需要有所不同。也许,可以使用WizardForm.Handle 代替,或者nil 可以传递给GetDC
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-12
    • 2011-08-13
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    • 1970-01-01
    相关资源
    最近更新 更多