.NET - 一种将我自己的剪贴板格式添加到现有格式的方法
我有一个 Excel 插件,可以在工作表上显示一些结构。用户可以复制结构并将其粘贴到另一个工作表或由剪贴板格式处理的另一个应用程序中。当用户复制结构时,我将结构转换为特定格式并使用 DataObject::SetData() 将其放在剪贴板上。请注意,当在 Excel 中启动复制时,它会将多种格式放入剪贴板(参见图片)。
问题是有一个第三方应用程序依赖于剪贴板上的数据(从 Excel 复制并粘贴到这个第三方应用程序中),但有趣的是我不确定它依赖于哪种格式。我需要保留 Excel 已设置的现有格式,并向其中添加我自己的格式。
目前,当我在 .NET 中使用 Clipboard 类(获取 DataObject 并在其中调用 SetData)时,所有其他格式都会被新格式替换。 然后,我尝试创建一个新的 DataObject,将现有格式数据复制到该数据对象,然后在剪贴板中设置该数据对象。 这工作正常,但复制数据需要时间。
// Copying existing data in clipboard to our new DataObject
IDataObject existingDataObject = Clipboard.GetDataObject();
DataObject dataObject = new DataObject();
string[] existingFormats = existingDataObject.GetFormats();
foreach (string existingFormat in existingFormats)
dataObject.SetData(existingFormat, existingDataObject.GetData(existingFormat));
我正在寻找一种解决方案,只需访问现有的 DataObject 并悄悄地将我自己的数据添加到其中,而不影响其他格式。
Excel 剪贴板格式 -(忽略本机格式)
I have a Excel addin that displays some structures on the worksheet. Users can copy the structures and paste them in another worksheet or another application which is handled by the Clipboard formats. When a user copies the structure, I convert the structure into a specific format and put it on the clipboard using the DataObject::SetData(). Please note that when a copy is initiated in Excel, it puts a number of formats on the clipboard (see image).
The problem is that there is a third party application that depends on the data on the clipboard(Copy from Excel and paste into this 3rd party app) but the funny thing is that I am not sure which format it depends on. I need to preserve the existing formats that Excel has put up there and also add my own format to it.
Currently when I use the Clipboard class in .NET (taking the DataObject and calling SetData inside it), all the other formats are replaced by new ones.
I then tried to create a new DataObject, copy the existing format data to this data object and then set this data object in the Clipboard. This works fine but it takes time to copy the data.
// Copying existing data in clipboard to our new DataObject
IDataObject existingDataObject = Clipboard.GetDataObject();
DataObject dataObject = new DataObject();
string[] existingFormats = existingDataObject.GetFormats();
foreach (string existingFormat in existingFormats)
dataObject.SetData(existingFormat, existingDataObject.GetData(existingFormat));
I am looking for a solution to just access the existing DataObject and quietly add my own data to it without affecting other formats.
Excel Clipboard Formats - (Ignore the Native Format)
Clipboard Formats http://www.freeimagehosting.net/uploads/258a7fcdd8.jpg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以为从 Windows 剪贴板获取的 IDataObject 创建一个包装类来添加数据。这个想法是,包装器将了解您的自定义格式,并将所有其他格式委托给原始的、包装的 IDataObject。
下面是显示构造函数和 IDataObject 方法实现之一的部分实现:
一个警告:检索系统剪贴板 IDataObject 时,您必须检查它是否是您的包装对象之一。在这种情况下,您不想继续包装包装纸。相反,您需要修改现有包装器的数据/格式字段,或者为原始剪贴板数据对象创建新的包装器。
You could create a wrapper class for the IDataObject that you get from the Windows clipboard to add your data. The idea is that the wrapper would know about your custom formats and delegate to the original, wrapped IDataObject for all other formats.
Here's a partial implementation showing the constructor and one of the IDataObject method implementations:
One caveat: When retrieving the system clipboard IDataObject, you'll have to check if it's one of your wrapper objects. In that case, you don't want to keep wrapping your wrappers. Instead you'd want to either modify the data/format fields of the existing wrapper, or create a new wrapper for the original clipboard data object.