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

4

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.

Pitta answered 25/8, 2009 at 6:53 Comment(0)
G
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;
Gerald answered 25/8, 2009 at 6:58 Comment(2)
Yep, that's what I was looking for. I'll accept it in a day or two, to make sure no one else has any other ideas. Cheers.Pitta
Do not forget to add " uses ShellApi; "Worker
C
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.
Championship answered 25/8, 2009 at 6:59 Comment(3)
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 OnMouseMoveRandall
I just checked - Delphi 7 most definitely has OnMouseEnter and OnMouseLeave :}Championship
@Gerry: Yes, they are; you just have to work a little harder. <g> You just add handlers for the CM_MOUSEENTER and CM_MOUSELEAVE messages.Km
L
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

Leban answered 25/8, 2009 at 7:6 Comment(1)
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.Pitta
D
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.

Dunsany answered 25/8, 2009 at 7:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.