我们知道在VC++环境下利用Extended Combobox很容易实现下图的样子,但是在Vc#条件下没有可扩展的Combobox控件,所以在实现起来比较麻烦。这里笔者向大家推荐两个类,第一个类用来获取本地的文件、文件夹、磁盘图标,第二类用来在Combobox中显示图标。
一、获取文件、文件夹、本地磁盘的类GetIcon
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
namespace Gateway
{
public class GetIcon
{
[DllImport("Shell32.dll")]
private static extern IntPtr SHGetFileInfo
(
string pszPath,
uint dwFileAttributes,
out SHFILEINFO psfi,
uint cbfileInfo,
SHGFI uFlags
);
[StructLayout(LayoutKind.Sequential)]
private struct SHFILEINFO
{
public SHFILEINFO(bool b)
{
hIcon = IntPtr.Zero; iIcon = 0; dwAttributes = 0; szDisplayName = ""; szTypeName = "";
}
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.LPStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.LPStr, SizeConst = 80)]
public string szTypeName;
};
private enum SHGFI
{
SmallIcon = 0x00000001,
LargeIcon = 0x00000000,
Icon = 0x00000100,
DisplayName = 0x00000200,
Typename = 0x00000400,
SysIconIndex = 0x00004000,
UseFileAttributes = 0x00000010
}
/// <summary>
/// 根据文件扩展名得到系统扩展名的图标
/// </summary>
/// <param name="fileName">文件名(如:win.rar;setup.exe;temp.txt)</param>
/// <param name="largeIcon">图标的大小</param>
/// <returns></returns>
public static Icon GetFileIcon(string fileName, bool largeIcon)
{
SHFILEINFO info = new SHFILEINFO(true);
int cbFileInfo = Marshal.SizeOf(info);
SHGFI flags;
if (largeIcon)
flags = SHGFI.Icon | SHGFI.LargeIcon | SHGFI.UseFileAttributes;
else
flags = SHGFI.Icon | SHGFI.SmallIcon | SHGFI.UseFileAttributes;
IntPtr IconIntPtr=SHGetFileInfo(fileName, 256, out info, (uint)cbFileInfo, flags);
if (IconIntPtr.Equals(IntPtr.Zero))
return null;
return Icon.FromHandle(info.hIcon);
}
/// <summary>
/// 获取文件夹图标
/// </summary>
/// <returns>图标</returns>
public static Icon GetDirectoryIcon(bool largeIcon)
{
SHFILEINFO _SHFILEINFO = new SHFILEINFO();
int cbFileInfo = Marshal.SizeOf(_SHFILEINFO);
SHGFI flags;
if (largeIcon)
flags = SHGFI.Icon | SHGFI.LargeIcon;
else
flags = SHGFI.Icon | SHGFI.SmallIcon;
IntPtr IconIntPtr = SHGetFileInfo(@"", 0, out _SHFILEINFO, (uint)cbFileInfo, flags);
if (IconIntPtr.Equals(IntPtr.Zero))
return null;
Icon _Icon = System.Drawing.Icon.FromHandle(_SHFILEINFO.hIcon);
return _Icon;
}
}
}
二、扩展Combobox控件的类ComboBoxEx
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Gateway
{
class ComboBoxEx : ComboBox
{
private ImageList imageList;
public ImageList ImageList
{
get { return imageList; }
set { imageList = value; }
}
public ComboBoxEx()
{
DrawMode = DrawMode.OwnerDrawFixed;
}
protected override void OnDrawItem(DrawItemEventArgs ea)
{
ea.DrawBackground();
ea.DrawFocusRectangle();
ComboBoxExItem item;
Size imageSize = imageList.ImageSize;
Rectangle bounds = ea.Bounds;
try
{
item = (ComboBoxExItem)Items[ea.Index];
if (item.ImageIndex != -1)
{
imageList.Draw(ea.Graphics, bounds.Left, bounds.Top,
item.ImageIndex);
ea.Graphics.DrawString(item.Text, ea.Font, new
SolidBrush(ea.ForeColor), bounds.Left + imageSize.Width, bounds.Top);
}
else
{
ea.Graphics.DrawString(item.Text, ea.Font, new
SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
}
}
catch
{
if (ea.Index != -1)
{
ea.Graphics.DrawString(Items[ea.Index].ToString(), ea.Font, new
SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
}
else
{
ea.Graphics.DrawString(Text, ea.Font, new
SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
}
}
base.OnDrawItem(ea);
}
}
class ComboBoxExItem
{
private string _text;
public string Text
{
get { return _text; }
set { _text = value; }
}
private int _imageIndex;
public int ImageIndex
{
get { return _imageIndex; }
set { _imageIndex = value; }
}
public ComboBoxExItem()
: this("")
{
}
public ComboBoxExItem(string text)
: this(text, -1)
{
}
public ComboBoxExItem(string text, int imageIndex)
{
_text = text;
_imageIndex = imageIndex;
}
public override string ToString()
{
return _text;
}
}
}