33 Votes

Delphi/Lazarus: How to set up a Link to a Website in an Application

Tutorial by Stefan Trost | Last update on 2023-01-29 | Created on 2012-08-12

Sometimes, it is a good idea to set up a link to a website in a Windows application, for example, to set a link to your homepage or to link some help pages of the program. In this tutorial, I will show you how to do that in Delphi or Lazarus.

In Delphi we are using a API function from the ShellApi for that and we want to style the label that should show the link that it will look like it is a link.

The Label with the Link

To make our TLabel looking like a link, we are using the following font and cursor properies, that can also be adjusted by using the Object Inspector:

Label1.Caption    := 'www.sttmedia.com';
Label1.Font.Color := clBlue;
Label1.Font.Style := [fsUnderline];
Label1.Cursor     := crHandPoint;

The caption of the label is the Internet adress, we want to call. This will be read out and called later via the code, so that we only have to modify the label's caption to change the address and we do not have to change anything additionally in the underlying code.

In order to emulate a link with the font, we set the font color to blue and the font style to underlined. In addition, we adapt the cursor so that the cursor becomes a hand as soon as we move the mouse over the label to signal its clickable.

The Delphi Code

If we use Delphi for the programming, we have to add the unit "ShellApi" to our "Uses" section. Then we can write the following code in the OnClick event of our Label:

uses 
   ShellApi, ...

procedure TForm1.Label1Click(Sender: TObject);
begin
  ShellExecute(Application.Handle, 'open', PChar(Label1.Caption), 
    nil, nil, SW_ShowNormal);
end;

If we click on our label, the standard browser will be opened with the website written in the caption of the label.

The Lazarus Code

If you use Lazarus instead of Delphi, it is even easier: In Lazarus you can simply use the function OpenURL() from the Unit LCLIntf to open a website platform independently from your Lazarus program on every operating system. So the code could look like this:

uses
   LCLIntf, ...

procedure TForm1.Label1Click(Sender: TObject);
begin
  OpenURL('https://' + Label1.Caption);
end;

OpenURL() can be passed the URL directly without further parameters and opens the website in the system's default browser. Since we wrote the internet address without "https://" in the caption of our label, we are adding here "https://" to the caption of the label before. Of course, this can be adjusted as desired.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

Delphi: System-Wide HotKey

Tutorial | 1 Comment

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.