拖&放;降动态创建的快捷方式快捷方式、动态

2023-09-03 01:00:52 作者:时光机

我有一个C#应用程序创建的快捷方式,以启动与特定的参数和初始目录的其他程序。我希望用户能够从Windows窗体拖动一个快捷方式拖放到任意位置相关的如桌面,开始菜单,等等,但我真的不知道如何处理,任何人都可以点我的正确的方向?

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?

我已经看到了使用PInvoke的几件样品,并的IShellLink 喜欢这个,或读答案的那么像here,这已经帮助创建快捷方式,并将其保存在一个.lnk文件。我想我有当用户通过处理一个的MouseDown 信号。这是据我得到的,我想我需要确切地知道哪种类型的目标是希望接受的下降,以及如何序列化的捷径,但无法找到该部分的任何信息。

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.

该框架的版本是目前3.5,而我只考虑Windows平台。

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

在此先感谢您的帮助!

更新/解决方案:

使用 ShellLink code 上述创建一个临时的快捷方式文件,我只是用数据对象在拖放一样下面的例子:

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);
}

如果你考虑到的PInvoke code(这里没有显示),而我还需要创建一个目标名称该临时文件相当复杂。如果有人知道......呃,快捷方式,它的欢迎!或许到移植的code,对于它John Knoeller 给了一个链接(感谢!)。

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!).

推荐答案

雷蒙德陈没上他的博客这个题目一整篇文章退房的拖动一个虚拟文件

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

 
精彩推荐
图片推荐