C# windows窗体中带有图标的驱动器选择框
是否有显示带有图标的驱动器盘符列表的windows窗体控件?如果您愿意支付此费用,可以签出C# windows窗体中带有图标的驱动器选择框,c#,.net,windows,winforms,C#,.net,Windows,Winforms,是否有显示带有图标的驱动器盘符列表的windows窗体控件?如果您愿意支付此费用,可以签出 我不知道有任何免费控件。没有,但我相信你可以做到,这应该不会太棘手,无论是使用树状视图还是如果你只是想要列表,那么你可以使用ListView 获取驱动器的代码如下所示: //Get all Drives DriveInfo[] ListAllDrives = DriveInfo.GetDrives(); 要确定ListViewItem或TreeViewNodes的图标,可以执行以下操作: foreach
我不知道有任何免费控件。没有,但我相信你可以做到,这应该不会太棘手,无论是使用树状视图还是如果你只是想要列表,那么你可以使用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.)
}
我终于有了自己的控制权 我用驱动器填充列表视图,如下所示:
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);
}
[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);
}
其中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类按原样复制,如下所示:
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);
}
[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);
}
我希望它能帮助一些人。请注意,使用shell32.dll时要小心,它很容易更改(你知道microsoft是什么样的!),我在这里问了一个关于使用它们的图标的问题