例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);
}
}

效果如下:

参考文章

顺德早茶在C#中控制ListBox某一行的字体颜色

C#中动态修改ListBox的Item的颜色的方法

gyzsky, listbox隔行显示不同颜色

例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某一行的字体颜色的更多相关文章

  1. 【转】iOS中设置导航栏标题的字体颜色和大小

    原文网址:http://www.360doc.com/content/15/0417/11/20919452_463847404.shtml iOS中设置导航栏标题的字体颜色和大小,有需要的朋友可以参 ...

  2. iOS中设置导航栏标题的字体颜色和大小

    iOS中设置导航栏标题的字体颜色和大小,有需要的朋友可以参考下. 在平时开发项目的时候,难免会遇到修改导航栏字体大小和颜色的需求,一般使用自定义视图的方法,其实还存在一种方法. 方法一:(自定义视图的 ...

  3. MFC列表控件更改一行的字体颜色

    参考自(http://blog.csdn.net/ribut9225/article/details/6720639) 1.首先从CListCtrl 继承一个类,命名为CListCtrlCl 在头文件 ...

  4. vs 中怎么用c改变部分字体颜色

    // test.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <windows.h> #include< ...

  5. Ext.grid.panel 改变某一行的字体颜色

    grid.getStore().addListener('load', handleGridLoadEvent); function handleGridLoadEvent(store, record ...

  6. <td style="word-break:break-all"> 在html中控制自动换行

    在html中控制自动换行   其实只要在表格控制中添加一句 <td style="word-break:break-all">就搞定了. 其中可能对英文换行可能会分开一 ...

  7. 深度分析如何在Hadoop中控制Map的数量

    深度分析如何在Hadoop中控制Map的数量 guibin.beijing@gmail.com 很多文档中描述,Mapper的数量在默认情况下不可直接控制干预,因为Mapper的数量由输入的大小和个数 ...

  8. CSS控制文字显示一行,超出显示省略号

    这几天在项目需求里面遇到了很多之前没做过的需求,也慢慢更加认识到了css的强大,是真的强大.以后会把自己技术调研的东西都写出来,哪怕只是一点点或者很小的点,重在学习. “CSS控制文字显示一行,超出显 ...

  9. WPF 显示文件列表中使用 ListBox 变到ListView 最后使用DataGrid

    WPF 显示文件列表中使用 ListBox 变到ListView 最后使用DataGrid 故事背景: 需要检索某目录下文件,并列出来,提供选择和其他功能. 第一版需求: 列出文件供选择即可,代码如下 ...

随机推荐

  1. >=< 在set和dict中的不同

    两个dict比较的算法是,长度>键>值,由于dict无序,所以比较的时候会自动将键对齐比较,我们不用担心这个. >>> d1 = dict(x=1, y=2) >& ...

  2. 奇异值分解(SVD)详解

    在网上看到有很多文章介绍SVD的,讲的也都不错,但是感觉还是有需要补充的,特别是关于矩阵和映射之间的对应关系.前段时间看了国外的一篇文章,叫A Singularly Valuable Decompos ...

  3. Codeforces 311B Cats Transport【斜率优化DP】

    LINK 题目大意 有一些猫,放在一些位置,人一步移动一个位置 给出每个猫出现的时间,每个人可以自由安排其出发时间,沿途已经出现的猫捡起,猫等待的时间是被减去的时间减去出现的时间 猫可以等人,人不能等 ...

  4. Appium+python(1)简单的介绍环境搭建

    环境搭建其实并不难,只不过安装的东西有点多,要加的环境变量有点多. 链接:https://pan.baidu.com/s/1nwLhNIT 密码:56wn 这个压缩包里要用的都有了,只需要下载,然后安 ...

  5. python第三方库

    autopy autopy是一个自动化操作的python库,可以模拟一些鼠标.键盘事件,还能对屏幕进行访问 pywin32 win32api的python封装 PIL python的图形图像处理框架

  6. UVA11137 Ingenuous Cubrency

    题意 PDF 分析 考虑dp. 用\(d(i,j)\)表示用不超过i的立方凑成j的方案数. \(d(i,j)=d(i-1,j)+d(i,j-i^3)\) 时间复杂度\(O(IN+T)\) 代码 #in ...

  7. [MEF]第05篇 MEF的目录(Catalog)筛选

    一.演示概述本示例演示如何使用MEF提供的目录(Catalog)的扩展机制实现可过滤导出部件的自定义目录类.主要是通过继承ComposablePartCatalog基类,并实现接口INotifyCom ...

  8. piwik docker 安装

    备注: 生产环境使用docker-compose  1. 安装docker && docker-compose    此处略过 2. 下载docker-compose  的文件   h ...

  9. as3 阻止后续侦听器

    public class Test1 extends Sprite { private var spr:Sprite; private var spr2:Sprite; public function ...

  10. REST API权限集成设计

    REST API权限集成设计 应用分为两大部分,前端html+后端Rest服务,前端html和后端Rest服务部署完全分离. 目标:可访问资源都处于权限控制之下(意味着通过浏览器地址栏的任意url都会 ...