拖放删除动态创建的快捷方式

发布于 2024-08-15 10:18:43 字数 2095 浏览 9 评论 0原文

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

我看过一些使用 PInvoke 和 IShellLink这个,或者阅读SO上的答案就像这里,它已经帮助创建快捷方式并将它们保存在 .lnk 文件中。我假设当用户启动拖动操作时,我必须在 DoDragDrop() 调用中移交数据,例如通过处理 MouseDown 信号。据我所知,我想我需要确切地知道目标期望接受哪种类型的放置,以及如何序列化快捷方式,但找不到有关该部分的任何信息。

也许另一种选择是获取掉落的位置,并从我的应用程序中管理它,但我对如何做到这一点还是有点无能为力。

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

预先感谢您的帮助!


更新/解决方案:

使用ShellLink上面提到的代码来创建临时快捷方式文件,我只是使用DataObject进行拖放,如下例所示:

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代码(此处未显示),则相当复杂,我仍然需要使用目标名称创建这个临时文件。如果有人知道...呃,捷径,欢迎!也许通过移植 John Knoeller 给出的代码一个链接(谢谢!)。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

度的依靠╰つ 2024-08-22 10:18:43

Raymond Chen 在他的博客上写了一篇关于这个主题的整篇文章,请查看 拖动虚拟文件

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

洒一地阳光 2024-08-22 10:18:43

我在之前的帖子中回答了一个与此类似的问题。这可能是您的一个起点。

拖放链接

I answered a question sort of similar to this on a previous thread. This might be a starting point for you.

Drag and Drop link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文