使用Windows API Code Pack for Microsoft使用ShellFile对象

时间:2011-10-28 19:59:02

标签: c# wpf winforms windows-api-code-pack

可以从here下载适用于Microsoft的Windows API代码包。这是一个非常好的库,它有很好的例子。例如,如果我打开zip中的解决方案WindowsAPICodePack10下载代码包(它只包含我添加了win表单和wpf应用程序的库)

enter image description here

然后我可以非常轻松地使用该库,例如在我的wpf应用程序中,我可以拖动:

enter image description here

ExplorerBrowser用户控件(注意我必须添加对解决方案附带的库的引用)

然后使用按钮我可以用这行代码填充该控件:

        // Create a new CommonOpenFileDialog to allow users to select a folder/library
        CommonOpenFileDialog cfd = new CommonOpenFileDialog();

        // Set options to allow libraries and non filesystem items to be selected
        cfd.IsFolderPicker = true;
        cfd.AllowNonFileSystemItems = true;

        // Show the dialog
        CommonFileDialogResult result = cfd.ShowDialog();

        // if the user didn't cancel
        if (result == CommonFileDialogResult.Ok)
        {
            // Update the location on the ExplorerBrowser
            ShellObject resultItem = cfd.FileAsShellObject;
            explorerBrowser1.NavigationTarget = resultItem;
            //explorerBrowser1.Navigate(resultItem);
        }

之后我能够拥有类似的东西:

enter image description here

这太棒了,但我不懂微软。如果他们为您提供这些库,他们应该可以轻松自定义该用户控件。我下载这些库的原因是因为我需要将文件放在stackpanel上的特定目录中,并且能够使用与explorer上的文件相同的功能(能够拖动文件,在右键单击文件时获取上下文菜单,将文件丢弃到该容器等)

无论如何我不需要所有的功能。从studing库我认为用户控件包含一个ShellContainer对象,它的childern可能是ShellFiles。

因此,我想从这个库中创建一个ShellFile对象并将其放在StackPanel中。在对图书馆进行繁琐的学习之后,我终于找到了如何从shellFile中实例化一个对象(ShellFile类是抽象的):

string filename = @"C:\Program Files (x86)\FileZilla FTP Client\filezilla.exe"; \\random file
ShellFile shellFile = ShellFile.FromFilePath(filename);

现在,如果我可以将该文件放在容器中,那将会很好。我无法实例化ShellConteiner对象,因为它也是抽象的。那我怎么能把那个shell文件放在画布上呢?

或者我可以提取我需要的属性并创建一个代表shellFile的用户控件。我知道如何获得缩略图,我可以做类似的事情:

        string filename = @"C:\Program Files (x86)\FileZilla FTP Client\filezilla.exe";

        ShellFile shellFile = ShellFile.FromFilePath(filename);
        System.Drawing.Bitmap btm = shellFile.Thumbnail.ExtraLargeBitmap;

0 个答案:

没有答案
相关问题