.NET 自定义图片下拉框组件ImageComboBox
Demo
ImageComboBox 下拉效果
选择箭头样式1效果:
使用方法
C# 全选
//初始化图片下拉组件
ucImageComboBox1.ItemHeight = 21; //设置行高
ucImageComboBox1.Items.Clear();
ucImageComboBox1.Items.Add(new ComboBoxItemEx("箭头1", 0));
ucImageComboBox1.Items.Add(new ComboBoxItemEx("箭头2", 1));
ucImageComboBox 组件
C# 全选
/// <summary>
/// 图片下拉组件(www.csframework.com)
/// </summary>
public class ucImageComboBox : ComboBox
{
const int ICON_MARGIN_TOP = 2;
const int TEXT_MARGIN_TOP = 3;
private ImageList imagelist;
/// <summary>
/// 图片列表
/// </summary>
public ImageList ImageList
{
get
{
return this.imagelist;
}
set
{
this.imagelist = value;
}
}
public ucImageComboBox()
{
base.DrawMode = DrawMode.OwnerDrawFixed;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
if (this.imagelist != null)
{
Size imageSize = this.imagelist.ImageSize;
Rectangle bounds = e.Bounds;
try
{
ComboBoxItemEx item = (ComboBoxItemEx)base.Items[e.Index];
if (item.ImageIndex != -1)
{
this.imagelist.Draw(e.Graphics, bounds.Left, bounds.Top - ICON_MARGIN_TOP, item.ImageIndex);
e.Graphics.DrawString(item.Text, e.Font, new SolidBrush(e.ForeColor), (float)(bounds.Left + imageSize.Width), (float)bounds.Top + TEXT_MARGIN_TOP);
}
else
{
e.Graphics.DrawString(item.Text, e.Font, new SolidBrush(e.ForeColor), (float)bounds.Left, (float)bounds.Top + TEXT_MARGIN_TOP);
}
}
catch (Exception ex)
{
if (e.Index != -1)
{
e.Graphics.DrawString(base.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), (float)bounds.Left, (float)bounds.Top);
}
else
{
e.Graphics.DrawString(this.Text, e.Font, new SolidBrush(e.ForeColor), (float)bounds.Left, (float)bounds.Top);
}
}
}
base.OnDrawItem(e);
}
}
C# 全选
/// <summary>
/// 列表项目对象模型
/// </summary>
public class ComboBoxItemEx
{
/// <summary>
/// 文本内容
/// </summary>
public string Text { get; set; }
/// <summary>
/// 图片序号
/// </summary>
public int ImageIndex { get; set; }
public ComboBoxItemEx(string text, int imageIndex)
{
this.Text = text;
this.ImageIndex = imageIndex;
}
}
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
csframework.workflow C/S框架网