Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

56 views
Skip to first unread message

Roger Norris

unread,
Jul 24, 2003, 2:21:33 PM7/24/03
to
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?

Lion Shi

unread,
Jul 28, 2003, 5:28:12 AM7/28/03
to
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.
--------------------
| Content-Class: urn:content-classes:message
| From: "Roger Norris" <rogerc...@education.ti.com>
| Sender: "Roger Norris" <rogerc...@education.ti.com>
| Subject: C# drag-and-drop compatibility with old MFC app
| Date: Thu, 24 Jul 2003 11:21:33 -0700
| Lines: 36
| Message-ID: <027301c35210$69051140$a301...@phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcNSEGkFn3qE7EVlQg6WOY8osIfVTQ==
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| NNTP-Posting-Host: tk2msftngxa11.phx.gbl 10.40.1.163
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGXS01.phx.gbl!TK2MSFTNGXS02.phx.gbl!cpmsftngxa1
0.phx.gbl!cpmsftngxa07.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:48983
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms

Roger Norris

unread,
Jul 28, 2003, 3:51:40 PM7/28/03
to
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

Lion Shi

unread,
Jul 30, 2003, 11:44:10 PM7/30/03
to
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" <rogerc...@education.ti.com>
| Sender: "Roger Norris" <rogerc...@education.ti.com>

| References: <027301c35210$69051140$a301...@phx.gbl>
<mhoCrqOV...@cpmsftngxa06.phx.gbl>
| Subject: RE: C# drag-and-drop compatibility with old MFC app
| Date: Mon, 28 Jul 2003 12:51:40 -0700
| Lines: 75
| Message-ID: <02a301c35541$a9e57bd0$a401...@phx.gbl>


| 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

0 new messages