开发者

Force drop as shortcut object when dragging from .NET app to Windows Explorer

I'm writing a .NET application that presents a Tree/List view of objects in the same way as Windows Explorer. These are text-based items that the user can regard as files, and I want the user to be able to create shortcuts to them similar to the way they would create a shortcut to any file. Except in this instance the shortcut would be to my application with a specific command line rather than to a fil开发者_开发知识库e object. So I need to support drag and drop to Internet Explorer such that the drag icon looks like the Alt-drag (create shortcut) icon, and the drop operation creates a shortcut to my application with a custom command line.

My question: what information do I need to supply to Windows at the start of the drag operation, so that Windows Explorer displays the correct icon and always creates a shortcut in the drop event?


The easiest way to do this is to 1) create the shortcut and 2) drag it. I don't do much managed code at all so I can't say if there is any built-in support for the IShellLink object. If not, somebody has probably already done the work and built a wrapper. So you could create the ShellLink object and fill it in with the path to your application, parameters, etc. and save it somewhere (perhaps the temp folder). Then you would initiate the drag operation with something like this:

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
    {
        // left mouse button clicked; begin drag
        // create data object with FileDrop format (list of files to be dropped)
        DataObject data = new DataObject();
        System.Collections.Specialized.StringCollection paths = new System.Collections.Specialized.StringCollection();
        paths.Add(@"<temp_path>\My New Shortcut.lnk");
        data.SetFileDropList(paths);
        // begin the drag, indicating it is a copy operation
        DragDropEffects effect = DoDragDrop(data, DragDropEffects.Copy);
    }
}

Then when the user drops it (e.g. on their desktop) it will copy the shortcut file from the temp folder.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜