有没有任何Windows窗体控件显示带有图标的驱动器号的列表?
不,但我相信你可以做到这一点,不应该太棘手,无论是与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.) }
如果您愿意为此付费,可以查看http://viewpack.qarchive.org/ 。
我不知道有任何的自由控制。
我终于想出了自己的控制。
我用驱动器填充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类被复制为这个站点的形式,如下所示:
[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); }
我希望这会帮助别人。