在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 故事背景: 需要检索某目录下文件,并列出来,提供选择和其他功能. 第一版需求: 列出文件供选择即可,代码如下 ...
随机推荐
- docker下的Jenkins安装和体验【转】
原文地址:http://blog.csdn.net/boling_cavalry/article/details/78942408 作为一款优秀的持续集成工具,jenkins在日常的项目中经常会用到, ...
- quick2.2.6 问题记录
1.luasocket 不能使用方式 用下面地址的文件替换文件重新编译 https://github.com/chukong/quick-cocos2d-x/blob/master/lib/cocos ...
- daemon Thread
1.概念 守护进程(daemon)是指在UNIX或其他多任务操作系统中在后台执行的电脑程序,并不会接受电脑用户的直接操控.此类程序会被以进程的形式初始化.守护进程程序的名称通常以字母“d”结尾:例如, ...
- bzoj 4852 炸弹攻击
bzoj 4852 炸弹攻击 二维平面上的最优解问题,模拟退火是一个较为优秀的近似算法. 此题确定圆心后,便可 \(O(m)\) 算出收益,且最优解附近显然也较优,是连续变化的,可以直接模拟退火. 小 ...
- 《DSP using MATLAB》示例Example 8.30
%% ------------------------------------------------------------------------ %% Output Info about thi ...
- python调用rpc实现分布式系统
rpc 一般俗称,远程过程调用,把本地的函数,放到远端去调用. 通常我们调用一个方法,譬如: sumadd(10, 20),sumadd方法的具体实现要么是用户自己定义,要么存在于该语言的库函数中,也 ...
- openresty 使用 log_by_lua 发送日志到 syslog-ng
1. 安装 opm get p0pr0ck5/lua-resty-logger-socket 2. 使用 location lua_by_lua_block log_by_lu ...
- Discuz! X2.5RC 全新安装图文教程
http://www.discuz.net/thread-2660015-1-1.html 一步步教你利用Discuz X2.5建设论坛视频教程(174集) http://down.51cto.com ...
- oralce之 10046对Hash Join分析
前两天解决了一个优化SQL的case,SQL语句如下,big_table为150G大小,small_table很小,9000多条记录,不到1M大小,hash_area_size, sort_area_ ...
- Spark Streaming性能调优
数据接收并行度调优(一) 通过网络接收数据时(比如Kafka.Flume),会将数据反序列化,并存储在Spark的内存中.如果数据接收称为系统的瓶颈,那么可以考虑并行化数据接收.每一个输入DStrea ...