开发者

Create and Copy hyperlink with text/caption to Clipboard with c#

开发者 https://www.devze.com 2023-02-02 23:29 出处:网络
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

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.开发者_StackOverflow社区SetData but nothing seems to do the trick.

(I'm working on a Windows form application in VS2010, .NET4.0)


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);


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);


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消