这是我在列表框中绘制和着色项目的类。功能是ColorListBox
。如果我使用 8 号字体,它看起来还可以,但如果我使用 20 号字体,则 listBox 中的项目相互重叠;他们之间没有空间。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace GatherLinks
{
class ColorText
{
public static void Texts(RichTextBox box, string text, Color color)
{
box.SelectionStart = box.TextLength;
box.SelectionLength = 0;
box.SelectionColor = color;
box.AppendText(text);
box.SelectionColor = box.ForeColor;
}
public static void ColorListBox(List<string> data, DrawItemEventArgs e)
{
string strLeft = null;
string strMid = "---";
string strRight = null;
if (data[e.Index].Contains(strMid))
{
int index = data[e.Index].IndexOf(strMid);
strLeft = data[e.Index].Substring(0, index);
strRight = data[e.Index].Substring(index + strMid.Length);
}
using (Font f = new Font(FontFamily.GenericSansSerif, 20, FontStyle.Regular))
{
float startPos;
e.Graphics.DrawString(strLeft, f, Brushes.Red, e.Bounds.X, e.Bounds.Y);
startPos = e.Graphics.MeasureString(strLeft, f).Width;
e.Graphics.DrawString(strMid, f, Brushes.Black, e.Bounds.X + startPos, e.Bounds.Y);
startPos = e.Graphics.MeasureString(strLeft + strMid, f).Width;
e.Graphics.DrawString(strRight, f, Brushes.Green, e.Bounds.X + startPos, e.Bounds.Y);
}
}
}
}
这是尺寸为 20 时的外观图像: