如何在C#WinForms中对Listbox / ListView行的颜色部分进行着色? [英] Way to color parts of the Listbox/ListView line in C# WinForms?

查看:824
本文介绍了如何在C#WinForms中对Listbox / ListView行的颜色部分进行着色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 是否有办法对ListBox项目的部分颜色(不只是整行)?
    例如,列表框项目由5个字组成,只有一个是彩色的或者是3个。

  2. 是否有办法对ListView做同样的事情? (我知道ListView可以为每列,但我想在一列中有多种颜色)。

我只对免费的解决方案感兴趣,并且希望他们不太重视实现或改变当前的使用方式彩色ListBox代替正常的一个更好)。

I am interested in only free solutions, and preferred that they are not heavy to implement or change current usage (the least effort to introduce colored ListBox in place of normal one the better).

关于

MadBoy

推荐答案

本文如何使用 DrawItem ListBox,其中DrawMode设置为OwnerDraw值之一。基本上,你做这样的事情:

This article tells how to use DrawItem of a ListBox with DrawMode set to one of the OwnerDraw values. Basically, you do something like this:

listBox1.DrawMode = OwnerDrawFixed;
listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem);

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
// TODO: Split listBox1.Items[e.Index].ToString() and then draw each separately in a different color
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold),new SolidBrush(color[e.Index]),e.Bounds);
}

而不是单个DrawString调用,Split listBox1.Items [e.Index] .ToString()转换为单词,并对每个单词单独调用DrawString。

Instead of the single DrawString call, Split listBox1.Items[e.Index].ToString() into words and make a separate call to DrawString for each word. You'll have to replace e.bounds with an x,y location or a bounding rectangle for each word.

同样的方法也适用于 ListView

这篇关于如何在C#WinForms中对Listbox / ListView行的颜色部分进行着色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆