简体   繁体   English

使用OLE剪贴板和经典剪贴板出现意外/奇怪的结果,还是我错过了什么?

[英]Unexpected/weird results using OLE clipboard and classic clipboard, or do I miss something?

I'm trying to do the following trick: 我正在尝试执行以下技巧:

  1. I have IDataObject* to be set into the clipboard, so I'm using OleSetClipboard() to set it into the clipboard. 我将IDataObject*设置为剪贴板,因此我使用OleSetClipboard()将其设置为剪贴板。
  2. I have another CLIPFORMAT I want to add to the clipboard, but I can't do it with OleSetClipboard() because the IDataObject* I receive does not implement SetData() method. 我还有另一个想要添加到剪贴板的CLIPFORMAT ,但是我不能用OleSetClipboard()因为我收到的IDataObject*没有实现SetData()方法。 So, to overcome this limitation I OpenClipboard() with GetClipboardOwner() , this way, I can SetClipboardData() to the clipboard without EmptyClipboard() first. 因此,为了克服这个限制我OpenClipboard()GetClipboardOwner()我就可以了,这样SetClipboardData()到剪贴板不EmptyClipboard()第一。

Now, it all works well, but what happens is that OleGetClipboard() does not return the data I placed in the clipboard using SetClipboardData() , but I can using GetClipboardData() . 现在,一切正常,但是发生的是OleGetClipboard()不会返回我使用SetClipboardData()放置在剪贴板中的数据,但是我可以使用GetClipboardData()

I can imagine why this happens (It just returns the IDataObject* ), so I tried to OleFlushClipboard() to delete the IDataObject* , and OleGetClipboard() again to let the OS rebuild a new IDataObject* , and it still didn't contain the CLIPFORMAT added by SetClipboardData() . 我可以想象为什么会发生这种情况(它只返回IDataObject* ),所以我尝试使用OleFlushClipboard()删除IDataObject*OleGetClipboard()再次让操作系统重建新的IDataObject*但它仍然不包含SetClipboardData()添加的CLIPFORMAT

Does anyone have any idea how to overcome this issue? 有谁知道如何克服这个问题? or a different trick? 或其他技巧? or even explain why it works this way? 甚至解释为什么它会这样工作? Thanks 谢谢

I just tried this (on Windows 7) and it appears to work but only cross-process: 我刚刚尝试过(在Windows 7上),它似乎可以工作,但只能跨进程使用:

  • In a different process to the clipboard owner, OleGetClipboard returns a data object that contains all of the formats (ie the original formats from the data object and the extra ones added to the clipboard). 在与剪贴板所有者不同的过程中,OleGetClipboard返回一个包含所有格式的数据对象(即,数据对象的原始格式以及添加到剪贴板的其他格式)。
  • In the same process, OleGetClipboard always returns a data object that does not contain the extra clipboard formats. 在同一过程中,OleGetClipboard始终返回不包含额外剪贴板格式的数据对象。

In both cases, calling OleFlushClipboard makes no difference. 在这两种情况下,调用OleFlushClipboard都没有区别。

Anyway, this doesn't seem like a terribly robust solution. 无论如何,这似乎不是一个非常强大的解决方案。 What you can do instead is create your own data object that responds to the formats it knows about and delegates other formats to the original data object. 相反,您可以做的是创建自己的数据对象,以响应它所知道的格式,并将其他格式委托给原始数据对象。 The EnumFormatEtc method would combine formats from both objects, and so on. EnumFormatEtc方法将合并两个对象的格式,依此类推。 This article has the skeleton of a simple data object you could extend. 本文具有您可以扩展的简单数据对象的框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM