views:

569

answers:

4

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.

+9  A: 

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
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.
Rowan
+4  A: 

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
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
I just checked - Delphi 7 most definitely has OnMouseEnter and OnMouseLeave :}
moobaa
@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.
Ken White
+1  A: 

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:

http://stackoverflow.com/questions/1108075/how-to-bring-front-or-launch-browser-in-delphi

Ian Kemp
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
A: 

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.

Ulrich Gerhardt