拖放选项 [英] drag and drop options

查看:74
本文介绍了拖放选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个简单的工具。如果我拖动n个放置文件,我的文件夹将自动打开相应的文件夹和文件夹。现在我想为 .lnk 文件(快捷方式),如果我拖动一个 .lnk 文件,它必须打开目标文件

解决方案

好的,这是一个简单的模拟,但你应该得到想法...



首先添加 COM'Windows脚本宿主对象模型'引用到您的项目。



该行...

 使用IWshRuntimeLibrary; 

对于这个例子,我只是使用了一个列表框控件,但使用了你想要的...如果你处理 DragEnter 事件,可以获取作为参数传递的文件名。然后,您可以创建一个 WshShell 对象来获取链接的目标路径。

  private void listBox1_DragEnter发件人,DragEventArgs e)
{
String [] fileName =(String [])e.Data.GetData(FileName);

WshShell shell = new WshShell();
IWshShortcut link =(IWshShortcut)shell.CreateShortcut(fileName [0]);

String targetPath = link.TargetPath;

listBox1.Items.Add(targetPath);
}

代码不处理非快捷键等,但它应该给你一个启动...:)


I'm doing a simple tool. If I drag n drop files ,folders into my form it will automatically open the corresponding file and folder. Now I want to do it for .lnk files(shortcuts) if I drag a .lnk file, it must open the target file.

解决方案

Okay this is a simple mock up but you should get the idea...

First add the COM 'Windows Script Host Object Model' reference to your project.

Next include the line...

using IWshRuntimeLibrary;

For this example I just used a list box control but use what ever you want... If you handle the DragEnter event, you can get the file name passed as an argument. You can then create a WshShell object to get the links target path.

private void listBox1_DragEnter(object sender, DragEventArgs e)
{
    String[] fileName = (String[])e.Data.GetData("FileName");

    WshShell shell = new WshShell();
    IWshShortcut link = (IWshShortcut)shell.CreateShortcut(fileName[0]);

    String targetPath = link.TargetPath;

    listBox1.Items.Add(targetPath);
}

The code doesn't handle non shortcuts etc but it should give you a starter... :)

这篇关于拖放选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆