带图标和多行显示的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 编辑窗口 设置固定选项卡 使窗口选项卡多行显示
工具>选项> 确定后 如图就可以多行显示了.
随机推荐
- 彻底透析SpringBoot jar可执行原理
文章篇幅较长,但是包含了SpringBoot 可执行jar包从头到尾的原理,请读者耐心观看.同时文章是基于SpringBoot-2.1.3进行分析.涉及的知识点主要包括Maven的生命周期以及自定 ...
- 小白教程 Java web maven项目Windows下部署阿里云服务器全过程(买服务器,打包项目,连接服务器,配置服务器,部署项目)
-------------首先:购买服务器---------------- Step1: 在https://www.aliyun.com/?utm_content=se_1043015注册登录阿里云 ...
- Thread API的详细介绍
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.concurr ...
- 如何使用百度EasyDL进行情感分析
使用百度EasyDL定制化训练和服务平台有一段时间了,越来越能体会到EasyDL的易用性.在此之前我也接触过不少的深度学习平台,如类脑平台.Google的GCP深度学习平台.AWS深度学习平台,但我觉 ...
- jdk安装错误1316,jdk-10.0.1
打开注册表regedit ,定个位到 计算机\HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\ 把jdk,jre下面的目录删掉.即10.0.1 ,问题解决 参考: https ...
- Excel催化剂开源第23波-VSTO开发辅助录入功能关键技术
Excel催化剂开源第23波-VSTO开发辅助录入功能关键技术 Excel催化剂 2019.01.12 14:10* 字数 2948 阅读 41评论 0喜欢 0 编辑文章 在Excel催化剂的几大 ...
- C#3.0新增功能10 表达式树 01 简介
连载目录 [已更新最新开发文章,点击查看详细] 如果你使用过 LINQ,则会有丰富库(其中 Func 类型是 API 集的一部分)的经验. (如果尚不熟悉 LINQ,建议阅读 LINQ 教程,以 ...
- [PTA] 数据结构与算法题目集 6-12 二叉搜索树的操作集
唯一比较需要思考的删除操作: 被删除节点有三种情况: 1.叶节点,直接删除 2.只有一个子节点,将子节点替换为该节点,删除该节点. 3.有两个子节点,从右分支中找到最小节点,将其值赋给被删除节点的位置 ...
- 安装解压版MySQL5.76及以上版本 出现服务正在启动-服务无法启动的问题
最近重装了系统,去MySQL官网下载了最新的MySQL5.7.9,我选择的是解压版,安装之后启动服务的时候,提示服务无法启动,在网上找了很多教程,弄了很久都没有弄好,后来还是决定去英文官网找找答案, ...
- mysqli_query 的定义和用法
定义和用法 mysqli_query() 函数执行某个针对数据库的查询. 语法 mysqli_query(connection,query,resultmode); 参数 描述 connecti ...