Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / August 2005

Tip: Looking for answers? Try searching our database.

Drag & Drop Folder Name from Windows Explorer to a Windows Form

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David McKenzie - 16 Aug 2005 20:20 GMT
The title says it all.
What I would like to be able to do is drag a folder from Windows explorer on
to a form and just capture the full path and folder name.
Failing the fully qualified path, I absolutely need the folder name (which I
could find by iterating throught the folder structure)

All the doc I can find refers to dropping the data associated with an
object - and the Help topic
"Performing Drag-and-Drop Operations in Windows Forms"
Has not done much for me.
Am I missing something here?
Is what I want to do even possible?
"Jeffrey Tan[MSFT]" - 17 Aug 2005 05:26 GMT
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
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Lloyd Dupont - 17 Aug 2005 05:54 GMT
test that, that should gives you some idea....

public partial class Form1 : Form
{
   public Form1()
   {
       AllowDrop = true;
   }
   protected override void OnDragEnter(DragEventArgs drgevent)
   {
       base.OnDragEnter(drgevent);
       string[] fmt = drgevent.Data.GetFormats();
       foreach (string s in fmt)
           Console.WriteLine(s);
       Console.WriteLine(drgevent.Data.GetData(DataFormats.FileDrop));
   }
}

And remember, use the force, Oops, I mean the debugger is your friend!
David McKenzie - 17 Aug 2005 17:10 GMT
Thanks Jefferey and Lloyd for the quick responses to myt first post in this
forum.
I will check ithis and post back presently.
"Jeffrey Tan[MSFT]" - 18 Aug 2005 02:37 GMT
Hi David,

You are welcome. Please feel free to feedback any progress here, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

"Jeffrey Tan[MSFT]" - 22 Aug 2005 09:36 GMT
Hi David,

Have tried my code snippet? Is your problem resolved? Please feel free to
tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.