无法将HTML格式往返于剪贴板 [英] Can not round trip html format to clipboard

查看:85
本文介绍了无法将HTML格式往返于剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写Html格式,但是我什至无法获得一个简单的MSDN示例来工作。

I want to write Html format, but I can not even get a simple MSDN example of it to work.

http://msdn.microsoft.com/en-us/library/tbfb3z56.aspx

这个控制台应用程序(剪贴板往返器)是否适用于任何人?

Does this console app, a clipboard round tripper, work for anyone?


using System;
using System.Windows; //Need to add a PresentationCore or System.Windows.Forms reference

class Program {
    [STAThread]
    static void Main( string[] args ) {
        Console.WriteLine( "Copy a small amount of text from a browser, then press enter." );
        Console.ReadLine();

        var text = Clipboard.GetText();
        Console.WriteLine();
        Console.WriteLine( "--->The clipboard as Text:" );
        Console.WriteLine( text );

        Console.WriteLine();
        Console.WriteLine( "--->Rewriting clipboard with the same CF_HTML data." );
        //***Here is the problem code***
        var html = Clipboard.GetText( TextDataFormat.Html );
        Clipboard.Clear();
        Clipboard.SetText( html, TextDataFormat.Html );

        var text2 = Clipboard.GetText();
        Console.WriteLine();
        Console.WriteLine( "--->The clipboard as Text:" );
        Console.WriteLine( text2 );

        var isSameText = ( text == text2 );
        Console.WriteLine();
        Console.WriteLine( isSameText ? "Success" : "Failure" );

        Console.WriteLine();
        Console.WriteLine( "Press enter to exit." );
        Console.ReadLine();
    }
}


推荐答案

从浏览器复制数据到剪贴板,它将相同的数据以多种格式(包括文本和HTML)放入剪贴板。因此,您可以以文本或HTML格式读回数据。但是,当您在此处调用SetText时,只能传递HTML格式,因此,当您使用常规的GetText时,剪贴板上没有文本版本,并且会返回null。

When you copy data from a browser onto the clipboard, it puts the same data onto the clipboard in multiple formats, including both text and HTML. So you can read the data back out in either text or HTML format. However, when you call SetText here, you are ONLY passing in HTML format, so when you use the regular GetText, there is no text version on the clipboard and you get null back.

您可以使用IDataObject一次将多种格式(即文本和HTML)放到剪贴板上,但是在将数据放到剪贴板上之前必须自己进行格式之间的转换。有一个如何使用IDataObject的示例此处

You can put multiple formats onto the clipboard at once (i.e., both text and HTML) using IDataObject, but you have to do the translation between formats yourself before you put the data on the clipboard. There's an example of how to use IDataObject here.

这篇关于无法将HTML格式往返于剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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