Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

c# - Drag & drop of a dynamically created shortcut

I have a C# application that creates shortcuts to launch other programs with specific arguments and initial directories. I would like the user to be able to drag a shortcut from the Windows form and drop it anywhere relevant like the desktop, the start menu, and so on but I don't really know how to handle that, could anyone point me in the right direction?

I have seen a few samples using PInvoke and IShellLink like this one, or read answers on SO like here, which already help create shortcuts and save them in a .lnk file. I assume I have to hand over data in a DoDragDrop() call when the user initiates a drag operation, for example by handling a MouseDown signal. That's as far as I got, I suppose I need to know exactly which type the target is expecting to accept the drop, and how to serialize the shortcut, but couldn't find any information on that part.

Perhaps another option would be to get the location of the drop, and manage that from my application, but there again I'm a bit clueless as how to do that.

The framework version is currently 3.5, and I'm only considering Windows platforms.

Thanks in advance for your help!


Update/Solution:

Using the ShellLink code mentioned above to create a temporary shortcut file, I simply used DataObject for the drag and drop, like in the following example:

private void picShortcut_MouseDown(object sender, MouseEventArgs e)
{
    ShellLink link = new ShellLink();

    // Creates the shortcut:
    link.Target = txtTarget.Text;
    link.Arguments = txtArguments.Text;
    link.Description = txtDescription.Text;
    link.IconPath = txtIconFile.Text;
    link.IconIndex = (txtIconIndex.Text.Length > 0 ?
        System.Int32.Parse(txtIconIndex.Text) : 0);
    link.Save("tmp.lnk");

    // Starts the drag-and-drop operation:
    DataObject shortcut = new DataObject();
    StringCollection files = new StringCollection();
    files.Add(Path.GetFullPath("tmp.lnk"));
    shortcut.SetFileDropList(files);
    picShortcut.DoDragDrop(shortcut, DragDropEffects.Copy);
}

Quite complicated if you consider the PInvoke code (not shown here), and I still need to create this temporary file with the target name. If anyone knows a... erm, shortcut, it's welcome! Perhaps by porting the code for which John Knoeller gave a link (thanks!).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Raymond Chen did a whole article on this very topic on his blog check out dragging a virtual file


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...