带图标和多行显示的ListBox
源码https://www.codeproject.com/Articles/15464/Extending-the-ListBox-to-show-more-complex-items
定义控件
using System.Drawing;
using System.Windows.Forms; namespace testexListBox
{
internal class exListBoxItem
{
public exListBoxItem(int id, string title, string details, Image image)
{
Id = id;
Title = title;
Details = details;
ItemImage = image;
} public int Id { get; set; } public string Title { get; set; } public string Details { get; set; } public Image ItemImage { get; set; } public void drawItem(DrawItemEventArgs e, Padding margin,
Font titleFont, Font detailsFont, StringFormat aligment,
Size imageSize)
{
// if selected, mark the background differently
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
e.Graphics.FillRectangle(Brushes.CornflowerBlue, e.Bounds);
}
else
{
e.Graphics.FillRectangle(Brushes.Beige, e.Bounds);
} // draw some item separator
e.Graphics.DrawLine(Pens.DarkGray, e.Bounds.X, e.Bounds.Y, e.Bounds.X + e.Bounds.Width, e.Bounds.Y); // draw item image
e.Graphics.DrawImage(ItemImage, e.Bounds.X + margin.Left, e.Bounds.Y + margin.Top, imageSize.Width,
imageSize.Height); // calculate bounds for title text drawing
var titleBounds = new Rectangle(e.Bounds.X + margin.Horizontal + imageSize.Width,
e.Bounds.Y + margin.Top,
e.Bounds.Width - margin.Right - imageSize.Width - margin.Horizontal,
(int) titleFont.GetHeight() + ); // calculate bounds for details text drawing
var detailBounds = new Rectangle(e.Bounds.X + margin.Horizontal + imageSize.Width,
e.Bounds.Y + (int) titleFont.GetHeight() + + margin.Vertical + margin.Top,
e.Bounds.Width - margin.Right - imageSize.Width - margin.Horizontal,
e.Bounds.Height - margin.Bottom - (int) titleFont.GetHeight() - -
margin.Vertical - margin.Top); // draw the text within the bounds
e.Graphics.DrawString(Title, titleFont, Brushes.Black, titleBounds, aligment);
e.Graphics.DrawString(Details, detailsFont, Brushes.DarkGray, detailBounds, aligment); // put some focus rectangle
e.DrawFocusRectangle();
}
} public partial class exListBox : ListBox
{
private readonly Font _detailsFont;
private readonly StringFormat _fmt;
private readonly Size _imageSize;
private readonly Font _titleFont; public exListBox(Font titleFont, Font detailsFont, Size imageSize,
StringAlignment aligment, StringAlignment lineAligment)
{
_titleFont = titleFont;
_detailsFont = detailsFont;
_imageSize = imageSize;
ItemHeight = _imageSize.Height + Margin.Vertical;
_fmt = new StringFormat();
_fmt.Alignment = aligment;
_fmt.LineAlignment = lineAligment;
_titleFont = titleFont;
_detailsFont = detailsFont;
} public exListBox()
{
InitializeComponent();
_imageSize = new Size(, );
ItemHeight = _imageSize.Height + Margin.Vertical;
_fmt = new StringFormat();
_fmt.Alignment = StringAlignment.Near;
_fmt.LineAlignment = StringAlignment.Near;
_titleFont = new Font(Font, FontStyle.Bold);
_detailsFont = new Font(Font, FontStyle.Regular);
} protected override void OnDrawItem(DrawItemEventArgs e)
{
// prevent from error Visual Designer
if (Items.Count > )
{
var item = (exListBoxItem) Items[e.Index];
item.drawItem(e, Margin, _titleFont, _detailsFont, _fmt, _imageSize);
}
} protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
}
}
DEMO
using System;
using System.Drawing;
using System.Windows.Forms; namespace testexListBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
Image image1 = Image.FromFile(@"image1.jpg");
Image image2 = Image.FromFile(@"image2.jpg");
Image image3 = Image.FromFile(@"image3.jpg");
// exListBoxItem = int id, string title, string details, Image image
exListBox1.Items.Add(new exListBoxItem(, "John", "ICQ 56465464",image1));
exListBox1.Items.Add(new exListBoxItem(, "Bill","ICQ 56465464", image2));
exListBox1.Items.Add(new exListBoxItem(, "Peter", "ICQ 56465464",image3));
} private void exListBox1_DrawItem(object sender, DrawItemEventArgs e)
{
} private void exListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Text = ((exListBoxItem) exListBox1.SelectedItem).Id.ToString();
}
}
}

带图标和多行显示的ListBox的更多相关文章
- C# ListBox 每行显示颜色设置
使用ListBox时,每行显示不同的颜色 1.把AllowHtmlDraw属性设置为True 2.直接在Add或者AddRange里写标签 代码里用的是DEV插件里的ListBox,使用原生的也是一样 ...
- 【WPF】ListBox GridViewColumn Header 文字换行、文字多行显示
ListBox GridViewColumn Header 文字换行.文字多行显示,在Header中需要换行的地方写 <GridViewColumn Header="空间另存 为总量& ...
- 【传】玩转Android---UI篇---ImageButton(带图标的按钮)
原文网址:http://hualang.iteye.com/blog/964049 除了Android系统自带的Button按钮一万,还提供了带图标的按钮ImageButton 要制作带图标的按钮,首 ...
- CIconListBox带图标的列表框类
有时候,我们需要在列表框ListBox中插入带图标的文字项,这就需要自己派生一个类出来了,网上的一个CIconListBox类还不错,网站http://www.codeguru.com/Cpp/con ...
- Android实现“是否退出”对话框和“带图标的列表”对话框
今天我们学习的内容是实现两种对话框(Dialog),第一种是询问是否退出对话框,另外一种是带图标的列表对话框,程序的执行效果是,我们点击button1的时候,弹出第一种对话框,我们点击button2的 ...
- Android开源项目——带图标文字的底部导航栏IconTabPageIndicator
接下来的博客计划是,在<Android官方技术文档翻译>之间会发一些Android开源项目的介绍,直接剩下的几篇Android技术文档发完,然后就是Android开源项目和Gradle翻译 ...
- POI 导出excel带小数点的数字格式显示不对解决方法
最近看到了一个问题就是java导出excel中带小数点的数字显示不对, 比如我想在excel中第一行显示: 3,000.0 但是在excle中导出的格式总是不带小数点 3000(非文本格式),而且也 ...
- 【win】【qt5打包】【qt程序打包成一个可执行文件(带图标任何win都可以运行哦)】
[前言] 业务需求将qt程序打包成win可执行文件.咱是做linux的,奈何用的麒麟系统,程序运行在win,好嘛,重新在win qtcreator编译后打包呗. [目标] 1.给qt程序添加一个图标. ...
- visual studio 编辑窗口 设置固定选项卡 使窗口选项卡多行显示
工具>选项> 确定后 如图就可以多行显示了.
随机推荐
- 异常——cmd下javac错误:编码GBK不可映射字符
在看菜鸟教程时候用记事本创建文件,之后用notepad++编辑后,运行出现错误. 首先从信息上知道这是编码的问题了.开始试了下再notepad++上打开文件选择标签栏的“Encoding”中的“enc ...
- 数据结构与算法---堆排序(Heap sort)
堆排序基本介绍 1.堆排序是利用堆这种数据结构而设计的一种排序算法,堆排序是一种选择排序,它的最坏,最好,平均时间复杂度均为O(nlogn),它也是不稳定排序. 2.堆是具有以下性质的完全二叉树:每个 ...
- numpy表示图片详解
我自己的一个体会,在学习机器学习和深度学习的过程里,包括阅读模型源码的过程里,一个比较大的阻碍是对numpy掌握的不熟,有的时候对矩阵的维度,矩阵中每个元素值的含义晕乎乎的. 本文就以一个2 x 2 ...
- 通过代数,数字,欧几里得平面和分形讨论JavaScript中的函数式编程
本文是对函数式编程范式的系列文章从而拉开了与以下延续一个. 介绍 在JavaScript中,函数只是对象.因此,可以构造函数,作为参数传递,从函数返回或分配给变量.因此,JavaScript具有一流的 ...
- 个人永久性免费-Excel催化剂功能第18波-在Excel上也能玩上词云图
这年头数据可视化日新月异,在Excel上做数据分析,最后一步,难免要搞个图表输出高大上一回,微软也深知此道,在Excel2016上更新了一大波图表功能,市场上很耀眼的词云图还是没加进来,虽然在各大的在 ...
- 基站搭建与IMSI捕获
写在前面 : 实验目的是为了教学交流,坚决抵制违法行为. 一.实验目的 搭建基于OpenBTS的基站,手机接入该基站,进行短信.语音等测试. 二.所用仪器 USRP B210 1台,天线2根,PC机 ...
- 【CodeForces - 357B】Flag Day(水题)
Flag Day Descriptions 小G请你对 n 个点进行染色,可选的颜色有三种:白.红.蓝,并使得给定的 m 个三元组中,每个点的颜色各不相同. 因为你可能不会三分图匹配,于是小G给出了更 ...
- get 请求和post请求的具体区别
get请求是向服务器索要数据,post请求是向服务器传送数据 的,浏览器限制了get的传送量,post可以大量的把数据传给 服务器,一般情况下get请求,请求体是空的,请求只发一次 ,如果是post请 ...
- SQL server 2014使用导出数据为Excel
1.打开SQL server 2014,连接至数据库引擎 2.在要导出的数据库上右击,选择"任务->导出数据" 3.数据源选择"SQL Server Native ...
- Java网络和代理
Java网络和代理 1)简介 在当今的网络环境中,特别是企业网络环境中,应用程序开发人员必须像系统管理员一样频繁地处理代理.在某些情况下,应用程序应该使用系统默认设置,在其他情况下,我们希望能够非常严 ...