如何使 TLinkLabel 在 Delphi 中工作?

发布于 2024-07-13 18:02:09 字数 232 浏览 6 评论 0原文

我在表单上放置了一个 TLinkLabel,用包含有效 HTML 链接的标题填充它,并得到了一些漂亮的蓝色下划线文本。 当我运行该程序时,我希望它能够调用 Firefox(我的默认浏览器)并自动打开链接。 显然事实并非如此。

帮助文件说我必须在 OnLinkClick 事件处理程序中对此进行编码。 但它没有说明如何做到这一点。 它将传递一个名为“Link”的字符串值。 我怎么说“调用默认浏览器并打开链接”?

I put a TLinkLabel on my form, filled it in with a caption including a valid HTML link, and got some nice blue underlined text. When I ran the program, I expected it to invoke Firefox (my default browser) and open the link automatically. Apparently that's not the case.

The helpfile says I have to code this in an OnLinkClick event handler. It doesn't say anything about how to do that, though. It'll pass in a string value called "Link". How do I say "invoke the default browser and have it open Link"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

我只土不豪 2024-07-20 18:02:10

您可以调用 ShellExecute。
我为通用调用编写了这个方法,并且应该适用于您的情况。

procedure ShellOpen(const Url: string; const Params: string = '');
begin
  ShellAPI.ShellExecute(0, 'Open', PChar(Url), PChar(Params), nil, SW_SHOWNORMAL);
end;

在你的代码中你应该调用它

procedure TForm1.LinkLabelClick(Sender: TObject);
begin
  ShellOpen(LinkLabel.Caption);
end;

You can call ShellExecute.
I wrote this method for generic calls, and should works in your case.

procedure ShellOpen(const Url: string; const Params: string = '');
begin
  ShellAPI.ShellExecute(0, 'Open', PChar(Url), PChar(Params), nil, SW_SHOWNORMAL);
end;

In your code you should call this

procedure TForm1.LinkLabelClick(Sender: TObject);
begin
  ShellOpen(LinkLabel.Caption);
end;
遮云壑 2024-07-20 18:02:10

我在使用 delphi 2010 附带的 TLinkLabel 时遇到了各种各样的问题。
a) 该控件不会呈现为超链接,而是呈现为表单上的简单标签文本。 b) 即使我设置了 Cursor 属性,光标也不会改变以指出这是一个链接。 c) OnLinkClick 事件根本不会触发。
我正在使用 Windows 7。

因此,就我而言,TLinkLabel 没有做任何应该做的事情,而且毫无用处。 ShellExecute 是唯一的解决方案,必须放置在 OnClick 事件中。

I have all sorts of problems with TLinkLabel that ships with delphi 2010.
a) The control does not render as a hyperlink but as a simple label text on the form. b) the cursor does not change to point out this is a link even though I set the Cursor property. c) the OnLinkClick event does not fire at all.
I am working on windows 7.

So, as far as I am concerned, TLinkLabel does nothing as it should and is useless. ShellExecute is the only solution and must be placed in the OnClick event.

可爱暴击 2024-07-20 18:02:10

TLinkLabel 提供了一个看起来像链接的标签。 作为程序员,你的工作就是让它表现得像一个链接,因为只有你才能知道什么链接在你的程序中应该表现得像一个链接。 您希望标签使用标签中的 URL 自动打开用户的默认 Web 浏览器,但这并不是链接的唯一作用。 例如:

  • Internet Explorer 不是我的默认浏览器,但是当我单击 Internet Explorer 中的链接时,我不希望链接的页面在 Firefox 中打开。
  • 当我单击帮助程序中的链接时,我希望链接的主题出现在帮助程序中,而不是出现在任何 Web 浏览器中。
  • Eclipse 中的首选项页面非常复杂。 一个页面上的设置有时与另一页面上的设置相关。 这些页面上有链接,可将用户直接带到相关页面。 本例中不涉及 URL 和 HTML,但它们仍然是带有下划线文本的标签。

某些程序尝试提供在新窗口中打开链接与重新使用旧窗口之间的选择。 如果不知道正在使用哪个浏览器,您就无法实现该功能。 您的程序可能会为用户提供忽略默认浏览器设置并始终使用特定浏览器设置的选择。 为此,您的 UI 控件不能对程序应该执行的操作做出太多假设。

我猜您指的是 Delphi 附带的 TLinkLabel 控件。 (我的版本没有这样的组件。)我想 Delphi 控件旨在模仿 .Net 类库中的一个。 它可以容纳多个链接,每个链接可以做不同的事情。

如果您想要一个始终对 URL 执行 shell 默认操作的控件,请考虑使用不同的 TLinkLabelAlexander Bach 的作品 完全符合您的预期。 它来自 Delphi 3,但未经修改也可以在所有更高版本中运行,包括 Delphi 2009。如果您查看代码,您将了解它是如何工作的。 它只是调用 ShellExecute,如 < a href="https://stackoverflow.com/questions/542267/542472#542472">Cesar 的回答演示了。

TLinkLabel provides a label that looks like a link. It's your job as the programmer to make it act like a link because only you can know what links are supposed to act like in your program. You wanted the label to automatically open the user's default Web browser using the URL in the label, but that's not the only thing links do. For example:

  • Internet Explorer is not my default browser, but when I click a link in Internet Explorer, I do not expect the linked page to open in Firefox.
  • When I click a link in the help program, I expect the linked topic to appear in the help program, not in any Web browser at all.
  • The preference pages in Eclipse are very complicated. Settings on one page are sometimes related to settings on another page. There are links on those pages that take the user directly to the related page. There is no URL and no HTML involved in this case, and yet they're still labels with underlined text.

Some programs try to offer a choice between opening links in new windows versus re-using old windows. You can't implement that feature without knowing which browser is in use. Your program might offer the user a choice to ignore the default browser setting and always use a specific one. To do that, your UI control can't make too many assumptions about what the program is supposed to do.

I'm guessing you're referring to a TLinkLabel control that comes with Delphi. (My versions don't have such a component.) I imagine that the Delphi control is meant to mimic the one in the .Net class library. It can hold multiple links, and each link can do something different.

If you want a control that always does the shell's default action for URLs, then consider using a different TLinkLabel; the one by Alexander Bach does exactly what you expected. It's from Delphi 3, but it should work unmodified in all later versions as well, including Delphi 2009. If you look at the code, you'll see how it works. It simply calls ShellExecute, as Cesar's answer demonstrates.

纵情客 2024-07-20 18:02:10

哈哈,这很有趣。 我们不是将 crHandPoint 设置为光标、彩色和下划线字体并将 OnClick 事件填充到标准 TLabel,而是使用知道链接标记的组件,并且我需要提供相同的 On(Link)Click 事件:))

因此, 好处是,它可以更轻松地将链接嵌入到某些文本中,并且它使用链接的系统样式...

ps:实际上,您必须将 Some text with link< ;/a> 进入 Caption 并将 OnLinkClick 设置为 ShellExecute...

LOL, it's funny. So instead of setting crHandPoint as cursor, colored and underlined font and filling the OnClick event to standard TLabel we have component that knows link tag and which at all I need to supply with same On(Link)Click event :))

Only thing it is good for is that it makes easier to embed link into some text and that it is using system style of link...

p.s.: really you have to put Some text with <a href="some URL">link</a> into the Caption and setup OnLinkClick to that ShellExecute...

一笑百媚生 2024-07-20 18:02:10

I use a control called TInternetLabel instead. It does exactly what you want: on click it opens the browser so you don't have to put code in the OnClick event.

星星的軌跡 2024-07-20 18:02:10

我尝试了这个解决方案,但在 Delphi XE4 中仍然出现问题,可能是因为 ShellOpen 不理解标题中的 HTML 代码。
对我有用的是 Cesar Romero(基本代码)、Adam Feistner(标题中的 HTML 代码)和旧解决方案的组合:


  • 将 URL 放在 HINT 字段中。
  • 更改行:ShellOpen(LinkLabel.Caption);

    ShellOpen(LinkLabel.Hint);

这对我有用。

I tried this solution but it still gave problems in Delphi XE4, probably becasue ShellOpen does not understand the HTML-code in the Caption.
What worked for me was a combination of Cesar Romero (the basic code), Adam Feistner (The HTML-code in the Caption) and an older solution:


  • Put the URL in the HINT field.
  • Change the line: ShellOpen(LinkLabel.Caption);
    to
    ShellOpen(LinkLabel.Hint);

This worked for me.

暮倦 2024-07-20 18:02:10

在 .Caption 链接中,您需要将自己格式化为 html < a href = ...

YT:Delphi 027 LinkLabel / 01 链接

In .Caption link you need to format yourself in html < a href = ...

YT: Delphi 027 LinkLabel / 01 Link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文