Search code examples
inno-setuppascalscript

Inno Setup use a different icon on a custom form title bar


By default, custom forms use the same icon in the title bar as that of the main WizardForm, which is the SetupIconFile. Is there any way to give a custom form a different icon on it's title bar?

[Code]
var
  CustomWindowForm: TForm;

{ Create and show the Custom window }
procedure ShowCustomWindow();
begin
  CustomWindowForm := TForm.Create(WizardForm);
  with CustomWindowForm do
    begin
      BorderStyle := bsSingle;
      Position := poOwnerFormCenter;
      Caption := 'Window Title';
      ClientWidth := ScaleX(400);
      ClientHeight := ScaleY(400);
      Show;
    end;
end;

What I need is something like an Icon property for TForm, but there doesn't appear to be one and I cannot find any information on this anywhere.


Solution

  • You have to use WinAPI, particularly LoadImage function and WM_SETICON message:

    [Files]
    Source: "custom.ico"; Flags: dontcopy
    
    [Code]
    
    const
      IMAGE_ICON = 1;
      LR_LOADFROMFILE = $10;
      WM_SETICON = $80;
      ICON_SMALL = 0;
    
    function LoadImage(
      hInst: Integer; ImageName: string; ImageType: UINT; X, Y: Integer;
      Flags: UINT): THandle; external 'LoadImageW@User32.dll stdcall';
    
    procedure CustomFormShow(Sender: TObject);
    var
      Icon: THandle;
    begin
      ExtractTemporaryFile('custom.ico');
      Icon := LoadImage(
        0, ExpandConstant('{tmp}\custom.ico'), IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
      SendMessage(TForm(Sender).Handle, WM_SETICON, ICON_SMALL, Icon);
    end;
    
    var
      CustomWindowForm: TForm;
    
    { Create and show the custom window }
    procedure ShowCustomWindow();
    begin
      CustomWindowForm := TForm.Create(WizardForm);
      with CustomWindowForm do
      begin
        { your code }
    
        OnShow := @CustomFormShow;
        Show;
      end;
    end;
    

    (The code is for Unicode version of Inno Setup – The only version as of Inno Setup 6)

    enter image description here