Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

.net - 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 exactly is copied into the clipboard? I 'm a .NET C# programmer. How would I program this?

question from:https://stackoverflow.com/questions/1885956/how-does-copy-paste-of-formatted-text-work

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...