使用 C# 创建带有文本/标题的超链接并将其复制到剪贴板

发布于 2024-10-10 03:25:05 字数 314 浏览 0 评论 0原文

在各种程序中,您可以将超链接复制到剪贴板并将其粘贴到其他应用程序中。例如,可以将本页底部的“随时欢迎反馈”链接复制并粘贴到 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 技术交流群。

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

发布评论

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

评论(3

千秋岁 2024-10-17 03:25:05

我不知道您正在使用的架构,但无论如何您只需将 URL 复制到剪贴板中即可。

例如,假设您有一个名为 myHyperlinkHyperLink 控件和一个名为 copyButtonButton

当用户单击按钮时,您只需使用 Clipboard.SetText(string)myHyperlink 的 URL 属性传递给该方法。

编辑:要在 Word 等其他程序中显示带标题的超链接,您必须使用特定标题以 HTML 方式设置文本。

Version:0.9
StartHTML:<<<<<<<1
EndHTML:<<<<<<<2
StartFragment:<<<<<<<3
EndFragment:<<<<<<<4
SourceURL: www.google.it
<html>
<body>
<!--StartFragment-->
<a href="http://programmers.stackexchange.com/">programmers</a></span></span>
<!--EndFragment-->
</body>
</html>

这是一个 HTML 示例,让我们尝试在 C# 中推广它:

private const string html = @"Version:0.9
StartHTML:<<<<<<<1
EndHTML:<<<<<<<2
StartFragment:<<<<<<<3
EndFragment:<<<<<<<4
SourceURL: {0}
<html>
<body>
<!--StartFragment-->
<a href='{0}'>{1}</a>
<!--EndFragment-->
</body>
</html>";

然后按如下方式使用它:

string link = String.Format(html, "http://www.google.it", "Google");
Clipboard.SetText(link, TextDataFormat.Html);

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 a Button 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.

Version:0.9
StartHTML:<<<<<<<1
EndHTML:<<<<<<<2
StartFragment:<<<<<<<3
EndFragment:<<<<<<<4
SourceURL: www.google.it
<html>
<body>
<!--StartFragment-->
<a href="http://programmers.stackexchange.com/">programmers</a></span></span>
<!--EndFragment-->
</body>
</html>

This is an example of HTML, let's try to generalize it in C#:

private const string html = @"Version:0.9
StartHTML:<<<<<<<1
EndHTML:<<<<<<<2
StartFragment:<<<<<<<3
EndFragment:<<<<<<<4
SourceURL: {0}
<html>
<body>
<!--StartFragment-->
<a href='{0}'>{1}</a>
<!--EndFragment-->
</body>
</html>";

And then use it as follows:

string link = String.Format(html, "http://www.google.it", "Google");
Clipboard.SetText(link, TextDataFormat.Html);
难如初 2024-10-17 03:25:05

如果有人和我有同样的问题:
建议的“在另一个程序(如 Word)中添加带标题的超链接”的解决方案并不完全像复制超链接一样工作,因为如果您进入一个不支持超链接的程序,则您什么也不会过去。如果您手动< /a> 复制超链接,它会超过标题。

我通过将标题字符串添加到剪贴板来实现这一点,@as-cii 的解决方案
(它看起来并不完美,所以如果您知道更好的解决方案,请告诉我)

private const string html = @"Version:0.9
StartHTML:<<<<<<<1
EndHTML:<<<<<<<2
StartFragment:<<<<<<<3
EndFragment:<<<<<<<4
SourceURL: {0}
<html>
<body>
<!--StartFragment-->
<a href='{0}'>{1}</a>
<!--EndFragment-->
</body>
</html>";
string link = String.Format(html, "http://www.google.com", "Google");
DataObject dataObject = new DataObject();
dataObject.SetText(link, TextDataFormat.Html);
dataObject.SetText("Google");
Clipboard.SetDataObject(dataObject, true);

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)

private const string html = @"Version:0.9
StartHTML:<<<<<<<1
EndHTML:<<<<<<<2
StartFragment:<<<<<<<3
EndFragment:<<<<<<<4
SourceURL: {0}
<html>
<body>
<!--StartFragment-->
<a href='{0}'>{1}</a>
<!--EndFragment-->
</body>
</html>";
string link = String.Format(html, "http://www.google.com", "Google");
DataObject dataObject = new DataObject();
dataObject.SetText(link, TextDataFormat.Html);
dataObject.SetText("Google");
Clipboard.SetDataObject(dataObject, true);
萌辣 2024-10-17 03:25:05

https://theartofdev.com/2014/06/12 /setting-htmltext-to-clipboard-revisited/ 最适合我。

处理@NellyFlo 描述的问题,并在 Skype for Business 中粘贴。

简而言之:

  • 包含 ClipboardHelper.cs
  • 使用 设置剪贴板
    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:

  • include ClipboardHelper.cs
  • set the clipboard with
    var text = "Google";
    var link = "http://www.google.com";
    ClipboardHelper.CopyToClipboard("\<a href=\"{link}\">{text}</a>", text);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文