好久没有给大家东西了.看见了这个.感觉不错.ListBox with icon
点击次数:24 次 发布日期:2008-11-26 23:57:30 作者:源代码网
|
源代码网推荐 Hello all, 源代码网推荐 All of us like more color or image in our control, so am i. 源代码网推荐 This article, I give custom ListBox class that has image property for each item. 源代码网推荐 Note: my article has no source code because it very short and easy. 源代码网推荐 The first: we create 2 class for GListBox 源代码网推荐 ... 源代码网推荐 // GListBoxItem class 源代码网推荐 public class GListBoxItem 源代码网推荐 { 源代码网推荐 private string _myText; 源代码网推荐 private int _myImageIndex; 源代码网推荐 // properties 源代码网推荐 public string Text 源代码网推荐 { 源代码网推荐 get {return _myText;} 源代码网推荐 set {_myText = value;} 源代码网推荐 } 源代码网推荐 public int ImageIndex 源代码网推荐 { 源代码网推荐 get {return _myImageIndex;} 源代码网推荐 set {_myImageIndex = value;} 源代码网推荐 } 源代码网推荐 //constructor 源代码网推荐 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; 源代码网推荐 } 源代码网推荐 }//End of GListBoxItem class 源代码网推荐 源代码网推荐 源代码网推荐 // GListBox class 源代码网推荐 public class GListBox : ListBox 源代码网推荐 { 源代码网推荐 private ImageList _myImageList; 源代码网推荐 public ImageList ImageList 源代码网推荐 { 源代码网推荐 get {return _myImageList;} 源代码网推荐 set {_myImageList = value;} 源代码网推荐 } 源代码网推荐 public GListBox() 源代码网推荐 { 源代码网推荐 // Set owner draw mode 源代码网推荐 this.DrawMode = DrawMode.OwnerDrawFixed; 源代码网推荐 } 源代码网推荐 protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e) 源代码网推荐 { 源代码网推荐 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,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); 源代码网推荐 } 源代码网推荐 }//End of GListBox class 源代码网推荐 源代码网推荐 After that, in order to our code, we could use : 源代码网推荐 ... 源代码网推荐 GListBox lb = new GListBox(); 源代码网推荐 lb.ImageList = imageList; 源代码网推荐 lb.Items.Add( new GListBoxItem("Image 1",0)); 源代码网推荐 lb.Items.Add( new GListBoxItem("Image 2",1)); 源代码网推荐 lb.Items.Add( new GListBoxItem("Image 3",2)); 源代码网推荐 .... 源代码网推荐 That"s all 源代码网推荐 Thanks for reading. 源代码网推荐 Nguyen Ha Giang 源代码网推荐 mailto: giang@citd.edu.vn 源代码网供稿. |
