为什么我不能在一个程序的两个实例之间拖动一个点? [英] Why can't I drag a Point between two instances of a program?

查看:106
本文介绍了为什么我不能在一个程序的两个实例之间拖动一个点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DoDragDrop,我将数据设置为 Point 。当我在一个实例中拖动时 - 一切都OK。但是当我拖动程序的两个实例Visual Studio给我这个错误:

I have a DoDragDrop where I set the data to a Point.when I drag within one instance – everything's OK. But when I drag between two instances of the program Visual Studio gives me this error:


指定的记录不能映射到托管值类

The specified record cannot be mapped to a managed value class.

为什么?

编辑:这里是代码:

DataObject d = new DataObject();
d.SetData("ThePoint", MyPoint);
DragDropEffects e = DoDragDrop(d, DragDropEffects.Move);

And:

Point e2 = (Point)e.Data.GetData("ThePoint");


推荐答案


指定的记录无法映射

注意单词记录的奇怪性。它是一个以COM为中心的struct单词。你正在努力做的几乎工作,但不完整。 DoDragDrop()方法正确地将Point结构编组为COM对象,因为Point具有[ComVisible(true)]属性。缺少的成分是IRecordInfo所需的信息,IRecordInfo是描述结构布局的COM接口。必需,因为结构具有非常的编译器依赖的布局。

Note the oddity of the word "record". It is a COM-centric word for "struct". What you are trying to do almost works, but not quite. The DoDragDrop() method properly marshals the Point structure to a COM object, possible because Point has the [ComVisible(true)] attribute. The missing ingredient is the info required by IRecordInfo, a COM interface that describes the layout of the structure. Required because structures have a very compiler dependent layout.

此接口通常通过从类型库读取结构定义来实现。实际上这是可用的,Point struct在c:\windows\microsoft.net\framework\v2.0.50727\system.drawing.tlb中描述。您可以使用OleView.exe工具,File + View Typelib。

This interface is normally implemented by reading the structure definition from a type library. Which is in fact available, the Point struct is described in c:\windows\microsoft.net\framework\v2.0.50727\system.drawing.tlb. You can look at it with the OleView.exe tool, File + View Typelib.

除了COM对象的接收者必须将其翻译的部分外,一切都很好回到一个托管对象,一个点。这需要找出什么类型的库包含对象定义,因此IRecordInfo可以做它的工作。其中记录在注册表中,HKCR\Record键。哪个包含Point的条目。 Kaboom。

Everything good, except for the part where the receiver of the COM object has to translate it back to a managed object, a Point. That requires finding out what type library contains the object definition so IRecordInfo can do its job. Which is recorded in the registry, HKCR\Record key. Which does not contain an entry for Point. Kaboom.

创建自己的类(不是struct)来存储数据,给它[Serializable]属性,以便它可以平凡地被封送。

Create your own class (not struct) to store the data, give it the [Serializable] attribute so it can trivially be marshaled.

这篇关于为什么我不能在一个程序的两个实例之间拖动一个点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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