c#.net中创建带图标的ListBox
下面的代码实现了带图标的ListBoox的功能,可以直接拷贝即可运行。运行结果如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ListBoxWithIcon
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.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测试";
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);
}
}
}
}
c#.net中创建带图标的ListBox相关推荐
- CSS/HTML 5简洁带图标的input日期选择器
CSS/HTML 5简洁带图标的input日期选择器 说明 css代码 html代码 演示效果图 说明 演示效果分辨率为1980x1080分辨率效果 高度可定制修改 纯css代码控制 使用的input ...
- 在ArcEngine中创建带高程Z值的点和线图层
管线和其附属物的坐标数据都是带有Z值的 而且有些情况下,一个管段的两个端点的x,y值一模一样(垂直的管段) 这样的线,在直接生成shape图层的时候,就会产生问题,特别是 使用ArcSDE的C API ...
- excel瀑布图_在Excel中创建瀑布图
excel瀑布图 We have a very famous waterfall here in Canada, and it creates gorgeous photos, like this o ...
- 在Excel 2007中创建“迷你图”
Office 2010上线了,在Excel 2010中一个非常激动人心的功能便是"迷你图",实际上就是一种单元格中的图表,非常实用,无奈这种功能在早先的版本中没有. 虽然在家里已经 ...
- Leaflet中使用awesome-markers插件显示带图标的marker
场景 Vue+Leaflet实现加载OSM显示地图: Vue+Leaflet实现加载OSM显示地图_BADAO_LIUMANG_QIZHI的博客-CSDN博客 在上面的基础上,实现Marker上带图标 ...
- java导出的文件名带正斜杠_如何在名称中创建带正斜杠的注册表项
我需要创建以下注册表项 HKLM:\software\bmc software\control-m/agent 但由于在"agent"之前的正斜杠而出现问题 创建没有正斜杠的条目我 ...
- G++编译带图标的exe文件
在ico文件同目录下创建一个.rc后缀的文件 右键刚新建的rc文件 -> 编辑 输入 A ICON "你的图标文件名" (注意大小写) 使用 windres -i " ...
- Qt生成带图标的exe文件
1.新建test工程,将图标test.icon放到test/icons目录中. 2.选择菜单"文件"->"新建文件或项目",在弹出的对话框中选择" ...
- 分享自TERRY-V 《Qt Creator生成带图标的exe文件》
超级nice,试了一下,可以用 来源:http://blog.sina.com.cn/s/blog_74a7e56e01018koi.html 转载于:https://www.cnblogs.com/ ...
最新文章
- 《转载》Java异常处理的10个最佳实践
- 目标检测分割--BlitzNet: A Real-Time Deep Network for Scene Understanding
- Java 25 岁,发展现状大曝光!程序员的机会来了
- Amcharts绑定新数据后不会刷新图表的解决办法
- Android获取网速的方法
- bootstrap.yml和application.yml的区别
- css3动画事件—webkitAnimationEnd
- opencv 多线程加速_线程池给你写好了,想加速拿来用就行哈
- go语言调度器源代码情景分析之五:汇编指令
- 由树的定义和递归想到的
- 微信小程序/js上传图片值腾讯云cos
- 数据结构常见面试题,一网打尽!
- 区分惯性环节与延迟环节
- 【PTA-python】第5章-1 输出星期名缩写 (70 分)
- vue学习笔记二:HBuilder X框架搭建
- 比较全的机油教程(android)
- android 下 ftp 客户端软件编写(ftp4j)
- Kaldi简介【开源语音识别工具】
- linux内核开启otg,OTG驱动分析(一)
- 网页无法在微信内访问怎么办,微信跳转链接的实现方法
热门文章
- Android系统匿名共享内存(Anonymous Shared Memory)C++调用接口分析
- 水彩在网页设计中应用的15个优秀案例
- 试hp服务器不识别网卡一则
- 容器编排技术 -- Kubernetes 应用连接到 Service
- Hibernate命名查询示例 - @NamedQuery
- 数据库面试 - 分库分表之后,id 主键如何处理?
- 分布式事物框架TCC-Transaction使用教程
- yarn : 无法加载文件 E:\leading\NodeJs\info\node_global\yarn.ps1,因为在此系统上禁止运行脚本
- 【网站】Bing每日壁纸API分享
- web表单设计:点石成金_如何设计安全的Web表单:验证,清理和控制