How to copy both - HTML and text to the clipboard?

淺唱寂寞╮ 提交于 2020-01-13 09:15:14

问题


I'm trying to put in the clipboard piece of HTML and plain text at the same time, so that HTML-capable editors could paste HTML, and other editors could use plain text.

Clipboard.SetData(DataFormats.Html, htmlWithHeader);
Clipboard.SetData(DataFormats.UnicodeText, plainText);

But only the last format is actually put to the clipboard. In the sample above, clipboard would contain only plaintext (as shown by Clipboard.GetDataObject().GetFormats()). And if I swap the lines, the clipboard would have only the HTML format.

How can I put both formats into the clipboard at the same time?


回答1:


You can NOT use Clipboard.SetData for setting both HTML and plain text. The second call of SetData will clear the content of clipboard that has been set by first call and store the new data.

You should use DataObject and Clipboard.SetDataObject().

Example:

DataObject dataObj = new DataObject();
dataObj.SetData(DataFormats.Html, htmlWithHeader);
dataObj.SetData(DataFormats.Text, plainText);

Clipboard.SetDataObject(dataObj);


来源:https://stackoverflow.com/questions/16286957/how-to-copy-both-html-and-text-to-the-clipboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!