C# Winform编程ListBox之添加图标
来源:互联网 发布:java多线程抢票 编辑:程序博客网 时间:2024/06/06 08:00
先上图:
这里添加固定的图片的。
代码:
- private Color RowBackColorAlt=Color.FromArgb(200,200,200);//交替色
- private Color RowBackColorSel = Color.FromArgb(150, 200, 250);//选择项目颜色
- public Form1()
- {
- InitializeComponent();
- listBox1.DrawMode = DrawMode.OwnerDrawFixed;
- listBox1.ItemHeight = 24;
- }
- private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
- {
- Brush myBrush = Brushes.Black;
- if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
- {
- myBrush = new SolidBrush(RowBackColorSel);
- }
- else if (e.Index % 2 == 0)
- {
- myBrush = new SolidBrush(RowBackColorAlt);
- }
- else
- {
- myBrush = new SolidBrush(Color.White);
- }
- e.Graphics.FillRectangle(myBrush, e.Bounds);
- e.DrawFocusRectangle();//焦点框
- //绘制图标
- Image image = Image.FromFile("images/item.png");
- Graphics g = e.Graphics;
- Rectangle bounds = e.Bounds;
- Rectangle imageRect = new Rectangle(
- bounds.X,
- bounds.Y,
- bounds.Height,
- bounds.Height);
- Rectangle textRect = new Rectangle(
- imageRect.Right,
- bounds.Y,
- bounds.Width - imageRect.Right,
- bounds.Height);
- if (image != null)
- {
- g.DrawImage(
- image,
- imageRect,
- 0,
- 0,
- image.Width,
- image.Height,
- GraphicsUnit.Pixel);
- }
- //文本
- StringFormat strFormat = new StringFormat();
- //strFormat.Alignment = StringAlignment.Center;
- strFormat.LineAlignment = StringAlignment.Center;
- e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), textRect,strFormat);
- }
- C# Winform编程ListBox之添加图标
- C# Winform编程ListBox之添加图标
- C#之winform基础 为listbox添加右键菜单
- C#之winform基础 listbox控件可以多选
- WinForm中的ListBox组件编程
- C#之Winform编程01
- [c# WinForm]往ListView中添加大量的不同图标
- 在C#中实现listbox的项添加与移除(winform)
- C#(WinForm) ComboBox和ListBox添加项
- C#之WinForm基础 添加菜单栏
- C# winform listBox中的项上下移动
- C# WinForm开发系列 - ListBox/ListView/Panel
- C# WinForm ListBox 绑定数据库代码
- C# WinForm开发系列 - ListBox/ListView/Panel
- C# WinForm开发系列 - ListBox/ListView/Panel
- C# WinForm开发系列 - ListBox/ListView/Panel
- C# WinForm开发系列 - ListBox/ListView/Panel
- C#之WinForm基础 未选择状态下的listbox的selectindex是-1
- makefile编写粗略(部分)解读
- 喜欢的句子
- 处女作
- 黑马程序员----网络编程
- 我的Android进阶之旅------>RGB颜色查询对照表
- C# Winform编程ListBox之添加图标
- oracle数据类型
- 3.3FactoryMethod——工厂方法
- c# 控制winform程序只能打开一个
- android通知栏响应事件
- java第五章
- C++面试问题集锦
- 黑马程序员---JDK1.5新特性
- IOS关于XIB文件和调试时候显示不一样问题