格式化文本的复制粘贴如何工作? [英] How does Copy Paste of formatted text work?

查看:83
本文介绍了格式化文本的复制粘贴如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对实现复制和粘贴功能的方法感到困惑。这正是我感到困惑的地方:



当我从MS Word复制格式文本(使用不同于HTML的标记语言)并粘贴到RTF编辑器中时网络浏览器,例如gmail或 http://www.freerichtexteditor.com/index.php ?inc = demo / index 保留了格式,但是现在标记已转换为HTML。这怎么发生的?是什么负责转换?



如果我将此文本粘贴到其他应用程序中,它将被转换为该格式。如果我复制了一些html页面并将其粘贴到word中,那么将从HTML到word的标记转换。再次,怎么做?



然后,如果我将复制的格式化文本粘贴到记事本之类的文本编辑器中,则所有格式都将丢失并且标记被剥离。哪个应用程序剥离了标记并将其转换为纯文本?



当我复制格式化文本时,究竟将什么复制到剪贴板中?我是.NET C#程序员。我该如何编程?

解决方案

剪贴板上的数据通过FORMATETC记录扩展:


http://msdn.microsoft.com/ zh-cn / library / ms682177%28VS.85%29.aspx


FORMATETC记录的第一个字段包含 cfFormat 描述文件格式的成员。 cfFormat 可以是预定义的值,例如 CF_UNICODETEXT CF_BITMAP 或应用程序定义的类型,例如由Microsoft Word。


在.NET中,您显然可以查询剪贴板对象,以找出其包含的数据格式:


http:// msdn。 microsoft.com/en-us/library/system.windows.forms.clipboard.aspx


您要查找的方法是 Clipboard.SetData


如果您不知道目标应用程序的格式,则可以使用此方法以多种格式存储数据。 / p>

使用此方法存储的数据在检索时可以转换为兼容格式。


要从剪贴板中以特定格式检索数据,请先在使用GetData方法检索剪贴板之前,请使用ContainsData方法确定剪贴板是否包含该格式的数据


关于您在Word中如何工作的具体问题,以上链接应为您提供足够的信息编写自己的剪贴板查看器的说明。由于Microsoft Word能够输出HTML文件,因此我猜测Word将数据以简单的Text,HTML,RTF以及Word格式写入剪贴板。


I'm confused about what implements the functionality of copy and paste. This is exactly what I'm confused with:

When I copy formatted text from MS Word (which uses a different markup language than HTML) and paste into an RTF editor in a web browser like gmail or http://www.freerichtexteditor.com/index.php?inc=demo/index the formatting is preserved but now the markup is converted into HTML. How did this happen? What took care of the conversion?

And if I had pasted this text into some other application, it will be converted into that format. If I copied some html page and pasted it in word then there will a markup conversion from HTML to word. Again, how?

Then if I paste this copied formatted text into a text editor like Notepad then all the formatting is lost and markup is stripped off. Which application stripped the markup and converted it to plain text?

When I copy formatted text, what exactly is copied into the clipboard? I 'm a .NET C# programmer. How would I program this?

解决方案

The data on the clipboard is extended with FORMATETC records:

http://msdn.microsoft.com/en-us/library/ms682177%28VS.85%29.aspx

The FORMATETC record contains as first field a cfFormat member which describes the file format. cfFormat can be a predefined value like CF_UNICODETEXT or CF_BITMAP or an application defined type defined by e.g. Microsoft Word.

In .NET you can apparently query the Clipboard object to find out which data formats it contains:

http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.aspx

The method you are looking for is Clipboard.SetData:

If you do not know the format of the target application, you can store data in multiple formats using this method.

Data stored using this method can be converted to a compatible format when it is retrieved.

To retrieve data from the Clipboard in a particular format, first use the ContainsData method to determine whether the Clipboard contains data in that format before retrieving it with the GetData method

As to your concrete question how it works in Word, the above links should give you enough information to write a little clipboard viewer yourself. Since Microsoft Word is able to output HTML files, my guess is that Word writes the data on the clipboard as simple Text, HTML, RTF and in Word format.

这篇关于格式化文本的复制粘贴如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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