7

How do I make a TLabel behave like a hyperlink in Delphi?

Note: I'm not interested in using TLinkLabel because of backwards compatibility issues.

Rowan
  • 2,384
  • 2
  • 21
  • 22

4 Answers4

26

Colour it blue, set style to underline and add an OnClick event!

procedure TForm1.Label1Click(Sender: TObject);
var
  MyLink: string;
begin
  MyLink := 'http://www.mysite.com/';
  ShellExecute(Application.Handle, PChar('open'), PChar(MyLink),    
   nil, nil, SW_SHOW);
end;
DmitryK
  • 5,542
  • 1
  • 22
  • 32
7

It depends on what you require of your hyperlinks. I'd just...

  • set the font color to blue
  • use the OnMouse[Enter|Leave|Move] events to appropriately apply the underline style to the font
  • use the OnClick event to spawn a browser & change the font color, as desired.
moobaa
  • 4,482
  • 1
  • 29
  • 31
  • OnMouseEnter and OnMouseLeave aren't available in D7 and earlier (not sure about D2005) so the comment about TLinkLabel also precludes this. You will have to use OnMouseMove – Gerry Coll Aug 25 '09 at 11:03
  • I just checked - Delphi 7 most definitely has OnMouseEnter and OnMouseLeave :} – moobaa Aug 26 '09 at 01:40
  • @Gerry: Yes, they are; you just have to work a little harder. You just add handlers for the CM_MOUSEENTER and CM_MOUSELEAVE messages. – Ken White Aug 26 '09 at 13:03
1

What version of Delphi are you using? Looking at my Delphi 4 IDE, TLabel has no OnMouseEnter/OnMouseLeave event, which would be necessary to change the cursor to a "Hand" when the user hovers over the "link".

It does have the OnClick event, which you can wire up to launch the user's web browser:

How to bring front or launch browser in Delphi

Community
  • 1
  • 1
Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
  • Delphi 2009. Perhaps mouse over was the wrong term. In the properties for the TLabel, in D2009, you are able to specify which cursor should be used for this label. – Rowan Aug 25 '09 at 07:09
0

One can tab to and give focus to links in a browser. Therefore I would consider using a windowed control (like an owner-drawn TButton) for this task.

Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83