RE: Drag & Drop Folder Name from Windows Explorer to a Windows Form by v-jetan
v-jetan
Tue Aug 16 23:26:04 CDT 2005
Hi David,
Thanks for your post.
What you want is the windows shell drag&drop support. Currently, there is
no build-in support for doing shell drag&drop in Winform. However, we can
p/invoke some Shell API to get this done.
Below is a little sample I wrote to demonstrate what you want(we can place
this code snippet in any form class):
private const int WS_EX_ACCEPTFILES=0x00000010;
protected override CreateParams CreateParams
{
get
{
CreateParams cp=base.CreateParams;
cp.ExStyle=cp.ExStyle|WS_EX_ACCEPTFILES;
return cp;
}
}
[DllImport("shell32.dll", CharSet=CharSet.Auto)]
public static extern int DragQueryFile(IntPtr hDrop, int iFile,
StringBuilder lpszFile, int cch);
[DllImport("shell32.dll")]
static extern void DragFinish(IntPtr hDrop);
private const int WM_DROPFILES=0x0233;
protected override void WndProc(ref Message m)
{
if(m.Msg==WM_DROPFILES)
{
int iNumChars=DragQueryFile(m.WParam , 0, null, 0);
StringBuilder szDropFilePath = new StringBuilder(iNumChars+1);
DragQueryFile(m.WParam , 0, szDropFilePath, iNumChars+1);
DragFinish(m.WParam );
this.Text=szDropFilePath.ToString();
}
base.WndProc (ref m);
}
First, we add WS_EX_ACCEPTFILES window extended style to our form, this
will enable our form to accept shell file drop and display an accept icon
for dragging.
Whenever a file is dragged over our form, WM_DROPFILES will be posted to
the form WndProc. In this notificaiton, we can P/invoke DragQueryFile Shell
API to get the dropped files collection. Currently, I just pass 0 for the
second parameter to get the first file in the dropped file collection. For
more detailed usage of this API, please search it in MSDN documentation.
Hope this helps.
===================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.