1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82:
| 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; } } public GListBoxItem(string text, int index) { _myText = text; _myImageIndex = index; } public GListBoxItem(string text) : this(text, -1) { } public GListBoxItem() : this("") { } public override string ToString() { return _myText; } } public class GListBox : ListBox { private ImageList _myImageList = new ImageList(); public ImageList ImageList { get { return _myImageList; } set { _myImageList = value; } } public GListBox() { this.DrawMode = DrawMode.OwnerDrawFixed; } protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e) { e.DrawBackground(); e.DrawFocusRectangle(); GListBoxItem item = new GListBoxItem(); Rectangle bounds = e.Bounds; Size imageSize = _myImageList.ImageSize; try { item = (GListBoxItem)Items[e.Index]; if (item.ImageIndex != -1) { ImageList.Draw(e.Graphics, bounds.Left, bounds.Top, item.ImageIndex); e.Graphics.DrawString(item.Text, e.Font, new SolidBrush(e.ForeColor), bounds.Left + imageSize.Width, bounds.Top); } else { e.Graphics.DrawString(item.Text, e.Font, new SolidBrush(e.ForeColor), bounds.Left, bounds.Top); } } catch { if (e.Index != -1) { e.Graphics.DrawString(Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), bounds.Left, bounds.Top); } else { e.Graphics.DrawString(Text, e.Font, new SolidBrush(e.ForeColor), bounds.Left, bounds.Top); } } base.OnDrawItem(e); } } |