首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 笔记本电脑 | 北大青鸟 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Dotnet频道
中国IT教育
Google
首页 ASP.NET  C#  XML/WebService ADO.NET VC.NET VB.NET .NET 资讯动态 专题 RSS订阅 讨论 下载
您现在的位置: 中国IT实验室 >> Dotnet >> C# >> 正文

c#.net中创建带图标的ListBox

下面的代码实现了带图标的ListBoox的功能,可以直接拷贝即可运行。运行结果如下:

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.<a class="channel_keylink" href="http://windows.chinaitlab.com/" target="_blank">Windows</a>.Forms; using System.Data; namespace ListBoxWithIcon { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.<a class="channel_keylink" href="http://windows.chinaitlab.com/" target="_blank">Windows</a>.Forms.Form { private GListBox lb; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { ImageList imageList = new ImageList(); imageList.Images.Add(Image.FromFile(@"D:\我的文件\myrice\favicon.ico")); imageList.Images.Add(Image.FromFile(@"D:\我的文件\myrice\favicon4.ico")); imageList.Images.Add(Image.FromFile(@"D:\我的文件\myrice\favicon5.ico")); this.lb = new GListBox();; this.SuspendLayout(); // // lb // this.lb.ItemHeight = 16; this.lb.Location = new System.Drawing.Point(10, 10); this.lb.Name = "listBox1"; this.lb.Size = new System.Drawing.Size(200, 60); this.lb.TabIndex = 0; lb.ImageList = imageList; lb.Items.Add( new GListBoxItem("http://xml.sz.luohuedu.net/",0)); lb.Items.Add( new GListBoxItem("http://lucky.myrice.com/",1)); lb.Items.Add( new GListBoxItem("【孟宪会之精彩世界】",2)); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(292, 160); this.Controls.Add(this.lb); this.Text = "带图标的ListBox<a class="channel_keylink" href="http://softtest.chinaitlab.com" target="_blank">测试</a>"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { } // GListBoxItem 类 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; } } // GListBox 类 public class GListBox : ListBox { private ImageList _myImageList; 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; 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); } } } }
【责编:Peng】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·开源软件测试工具学习专题
 ·JSP Web开发 入门基础到高手进阶教程…
 ·JavaFX—是Java桌面的新希望么?
 ·安全至上 .NET开发安全策略…
 ·测试用例设计之道-测试用例学习专题
 ·面向Java开发人员的Scala指南
 ·Java设计模式之实例详解
 ·Oracle数据库11g 面向DBA和开发人员的重要新特性…
 ·桌面应用软件编程 J2SE技术详解…
 ·我“炫”我精彩-------WPF开发教程
 今日更新
 社区讨论
 博客论点
 频道精选
 Dotnet频道相关导航