0

我有这个类,我用它来为 Form1 的 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)
        {
            int keywords = 0;
            string keyword = null;
            string url = data[e.Index].Substring(0, 5);
            if (data[e.Index].Contains("Local KeyWord:"))
            {
                keywords = data[e.Index].IndexOf("Local KeyWord:");
                keyword = data[e.Index].Substring(keywords, 14);
            }
            else
            {
                keywords = data[e.Index].IndexOf("Localy KeyWord:");
                keyword = data[e.Index].Substring(keywords, 15);
            }

            using (Font f = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular))
            {
                if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                    e.Graphics.FillRectangle(Brushes.LightBlue, e.Bounds);
                else
                {
                    using (SolidBrush sb = new SolidBrush(SystemColors.Window))
                        e.Graphics.FillRectangle(sb, e.Bounds);
                }

                SizeF size = e.Graphics.MeasureString(url, f);
                using (SolidBrush sb = new SolidBrush(Color.Red))
                {
                    e.Graphics.DrawString(url, f, sb, new PointF(e.Bounds.X, e.Bounds.Y));
                    string toMeasure = data[e.Index].Substring(0, keywords - 1);
                    float startPos = e.Graphics.MeasureString(toMeasure, f).Width;
                    e.Graphics.DrawString(keyword, f, sb, new PointF(e.Bounds.X + (int)startPos, e.Bounds.Y));
                }
                // string token = data[e.Index].Substring(url.Length, data[e.Index].LastIndexOf(" --- ") - (url.Length));
                // e.Graphics.DrawString(token, f, Brushes.Black, new PointF(e.Bounds.X + size.Width, e.Bounds.Y));
                // Get the string to print in black.
                int first = url.Length;
                int last = data[e.Index].LastIndexOf(" --- ") + 5; //(5 = length of " --- ").
                int length = last - first;
                string token = data[e.Index].Substring(first, length);

                // Get the place to draw it.
                size = e.Graphics.MeasureString(url, f);
                float positionX = size.Width;

                // Draw the string.
                e.Graphics.DrawString(token, f, Brushes.Black, new PointF(positionX, e.Bounds.Y));

                //size = e.Graphics.MeasureString(url + token, f);
                //size = e.Graphics.MeasureString("Url:" + token, f);
                //e.Graphics.DrawString(data[e.Index].Substring(data[e.Index].IndexOf(token) + token.Length, data[e.Index].LastIndexOf(": ") - token.Length - data[e.Index].IndexOf(token) + 1), f, Brushes.Black, new PointF(size.Width, e.Bounds.Y));

                token = data[e.Index].Substring(data[e.Index].LastIndexOf(": ") + 2);
                size = e.Graphics.MeasureString(data[e.Index].Substring(0, data[e.Index].LastIndexOf(token)), f);
                using (SolidBrush sb = new SolidBrush(Color.Green))
                    e.Graphics.DrawString(token, f, sb, new PointF(e.Bounds.X + size.Width + 4, e.Bounds.Y));

                e.DrawFocusRectangle();
          }
        }
    }
}

问题是在函数 ColorListBox 中它正在寻找特定的字符串/项目:“Local KeyWord”

但在 Form1 中,listBox 可能包含任何我的意思是它可能包含我希望此函数适用于 listBox 中的任何字符串的任何字符串。

例如,现在在 Form1 中我做了:

                data.Add("System Fan Speed - ");
                listBox1.DataSource = data;

我想要做的是右侧的文本,例如 Gpu 温度的名称将是红色,然后字符串“-”将是黑色,然后它自身的温度数字将是绿色。我希望在这个函数中,我将能够为我在列表框项目中选择的每个部分设置颜色。

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index == -1)
            {
            }
            else
            {
                string url = data[e.Index].Substring(0, 5);

                using (Font f = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular))
                {
                    ColorText.ColorListBox(data, e);
                }
            }
        }
4

0 回答 0