C# drag-and-drop compatibility with old MFC app

R

Roger Norris

One of my development team's older apps uses the clipboard
formats CFSTR_FILECONTENTS and CFSTR_FILEDESCRIPTOR to
implement drag & drop functionality. From an MSDN article
on Shell Clipboard Formats, "These two formats are the
preferred way to transfer Shell objects that are not stored
as file-system files." The objects we're transfering are
not stored as file-system files, and we're also having to
use delayed rendering - two good reasons not to use the
more common CF_HDROP clipboard format.

We're now trying to start doing all new development with
..NET (C#). It is very important for our new apps to be
fully compatible with the old ones. However, so far I've
not been able to figure out a way to get our C# Windows
Forms app to support that clipboard format for drag & drop
operations.

The FILEDESCRIPTOR format is not a problem. The
IDataObject provided by the DragEventArgs will give me back
a byte stream which I can use to extract the names of the
files being dropped.

However, attempting to get at the FILECONTENTS object
results in nothing. The IDataObject doesn't return any
sort of object; it just returns null. Obviously, the names
of the files aren't very useful if I can't get at the data
which is supposed to go in them. Also, even if it did
work, I can see no way to specify which file to retrieve.
In our MFC C++ app, the call to GetData would allow us to
specify the index of the file we wanted in the FORMATETC
parameter. But .NET's IDataObject.GetData doesn't have an
analagous parameter.

Can someone offer a way to support that clipboard format in
a C# Windows Forms app?
 
L

Lion Shi

Hello Roger,

Unfortunately, there is no managed solution for supporting
CFSTR_FILECONTENTS. The current implementation of the DataObject class in
the .NET Framework does not permit specifying a lindex value as required by
the CFSTR_FILECONTENTS clipboard format. The only solution available at
this time is to use interop and implement an IDropTarget interface and
call the RegisterDragDrop API to register the desired window(s) as a drop
target. Your IDropTarget::Drop can then call into the IDataObject::GetData
method, specifying the index of the file in the FORMATETC structure you
are interested in.

Currently, the easiest way to implement IDropTarget and use IDataObject
would be in either unmanaged C/C++ or managed C++. There is no completely
managed solution for this issue.

I hope this info is helpful for you.

Best regards,

Lion Shi [MSFT]
MCSE, MCSD
Microsoft Support Engineer
Get Secure! ¨C www.microsoft.com/security

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2003 Microsoft Corporation. All rights
reserved.
--------------------
| Content-Class: urn:content-classes:message
| From: "Roger Norris" <[email protected]>
| Sender: "Roger Norris" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: RE: C# drag-and-drop compatibility with old MFC app
| Date: Mon, 28 Jul 2003 12:51:40 -0700
| Lines: 75
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: quoted-printable
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MIMEOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcNVQanlUSeynB68R/y4tXwp+AVJKg==
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:49146
| NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| Thank you for your response, but I don't think I have
| communicated my problem clearly.
| I can enumerate the clipboard formats just fine. Calling
| e.Data.GetFormats() returns two valid formats:
| "FileGroupDescriptor" and "FileContents".
| I then try to retrieve the data that is stored for each of
| these formats:
| private void onDragDrop(object sender, DragEventArgs e)
| {
| object foo = e.Data.GetData("FileGroupDescriptor");
| object bar = e.Data.GetData("FileContents");
| }
| The object "foo" is now a byte stream, which I can use to
| retrieve the names of the files that have been dropped onto
| my window. The object "bar", however, comes back as a
| null. I have no way of getting at the data in the
| "FileContents" clipboard format, and that is my problem.
| Changing the code to only request the "FileContents"
| (instead of both formats) does NOT fix the problem:
| private void onDragDrop(object sender, DragEventArgs e)
| {
| object bar = e.Data.GetData("FileContents");
| }
| Do you know of any workaround to get at that clipboard
| format from .NET? To answer your other question, yes, the
| thread is running in STA mode.
| Thank you for your help.
| - Roger
| >-----Original Message-----
| >Hello Roger,
| >
| >The first thing we should check is that the thread is run
| STA mode. The
| >underlying native implementation of clipboard needs the
| STA thread mode.
| >Then we can enumerate all data formats and data in the
| DataObject, and get
| >the preper one from it:
| >
| >private void Form1_DragDrop(object sender,
| >System.Windows.Forms.DragEventArgs e)
| >{
| > //Get the available data formats
| > foreach(string format in e.Data.GetFormats())
| > Console.WriteLine(format);
| >
| > //Get your data with appropriate format
| > //Object obj = data.GetData("Format");
| >}
| >
| >I hope this helps you.
| >
| >Best regards,
| >
| >Lion Shi [MSFT]
| >MCSE, MCSD
| >Microsoft Support Engineer
| >Get Secure! ¨C www.microsoft.com/security
| >
| >This posting is provided "AS IS" with no warranties, and
| confers no rights.
| >You assume all risk for your use. 2003 Microsoft
| Corporation. All rights
| >reserved.
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top