使用 C# 创建带有文本/标题的超链接并将其复制到剪贴板
在各种程序中,您可以将超链接复制到剪贴板并将其粘贴到其他应用程序中。例如,可以将本页底部的“随时欢迎反馈”链接复制并粘贴到 MS Word 中。我想以编程方式创建这样的链接,将其复制到剪贴板,然后能够将其粘贴到其他地方。
例如,带有映射到 stackoverflow.com 的文本 Stack 的链接。
我已经用 Clipboard.SetData 尝试了各种方法,但似乎没有任何效果。
(我正在VS2010,.NET4.0中开发Windows窗体应用程序)
In all sorts of programs you can copy hyperlinks to clipboard and paste them into other applications. E g the ’feedback always welcome’ link at the bottom of this page can be copied and pasted into MS Word. I want to create such a link programmatically, copy it to the Clipboard and then be able to paste it somewhere else.
For example a link with the text Stack that maps to stackoverflow.com.
I’ve tried all sorts of things with Clipboard.SetData but nothing seems to do the trick.
(I'm working on a Windows form application in VS2010, .NET4.0)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道您正在使用的架构,但无论如何您只需将 URL 复制到剪贴板中即可。
例如,假设您有一个名为 myHyperlink 的
HyperLink
控件和一个名为 copyButton 的Button
。当用户单击按钮时,您只需使用
Clipboard.SetText(string)
将 myHyperlink 的 URL 属性传递给该方法。编辑:要在 Word 等其他程序中显示带标题的超链接,您必须使用特定标题以 HTML 方式设置文本。
这是一个 HTML 示例,让我们尝试在 C# 中推广它:
然后按如下方式使用它:
I don't know the architecture you're working with, but in any case you have just to copy the URL in the Clipboard.
For example, assuming you've got an
HyperLink
control named myHyperlink and aButton
named copyButton.When the user clicks the button you have just to use
Clipboard.SetText(string)
passing to the method the URL Property of myHyperlink.EDIT: To show an hyperlink with caption in another program like Word you have to set the text in a HTML way with a particular header.
This is an example of HTML, let's try to generalize it in C#:
And then use it as follows:
如果有人和我有同样的问题:
建议的“在另一个程序(如 Word)中添加带标题的超链接”的解决方案并不完全像复制超链接一样工作,因为如果您进入一个不支持超链接的程序,则您什么也不会过去。如果您手动< /a> 复制超链接,它会超过标题。
我通过将标题字符串添加到剪贴板来实现这一点,@as-cii 的解决方案
(它看起来并不完美,所以如果您知道更好的解决方案,请告诉我)
In the case somebody has the same problem as me:
The suggested solution for "hyperlink with caption in another program like Word" does not work totally like copying a hyperlink, because if you past in a program, which does not support hyperlinks, you past nothing. If you would manualy copy a hyperlink, it would past the caption.
I achieved this by additionally adding the caption string to the clipboard to the solution by @as-cii
(it doesn't seem perfect so let me know, if you know a better solution)
https://theartofdev.com/2014/06/12 /setting-htmltext-to-clipboard-revisited/ 最适合我。
处理@NellyFlo 描述的问题,并在 Skype for Business 中粘贴。
简而言之:
设置剪贴板
var text =“谷歌”;
var link =“http://www.google.com”;
ClipboardHelper.CopyToClipboard("\{text}", text);
https://theartofdev.com/2014/06/12/setting-htmltext-to-clipboard-revisited/ worked best for me.
Handles the problem described by @NellyFlo, plus pastes in Skype for Business.
In short:
var text = "Google";
var link = "http://www.google.com";
ClipboardHelper.CopyToClipboard("\<a href=\"{link}\">{text}</a>", text);