5

在 Windows 中,无论是在桌面上还是在 Windows 资源管理器中,我都想检测文件或文件夹被选中(突出显示)的时刻。发生这种情况时,我想显示一个显示文件或文件夹全名的消息框。

如果选择了多个项目,我想显示所有项目。

请注意,我的解决方案必须用 C# 编写。

4

2 回答 2

5

看一下这个例子来获取鼠标点击或选择的事件:

https://stackoverflow.com/questions/7222749/i-created-a-program-to-hide-desktop-icons-on-double-click-of-desktop-but-would-o

加入以下代码,记住添加对 SHDocVW.dll 和 Shell32.dll 的引用,这将返回每个资源管理器中所有选定的项目和文件夹路径。

public void GetListOfSelectedFilesAndFolderOfWindowsExplorer()
    {
        string filename;
        ArrayList selected = new ArrayList();
        var shell = new Shell32.Shell();
        //For each explorer
        foreach (SHDocVw.InternetExplorer window in new SHDocVw.ShellWindows())
        {
            filename = Path.GetFileNameWithoutExtension(window.FullName).ToLower();
            if (filename.ToLowerInvariant() == "explorer")
            {
                Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)window.Document).SelectedItems();
                foreach (Shell32.FolderItem item in items)
                {
                    MessageBox.Show(item.Path.ToString());
                    selected.Add(item.Path);
                }
            }
        }
    }
于 2013-03-27T09:20:09.053 回答
2

只需在 Renier 的回答中添加一些内容:

  • SHDocVW.dll 和 Shell32.dll 位于文件夹C:\Windows\System32
  • 如果您在SHDocVw.ShellWindowsClass()中遇到错误,只需右键单击解决方案资源管理器上的SHDocVw引用,然后选择 Properties 并将 Embed Interop Types设置为 false
于 2017-10-16T23:15:26.983 回答