简体   繁体   中英

C# DoDragDrop on Non-Serializable object

I have a UserControl that can be dragged around my form. I get a first chance exception when the control is accidentally dragged away from my form and into the desktop (as an example):

A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in System.Windows.Forms.dll

Additional information: Invalid FORMATETC structure (Exception from HRESULT: 0x80040064 (DV_E_FORMATETC))

When I start the drag/drop process, I pass in an object of type Control. It is not Serializable nor do I want it to be. Is there a way I can work around this or is there handling for dragging an object outside the host form?

Callstack:

>   System.Windows.Forms.dll!System.Windows.Forms.DataObject.GetDataIntoOleStructs(ref System.Runtime.InteropServices.ComTypes.FORMATETC formatetc, ref System.Runtime.InteropServices.ComTypes.STGMEDIUM medium) + 0x175 bytes 
    System.Windows.Forms.dll!System.Windows.Forms.DataObject.System.Runtime.InteropServices.ComTypes.IDataObject.GetDataHere(ref System.Runtime.InteropServices.ComTypes.FORMATETC formatetc, ref System.Runtime.InteropServices.ComTypes.STGMEDIUM medium) + 0x70 bytes    
    System.Windows.Forms.dll!System.Windows.Forms.DataObject.System.Runtime.InteropServices.ComTypes.IDataObject.GetData(ref System.Runtime.InteropServices.ComTypes.FORMATETC formatetc, out System.Runtime.InteropServices.ComTypes.STGMEDIUM medium) + 0x152 bytes   
    [Native to Managed Transition]  

Here is another stack, but VS2010 hung so I couldn't copy and paste it. 截图

It isn't clear to me how this exception is getting triggered, the desktop should have no use for your control. The mouse cursor should show the "cannot drop here" shape. Nevertheless, first-chance exceptions during D+D are meaningless, they are swallowed and do not otherwise affect the operation. You only see them because you've got a debugger attached.

If you really want to drill this down then use Debug + Exceptions, tick the Thrown box for Common Language Runtime exceptions. The debugger will stop when the exception is raised. It is quite likely that it is raised inside Winforms plumbing code, you won't have source code to look at unless you enable the Reference Source. Post the stack trace in your question if you can't figure it out. Be sure to copy the entire one, scroll it if necessary to get the top activation frame.

But reiterating, this should not be a problem in practice. The user should see the 'cannot drop' mouse cursor, the exception is swallowed without terminating your program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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