例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. iot_programe Makefile hacking

    /***************************************************************************** * iot_programe Makefi ...

  2. I.MX6 修改调试串口号(ttymx0 -> ttymxc2)

    I.MX6 修改调试串口号(ttymx0 -> ttymxc2) 一.参考文章: uboot修改默认调试串口ttymxc0 ->ttymxc4(imx53) http://www.xueb ...

  3. centos7最小化安装后配置笔记

    一.安装wget(步骤2备用) yum install wget -y 二.切换yum源为阿里云 备份旧源: mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum ...

  4. 初次使用git遇到的问题总结

    第一次使用git时,遇到好多问题,但也都是小问题,下边我把这些问题总结一下. 问题一: 创建远程仓库的时候,如果你勾选了Initialize this repository with a README ...

  5. VS 编译太慢了吗?新建解决方案配置关闭一部分项目的编译

    手头的解决方案真大!里面的项目个数达到了 30 个或是 50 个?然而接近一半是单元测试项目和辅助工具.再加上一些不尽如人意的项目优化,编译速度真的是无力吐槽.幸好 Visual Studio 提供了 ...

  6. PAT 1006 换个格式输出 C语言

    让我们用字母B来表示“百”.字母S表示“十”,用“12...n”来表示个位数字n(<10),换个格式来输出任一个不超过3位的正整数.例如234应该被输出为BBSSS1234,因为它有2个“百”. ...

  7. yugabyte cloud native db 基本试用

    备注: 测试环境使用docker进行安装试用 1. 安装 a. Download mkdir ~/yugabyte && cd ~/yugabyte wget https://down ...

  8. 笔记:配置 webpack dev server

    笔记:配置 webpack dev server 安装 webpack-dev-server 组件 配置 webpack.config.js 配置 增加 html-webpack-plugin 组件 ...

  9. PostgreSQL编译安装

    PostgreSQL编译安装 安装语言包 ### PostgreSQL 初始化过程中,会读取操作系统字符编码, ### 若程序需要使用zh_CN.utf-8字符编码,需要在PostgreSQL 初始化 ...

  10. 基于Oracle的EntityFramework的WEBAPI2的实现(四)——自动生成在线帮助文档

    打开我们项目中的Area文件夹,正常情况下,我们会发现已经有了一个名字叫做[HelpPage]的区域(Area),这个区域是vs帮助我们自动建立的,它是一个mvc(不是webapi),它有普通的Con ...