创建和复制超链接文本/标题用C#到剪贴板 [英] Create and Copy hyperlink with text/caption to Clipboard with c#

查看:361
本文介绍了创建和复制超链接文本/标题用C#到剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在各种各样的程序,您可以复制链接到剪贴板并粘贴到其他应用程序。例如在反馈总是受欢迎的在本页面底部的链接可以被复制并粘贴到MS Word。我想创建编程这样的链接,将其复制到剪贴板,然后可以将其粘贴在其他地方。

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.

例如与文本的链接 的Stack 的<映射到/ STRONG> stackoverflow.com

For example a link with the text Stack that maps to stackoverflow.com.

我已经试过各种事情Clipboard.SetData但似乎没有这样的伎俩。

I’ve tried all sorts of things with Clipboard.SetData but nothing seems to do the trick.

(我工作在VS2010 Windows窗体应用程序,.NET4.0)

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

推荐答案

我不知道你正在使用的建筑,但在任何情况下,你刚才复制的URL中剪贴板中。

I don't know the architecture you're working with, but in any case you have just to copy the URL in the Clipboard.

例如,假设你有一个名为超链接控制的 myHyperlink 按钮命名的复制按钮的。

For example, assuming you've got an HyperLink control named myHyperlink and a Button named copyButton.

当用户点击你刚才使用 Clipboard.SetText(串)传递给方法的按钮网址属性的 myHyperlink

When the user clicks the button you have just to use Clipboard.SetText(string) passing to the method the URL Property of myHyperlink.

编辑:显示与标题的超链接,如Word另一个程序,你必须。设置在一个特定的头一个HTML的方式文本

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>

这是HTML的一个例子,让我们试着概括它在C#中:

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

这篇关于创建和复制超链接文本/标题用C#到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆