如何使 TLinkLabel 在 Delphi 中工作?
我在表单上放置了一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以调用 ShellExecute。
我为通用调用编写了这个方法,并且应该适用于您的情况。
在你的代码中你应该调用它
You can call ShellExecute.
I wrote this method for generic calls, and should works in your case.
In your code you should call this
我在使用 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.
TLinkLabel
提供了一个看起来像链接的标签。 作为程序员,你的工作就是让它表现得像一个链接,因为只有你才能知道什么链接在你的程序中应该表现得像一个链接。 您希望标签使用标签中的 URL 自动打开用户的默认 Web 浏览器,但这并不是链接的唯一作用。 例如:某些程序尝试提供在新窗口中打开链接与重新使用旧窗口之间的选择。 如果不知道正在使用哪个浏览器,您就无法实现该功能。 您的程序可能会为用户提供忽略默认浏览器设置并始终使用特定浏览器设置的选择。 为此,您的 UI 控件不能对程序应该执行的操作做出太多假设。
我猜您指的是 Delphi 附带的
TLinkLabel
控件。 (我的版本没有这样的组件。)我想 Delphi 控件旨在模仿 .Net 类库中的一个。 它可以容纳多个链接,每个链接可以做不同的事情。如果您想要一个始终对 URL 执行 shell 默认操作的控件,请考虑使用不同的
TLinkLabel
; Alexander 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: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 callsShellExecute
, as Cesar's answer demonstrates.哈哈,这很有趣。 我们不是将 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 theCaption
and setupOnLinkClick
to thatShellExecute
...我使用名为 TInternetLabel< 的控件/a> 相反。 它完全符合您的要求:单击时会打开浏览器,因此您不必在 OnClick 事件中添加代码。
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.
我尝试了这个解决方案,但在 Delphi XE4 中仍然出现问题,可能是因为 ShellOpen 不理解标题中的 HTML 代码。
对我有用的是 Cesar Romero(基本代码)、Adam Feistner(标题中的 HTML 代码)和旧解决方案的组合:
到
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:
to
ShellOpen(LinkLabel.Hint);
This worked for me.
在 .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