Lucas Matos
Lucas Matos

Reputation: 1162

How to add clickable links to custom Inno Setup WelcomeLabel?

I have an Inno Setup program with a custom WelcomeLabel2 message.

[Messages]
WelcomeLabel2=Lorem ipsum dolor sit amet CLICK_HERE consectetur adipiscing elit.

I am trying to make the CLICK_HERE a clickable link to a website.

Another thing I am wondering is how to make this CLICK_HERE text bold.

How can I achieve this?

Upvotes: 6

Views: 3724

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202158

Since Inno Setup 6.3, you can use TNewLinkLabel, which supports HTML-like <a> tag.

The official CodeClasses.iss gives this example:

LinkLabel := TNewLinkLabel.Create(Page);
LinkLabel.Caption := 'TNew<a id="jrsoftware">Link</a>Label with more text and an adjusted label height so it''s multi-line with a second <a id="jrsoftware">link</a> on the second line.';
LinkLabel.OnLinkClick := @LinkLabelOnLinkClick;
... 
procedure LinkLabelOnLinkClick(Sender: TObject; const Link: string; LinkType: TSysLinkType);
var
  ErrorCode: Integer;
begin
  if (LinkType = sltID) and (Link = 'jrsoftware') then
    ShellExecAsOriginalUser('open', 'https://jrsoftware.org', '', '', SW_SHOWNORMAL, ewNoWait, ErrorCode)
  else if LinkType = sltURL then  
    ShellExecAsOriginalUser('open', Link, '', '', SW_SHOWNORMAL, ewNoWait, ErrorCode);
end;

With older versions of Inno Setup, it's not easy.

To create a label that is clickable whole, you can use a code like:

procedure OpenBrowser(Url: string);
var
  ErrorCode: Integer;
begin
  ShellExec('open', Url, '', '', SW_SHOWNORMAL, ewNoWait, ErrorCode);
end;

procedure LinkClick(Sender: TObject);
begin
  OpenBrowser('https://www.example.com/');
end;

procedure InitializeWizard;
var
  Link: TLabel;
begin
  Link := TLabel.Create(WizardForm);
  Link.Left := ???;
  Link.Top := ???;
  Link.Parent := WizardForm.WelcomePage;
  Link.Caption := 'CLICK_HERE';
  Link.OnClick := @LinkClick;
  Link.ParentFont := True;
  Link.Font.Style := Link.Font.Style + [fsUnderline, fsBold];
  Link.Font.Color := clBlue;
  Link.Cursor := crHand;
end;

See also Show License Agreement link in Inno Setup while installation.


Though to create a label that have just parts of its text clickable is a way more difficult. If the text fits on one line, it's doable by stacking three labels next to each other (first the leading static text, then link, followed by the trailing static text). But if the text does not fit on one line, it's not doable, as the labels would overlap each other.


Alternatively, you can create an RTF document with the link and present it using a read-only TRichEditViewer:

procedure InitializeWizard;
var
  RichViewer: TRichEditViewer;
begin
  RichViewer := TRichEditViewer.Create(WizardForm);
  RichViewer.Left := WizardForm.WelcomeLabel2.Left;
  RichViewer.Top := WizardForm.WelcomeLabel2.Top;
  RichViewer.Width := WizardForm.WelcomeLabel2.Width;
  RichViewer.Height := WizardForm.WelcomeLabel2.Height;
  RichViewer.Parent := WizardForm.WelcomeLabel2.Parent;
  RichViewer.BorderStyle := bsNone;
  RichViewer.TabStop := False;
  RichViewer.ReadOnly := True;
  WizardForm.WelcomeLabel2.Visible := False;
   
  RichViewer.RTFText :=
    '{\rtf1 Lorem ipsum dolor sit amet ' +
    '{\b {\field{\*\fldinst{HYPERLINK "https://www.example.com/" }}' + 
    '{\fldrslt{CLICK_HERE}}}} ' +
    'consectetur adipiscing elit.}';
end;

enter image description here

You need Unicode version (the only version as of Inno Setup 6) for this, see How to add clickable links to custom page in Inno Setup using RichEditViewer?

To change the link color, see How to change the color of the hyperlink in RTF text in Inno Setup


As @Bill_Stewart commented, you should avoid starting the browser with elevated privileges. For a solution, see How to open a web site after uninstallation in non-elevated mode?

Upvotes: 17

Related Questions