Windows Develop Bookmark and Share   
 index > Windows Forms General > Subscribing to an Explorer drag drop event from a C# form
 

Subscribing to an Explorer drag drop event from a C# form

Hi there.
I'm trying to subscribe to a DragStart event that is raised from the Explorer, or any other window application.

My application is a C# Windows Form. I would like to intercept (from my C# application) when a user starts a drag action from any Windows application, doing some work accordingly.

Once I catch the Drag Event, I'm mostly interested in the file name and the content stream.

Could you give me some pointers so I can further research this subject?

Thanks in advance.
Cordially,

Agustin Garzon.

TheAgus  Thursday, August 14, 2008 1:17 PM

Hi,Agustin,

U can implement IMessageFilter so that ur Form can be added to the application's message pump to filter out a message or perform other operations before the message is dispatched to a form or control.

Then,call Application.AddMessageFilter methodto add ur form to an application's msg pump.

If it's about file drag&drop,by default,Form doesn't support file dragging.But these two winapisDragAcceptFiles and DragQueryFile could help to solve this problem.

Wish this could be helpful to u.

Kind Rgds.

kuki84  Thursday, August 14, 2008 7:35 PM
That's not possible. Writing a shell extention handler is the closest you could get. That's not something that's easily done in C# and strongly discouraged by MSFT.
  • Unmarked As Answer byTheAgus Wednesday, April 08, 2009 1:08 AM
  •  
nobugz  Friday, August 15, 2008 1:25 AM

hi,nobugs,

"That's not possible."

u mean what is impossible?

kuki84  Friday, August 15, 2008 6:12 AM

My first thought was screaming:"nobugz, I don't buy your that's not possible", with all due respect my friend.

I'm pretty certain it's possible, perhaps through Interop.

Somewhere one suggested monitoring the clipboard, like when you start a Drag Action in Windows, the element is placed in the clipboard. If that were possible, would be nice, but not good enough, this wouldn't tell when the element is dropped.

I also have the hope it could be done with a WMI query. I ran a sample that was monitoring for instantiation of new processes on Windows.This was great, I was able to get information about applications opened in Windows from the .NET application. There should be some kind of query that looks for drag-drop actions.

I found a C++ solution on here, so I'll stick to the "it's possible, it can be done in a clean manner" idea.

http://www.codeproject.com/KB/shell/explorerdragdrop.aspx

Kuki, I read your suggestion, thanks for posting Smile
Do you have any experience with the IMessageFilter interface? I tested a sample, but I haven't found much documentation, would it be able to intercept "drag-start" "drag-end" events? If you have more specific comments, they would be great Smile

Thanks for your help Smile

Cordially,

Agustin Garzon

TheAgus  Friday, August 15, 2008 1:18 PM

Hi,Agustin,

here is the more easier way.Wish it be helpful to u.

You drag a file from explorer to this form1,and then the form show out the filename. Is this what u want?

Code Snippet

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明?br>/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量?br>/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需?br>//
InitializeComponent();

//
// TODO: ?InitializeComponent 调用后添加任何构造函数代?br>//
}

/// <summary>
/// 清理所有正在使用的资源?br>/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方?- 不要使用代码编辑器修?br>/// 此方法的内容?br>/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AllowDrop = true;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "Form1";
this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form1_DragDrop);
this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Form1_DragEnter);

}
#endregion

/// <summary>
/// 应用程序的主入口点?br>/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
e.Effect = DragDropEffects.Link;
}

private void Form1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
MessageBox.Show(((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString());
}
}
}

kuki84  Saturday, August 16, 2008 4:57 PM

Dear Kuki, thanks for posting.

That's not what I'm looking for, that would be easy.

Based on your sample, I'll need to intercept the moment when an Explorer file starts being dragged. Your sample works when a file is dropped on the form.

Basically, from the .NET Windows Form I'd need to subscribe to the event raised by the Explorer on Drag Start.
It's way more easier to say than to do.

But I'll keep waiting for some advice on how to achieve this, through Interop or a WMI query perhaps.

Cordially,

Agustin Garzon

TheAgus  Saturday, August 16, 2008 7:20 PM

Hi,Agustin,

If you only interest in the start of a drag action from other form,I'm afraid that all the methoeds we talked about can not work.

I've read the article at codeprject.com u refered to,but I didn't find any solution that fit for u question.Maybe I didn't understand this documents well,can you kindly point it out?

Access the data contained in IDataObject may be a good way to work out ur question,but is there any way can a form reach that without implementing IDragTarget and being dropped into?At least I didn't find it out.ClipSpy can show it out,but it's also be dropped into.Actually I think the IDataObject Data isn't set to clipboard neither.What we can catch is the mouse events,but I don't think it's enough to support the IDataObject accessing. It seems that the file info of drag&drop only open for the drag target.

Can Mr. Nobugz give a further explanation of this discussing and ur last posting?

And Agustin,if u only interested in file copying and moving,I agree with Nobugz,try to use CopyHook...


kuki84  Sunday, August 17, 2008 6:45 PM

Dear Kika, I appreciate your effort butyour suggestions seems not to be leading to the place I want to go.

If it's only you andI talking on here I don't think we'll get to thesolution.

I think someone who reads my first post will understand what I'm trying to achieve, if I find a solution I'll post it back here.

I'll write one more time what I'm trying to make:

1-My C# Window Form is open, then I decide to minimizethis window.

2- Then I go to the desktop and start dragging a .txt file. My C# Form shouldmaximize again.

It's all solved already, except for the file dragging part. I need to interceptthe Drag Start actionstarted on the explorer. The file may or may be not dropped onmy windows form, that is not relevant to me.

The form is only supposed to maximize back up when the user starts dragging a .txt file from an external Windows Application.

Approaches and pointers will be more than welcome Smile

Sincerely,

Agustin Garzon

TheAgus  Sunday, August 17, 2008 7:54 PM

hi,Agustin,

Now I understand what u want by ur this posting.If this,CopyHook would benefit nothingSad

I've also try to find a way to capture the drag start action start from explorer of other forms as talked about on my last posting,but failed,and I thinkit may beimpossible for now.

Wish somebody else can give some constructive advice.

Good Luck!

kuki84  Sunday, August 17, 2008 8:36 PM

Ok, the solution would be writting a Shell Extension.

Nevertheless, writting a Shell Extension in managed code (C#) is strongly discouraged.

It could be done in unmanaged C++, but I was expecting to do it in C#.

So, bottom line, it can't be done in C# by writting a shell extension.

Why? Well,I don't think you will find a better answer than the one I found below!

See here, 2 pages of comments about this:

http://blogs.msdn.com/junfeng/archive/2005/11/18/494572.aspx

You might also want to take a look here, which is pretty much the same:

http://blogs.msdn.com/junfeng/archive/2005/11/18/494572.aspx

I'm studying some very nice Shell Extension articles anyway. Very interesting.

Cordially,

Agustin Garzon

TheAgus  Friday, August 22, 2008 1:02 AM

You can use google to search for other answers

Custom Search

More Threads

• Is there an easy way to sent a DataGridView to the printer?
• Outlook Reference.
• DataGridView custom cell template
• [help] device manager show nothing!
• Creating Windows forms without Visual Studio ?
• Staus bar not visible in MDI Frame
• How to get mp3 tag information
• AddOwnedForm of Container.
• Why is the filling of a ComboBox so slow?
• WebBrowser disappears when docking its parent window