在C#中控制ListBox某一行的字体颜色
例1
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Add("红色");
listBox1.Items.Add("黄色");
listBox1.Items.Add("蓝色");
listBox1.DrawMode = DrawMode.OwnerDrawFixed; // 属性里设置
} private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
Color vColor = e.ForeColor;
switch (e.Index)
{
case : vColor = Color.Red; break;
case : vColor = Color.Yellow; break;
case : vColor = Color.Blue; break;
}
e.Graphics.FillRectangle(new SolidBrush(vColor), e.Bounds);
e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(), e.Font,
new SolidBrush(e.ForeColor), e.Bounds);
e.DrawFocusRectangle();
}
例2 一种动态渲染颜色的方式
根据字符串的前缀,分别对文字颜色进行渲染。
// ListBox DrawItem事件响应函数
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index >= )
{
e.DrawBackground();
Brush mybsh = Brushes.Black;
// 判断是什么类型的标签
if (listBox1.Items[e.Index].ToString().IndexOf("你好") != -)
{
mybsh = Brushes.Green;
}
else if (listBox1.Items[e.Index].ToString().IndexOf("你坏") != -)
{
mybsh = Brushes.Red;
}
// 焦点框
e.DrawFocusRectangle();
//文本
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, mybsh, e.Bounds, StringFormat.GenericDefault);
}
}
效果如下,当输入“你好”并按添加按钮的时候相应的ListBox的内容变为的绿色,输入“你坏”的时候变为了红色,达到了我们的要求目的:
例3 隔行显示不同的颜色
要实现这个效果很简单,只需自定义一个类继承ListBox,然后重写OnDrawItem事件就可以了,下面看代码
class CListbox:ListBox
{ [System.Runtime.InteropServices.DllImport("user32.dll")]
static extern IntPtr GetWindowDC(IntPtr hWnd);
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); protected override void OnDrawItem(DrawItemEventArgs e)
{ Graphics g = e.Graphics; if (e.Index % == )
{ g.FillRectangle(new SolidBrush(Color.WhiteSmoke), e.Bounds);
g.DrawString(Items[e.Index].ToString(), e.Font, new SolidBrush(Color.Blue), e.Bounds); }
else
{ g.FillRectangle(new SolidBrush(Color.WhiteSmoke), e.Bounds);
g.DrawString(Items[e.Index].ToString(), e.Font, new SolidBrush(Color.Red), e.Bounds); }
e.DrawFocusRectangle();
g.Dispose(); base.OnDrawItem(e);
} //捕获消息,画listbox边框
protected override void WndProc(ref Message m)
{
if (m.Msg == 0xf || m.Msg == 0x133)
{
IntPtr hDC = GetWindowDC(m.HWnd);
System.Drawing.Pen pen = new Pen(Color.YellowGreen, );
System.Drawing.Graphics g = Graphics.FromHdc(hDC);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.DrawRectangle(pen, , , this.Width - , this.Height - );
pen.Dispose();
g.Dispose();
ReleaseDC(m.HWnd, hDC);
}
base.WndProc(ref m);
}
} // 应用如下:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private CListbox mylistbox; private void Form1_Load(object sender, EventArgs e)
{
mylistbox = new CListbox();
mylistbox.ItemHeight = ;
mylistbox.DrawMode = DrawMode.OwnerDrawVariable;
mylistbox.Height = ;
mylistbox.Width = ;
mylistbox.Items.Add("第一行显示!");
mylistbox.Items.Add("第二行显示!");
mylistbox.Items.Add("第三行显示!");
mylistbox.Items.Add("第四行显示!");
mylistbox.Items.Add("第五行显示!");
mylistbox.Items.Add("第六行显示!");
mylistbox.Items.Add("第七行显示!");
mylistbox.Items.Add("第八行显示!");
mylistbox.Items.Add("第九行显示!");
mylistbox.Items.Add("第十行显示!");
mylistbox.Items.Add("第十一行显示!");
mylistbox.Items.Add("第十二行显示!");
this.Controls.Add(mylistbox);
}
}
效果如下:
参考文章
例2 一种动态渲染颜色的方式
Normal
0
7.8 磅
0
2
false
false
false
EN-US
ZH-CN
X-NONE
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri",sans-serif;
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-font-kerning:1.0pt;}
在C#中控制ListBox某一行的字体颜色的更多相关文章
- 【转】iOS中设置导航栏标题的字体颜色和大小
原文网址:http://www.360doc.com/content/15/0417/11/20919452_463847404.shtml iOS中设置导航栏标题的字体颜色和大小,有需要的朋友可以参 ...
- iOS中设置导航栏标题的字体颜色和大小
iOS中设置导航栏标题的字体颜色和大小,有需要的朋友可以参考下. 在平时开发项目的时候,难免会遇到修改导航栏字体大小和颜色的需求,一般使用自定义视图的方法,其实还存在一种方法. 方法一:(自定义视图的 ...
- MFC列表控件更改一行的字体颜色
参考自(http://blog.csdn.net/ribut9225/article/details/6720639) 1.首先从CListCtrl 继承一个类,命名为CListCtrlCl 在头文件 ...
- vs 中怎么用c改变部分字体颜色
// test.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <windows.h> #include< ...
- Ext.grid.panel 改变某一行的字体颜色
grid.getStore().addListener('load', handleGridLoadEvent); function handleGridLoadEvent(store, record ...
- <td style="word-break:break-all"> 在html中控制自动换行
在html中控制自动换行 其实只要在表格控制中添加一句 <td style="word-break:break-all">就搞定了. 其中可能对英文换行可能会分开一 ...
- 深度分析如何在Hadoop中控制Map的数量
深度分析如何在Hadoop中控制Map的数量 guibin.beijing@gmail.com 很多文档中描述,Mapper的数量在默认情况下不可直接控制干预,因为Mapper的数量由输入的大小和个数 ...
- CSS控制文字显示一行,超出显示省略号
这几天在项目需求里面遇到了很多之前没做过的需求,也慢慢更加认识到了css的强大,是真的强大.以后会把自己技术调研的东西都写出来,哪怕只是一点点或者很小的点,重在学习. “CSS控制文字显示一行,超出显 ...
- WPF 显示文件列表中使用 ListBox 变到ListView 最后使用DataGrid
WPF 显示文件列表中使用 ListBox 变到ListView 最后使用DataGrid 故事背景: 需要检索某目录下文件,并列出来,提供选择和其他功能. 第一版需求: 列出文件供选择即可,代码如下 ...
随机推荐
- c语言输出4*5的数列?
1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 输出上面的数列,用c实现的代码:<pre lang="c" line=&quo ...
- enum枚举类型的定义
enum枚举类型的定义方式与某种用法 #include <iostream> using namespace std; int main() { enum TOT{ zero, one, ...
- hihocoder1618 单词接龙
#1618 : 单词接龙 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个单词字典和一个起始字母.小Hi需要从起始字母开始,每次再加上一个字母,生成长度为2.3. ...
- LOJ2609. NOIP2013 火柴排队 【树状数组】
LOJ2609. NOIP2013 火柴排队 LINK 题目大意: 给你两个数列,定义权值∑i=1(ai−bi)^2 问最少的操作次数,最小化权值 首先需要发现几个性质 最小权值满足任意i,j不存在a ...
- HDU2825 Wireless Password 【AC自动机】【状压DP】
HDU2825 Wireless Password Problem Description Liyuan lives in a old apartment. One day, he suddenly ...
- BZOJ4543 [POI2014]Hotel加强版
题意 有一个树形结构,每条边的长度相同,任意两个节点可以相互到达.选3个点.两两距离相等.有多少种方案? 数据范围:n<=100000 分析 参照小蒟蒻yyb的博客. 我们先考虑一个\(O(n^ ...
- LG4848 崂山白花蛇草水
题意 神犇 Aleph 在 SDOI Round2 前立了一个 flag:如果进了省队,就现场直播喝崂山白花蛇草水.凭借着神犇 Aleph 的实力,他轻松地进了山东省省队,现在便是他履行诺言的时候了. ...
- c++ 中 毫秒级时间获取
#include <time.h> clock_t start,ends; start=clock(); Sleep(); ends=clock(); cout<<ends-s ...
- MySQL 性能优化技巧
原文地址:MySQL 性能优化技巧 博客地址:http://www.extlight.com 一.背景 最近公司项目添加新功能,上线后发现有些功能的列表查询时间很久.原因是新功能用到旧功能的接口,而这 ...
- Go编译安装
go编译安装: 设置环境变量 # sudo vim /etc/profile.d/go export GOROOT=/home/evescn/go export PATH=$PATH:$GOROOT/ ...