如何设置HTML在C#中到剪贴板? [英] How to set HTML to clipboard in C#?

查看:606
本文介绍了如何设置HTML在C#中到剪贴板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望把丰富的文本在HTML中剪贴板上,所以当用户粘贴到Word中,将包括源HTML格式。

I want to put rich text in HTML on the clipboard so when the users paste to Word, it will include the source HTML formatting.

使用 Clipboard.SetText 方法不起作用。

Using the Clipboard.SetText method doesn't work.

另外,我想,如果用户粘贴到像Word富文本编辑器,将粘贴格式化的文本,如果它们粘贴到一个普通的编辑器如记事本,将粘贴纯文本。

Also, I would like that if users paste into a rich editor like Word it will paste formatted text, and if they paste into a plain editor like Notepad it will paste plain text.

推荐答案

在设定的HTML文本,您需要提供一个标题附加信息是什么片段的HTML,你实际上需要,同时能够围绕提供更多的造型贴它:

When setting HTML text, you need to provide a header with additional information to what fragment of the html you actually want to paste while being able to provide additional styling around it:

Version:0.9
StartHTML:000125
EndHTML:000260
StartFragment:000209
EndFragment:000222
<HTML>
<head>
<title>HTML clipboard</title>
</head>
<body>
<!–StartFragment–><b>Hello!</b><!–EndFragment–>
</body>
</html>

使用的标题(和正确的索引),称 Clipboard.SetText TextDataFormat.Html 将这样的伎俩

With the header (and correct indexes), calling Clipboard.SetText with TextDataFormat.Html will do the trick.

要处理HTML和纯文本膏,你不能使用 Clipboard.SetText 的方法,因为它每次被称为时间清除剪贴板;你需要创建一个数据对象实例,调用它的的SetData 方法一次HTML和曾经与纯文本,然后设置到剪贴板使用 Clipboard.SetDataObject 的对象。

To handle HTML and plain text pastes, you can’t use the Clipboard.SetText method, as it clears the clipboard each time it’s called; you need to create a DataObject instance, call its SetData method once with HTML and once with plain text, and then set the object to clipboard using Clipboard.SetDataObject.

更新

请参阅设置HTML /文本到剪贴板再访的详细信息和 ClipboardHelper 的实施。

See "Setting HTML/Text to Clipboard revisited" for more details and ClipboardHelper implementation.

这篇关于如何设置HTML在C#中到剪贴板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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