驱动器选择框与windows窗体中的图标

时间:2009-08-05 10:39:17

标签: c# .net windows winforms

是否有任何窗体控件显示带有图标的驱动器号列表?

3 个答案:

答案 0 :(得分:3)

不,但我确信你可以做到这一点,不应该太狡猾,无论是使用TreeView还是你只想要列表,那么你可以使用ListView。

获取驱动器的代码与此类似:

//Get all Drives
DriveInfo[] ListAllDrives = DriveInfo.GetDrives();

要确定ListViewItem或TreeViewNodes的图标,您可以执行以下操作:

foreach (DriveInfo Drive in ListAllDrives)
{
    //Create ListViewItem, give name etc.
    ListViewItem NewItem = new ListViewItem();
    NewItem.Text = Drive.Name;

    //Check type and get icon required.
    if (Drive.DriveType.Removable)
    {
    //Set Icon as Removable Icon
    }    
    //else if (Drive Type is other... etc. etc.)
}

答案 1 :(得分:0)

如果您愿意为此付费,可以查看http://viewpack.qarchive.org/

我不知道有任何免费控制。

答案 2 :(得分:0)

我终于想出了自己的控制权。

我用驱动器填写listview,如下所示:

listView1.SmallImageList = new ImageList();
var drives = DriveInfo.GetDrives()
                      .Where(x => x.DriveType == DriveType.Removable)
                      .Select(x => x.Name.Replace("\\",""));
foreach (var driveName in drives)
{
    listView1.SmallImageList.Images.Add(driveName, GetFileIcon(driveName));
    listView1.Items.Add(driveName, driveName);
}

其中GetFileIcon是我自己调用SHGetFileInfo的方法:

IntPtr hImgSmall;    //the handle to the system image list
SHFILEINFO shinfo = new SHFILEINFO();
//Use this to get the small Icon
hImgSmall = Win32.SHGetFileInfo(fileName, 0, ref shinfo,
                               (uint)Marshal.SizeOf(shinfo),
                                Win32.SHGFI_ICON |
                                Win32.SHGFI_SMALLICON);

return System.Drawing.Icon.FromHandle(shinfo.hIcon);

Win32类被复制为this site形式,如下所示:

[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
    public IntPtr hIcon;
    public IntPtr iIcon;
    public uint dwAttributes;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
    public string szDisplayName;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
    public string szTypeName;
};

class Win32
{
    public const uint SHGFI_ICON = 0x100;
    public const uint SHGFI_LARGEICON = 0x0;    // 'Large icon
    public const uint SHGFI_SMALLICON = 0x1;    // 'Small icon

    [DllImport("shell32.dll")]
    public static extern IntPtr SHGetFileInfo(string pszPath,
        uint dwFileAttributes,
        ref SHFILEINFO psfi,
        uint cbSizeFileInfo,
        uint uFlags);
}

我希望它会帮助别人。