开发者

How does Copy Paste of formatted text work?

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 exact开发者_运维技巧ly 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.


Two things happen to make this work. First the source applications copies the data to the clipboard in multiple formats if possible. For example it might supply HTML, RTF, DOC, and plain text formats. Second the destination application is written to be able to paste from multiple formats if possible. For example it might first look for RTF and if that is not available it can paste plain text and if neither is available it can't paste at all.

It has been a while since I've used the clipboard functions but if I remember correctly it's possible for the source application to indicate what formats it can provide without actually doing the full conversion and data transfer to the clipboard. The actual conversion is only done when some other applications actually requests the data from the clipboard in a specific format.


There are some clipboard viewers(clipbrd.exe in win xp for example) that you can use to see what data is stored in clipboard, this behavior is because source application or destination(which exactly you will find by clipboard viewer) is supported that type of data, and converted in appropriated way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜