using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace PG
{
public class GListBoxItem
{
private string _myText;
private int _myImageIndex; // 属性
public string Text
{
get { return _myText; }
set { _myText = value; }
}
public int ImageIndex
{
get { return _myImageIndex; }
set { _myImageIndex = value; }
}
private int _imageOffset = 0;
public int ImageOffset
{
get { return _imageOffset; }
set { _imageOffset = value; }
}
//构造函数
public GListBoxItem(string text, int index)
{
_myText = text;
_myImageIndex = index;
}
public GListBoxItem(string text, int index,int imageOffset):this(text,index)
{
_imageOffset = imageOffset;
}
public GListBoxItem(string text) : this(text, -1)
{
}
public GListBoxItem() : this("")
{
}
public override string ToString()
{
return _myText;
}
}
public class GListBox : ListBox
{
private ImageList _myImageList;
public ImageList ImageList
{
get { return _myImageList; }
set { _myImageList = value; }
}
public GListBox()
{
this.DrawMode = DrawMode.OwnerDrawVariable;
this.ItemHeight = 20;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
if (e.Index == -1)
return;
e.DrawBackground();
e.DrawFocusRectangle();
GListBoxItem item;
Rectangle bounds = e.Bounds;
Size imageSize = _myImageList.ImageSize;
try
{
item = (GListBoxItem)Items[e.Index];
if (item.ImageIndex != -1)
{
ImageList.Draw(e.Graphics, bounds.Left + item.ImageOffset, bounds.Top + 2, item.ImageIndex);
e.Graphics.DrawString(item.Text, e.Font, new SolidBrush(e.ForeColor), bounds.Left + item.ImageOffset + imageSize.Width, bounds.Top + 2);
}
else
{
e.Graphics.DrawString(item.Text, e.Font, new SolidBrush(e.ForeColor), bounds.Left, bounds.Top+2);
}
}
finally
{
}
}
}
}