拖放删除动态创建的快捷方式
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Raymond Chen 在他的博客上写了一篇关于这个主题的整篇文章,请查看 拖动虚拟文件
Raymond Chen did a whole article on this very topic on his blog check out dragging a virtual file
我在之前的帖子中回答了一个与此类似的问题。这可能是您的一个起点。
拖放链接
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