如何在列表框中的特定情况下为特定项目的文本着色

本文关键字:项目 文本 情况下 列表 | 更新日期: 2023-09-27 18:33:14

我有这个方法:

private void batch_Resize(Image sourceImage,string oldfName, string sourceDirectory,string oldFileName)
        {
            Bitmap newImage = new Bitmap(512, 512);
            using (Graphics gr = Graphics.FromImage(newImage))
            {
                gr.SmoothingMode = SmoothingMode.AntiAlias;
                gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
                gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
                gr.DrawImage(sourceImage, new Rectangle(0, 0, newImage.Width, newImage.Height));
                i = i + 1;
                newImage.Save(@"d:'NewImages1'" + i.ToString("D6") + ".gif", System.Drawing.Imaging.ImageFormat.Gif);
                newImage.Save(@"d:'NewImages1'" + oldfName + ".gif", System.Drawing.Imaging.ImageFormat.Gif);
                string filesExit = sourceDirectory + "''"+ oldfName + ".gif";
                if (!File.Exists(filesExit))
                {
                    newImage.Save(sourceDirectory + "''" + oldfName + ".gif", System.Drawing.Imaging.ImageFormat.Gif);
                }
                else
                {
                    itemToColor = "File already exist and was not overwritten:";
                    listBox1.Invoke(new MethodInvoker(delegate { listBox1.Items.Add("File already exist and was not overwritten: " + oldfName); }));
                }
            }
            if (newImage != null)
                newImage.Dispose();
        }

我想将调用的项目涂成红色:

listBox1.Invoke(new MethodInvoker(delegate { listBox1.Items.Add("File already exist and was not overwritten: " + oldfName); }));

我想用红色颜色文本:文件已经存在并且没有被覆盖:

我在设计器中将列表框1绘制模式更改为OwnderDrawFixed并增加了抽奖项目事件。

在抽奖项目事件中,我做到了:

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            Graphics g = e.Graphics;
            g.FillRectangle(new SolidBrush(Color.Olive), e.Bounds);
            g.DrawString(itemToColor, e.Font, new SolidBrush(e.ForeColor), new PointF(e.Bounds.X, e.Bounds.Y));
        }

itemToColor 是 form1 中的全局字符串,我想为文本着色:

itemToColor = "File already exist and was not overwritten:";
listBox1.Invoke(new MethodInvoker(delegate { listBox1.Items.Add("File already exist and was not overwritten: " + oldfName); }));

但它不起作用。

如何仅将调用中的文本着色为红色?文件已存在且未被覆盖:因此,如果出现,它只会在方法中出现时为其着色。

如何在列表框中的特定情况下为特定项目的文本着色

如果您没有任何其他指标哪些项目应该为红色,这里是低技术方法:

string itemToColor = "File already exist and was not overwritten.";

使用列表视图(因为我误读了你),这将是代码:

private void listView1_DrawItem(object sender, DrawItemEventArgs e)
{
   if (e.Item.Text != itemToColor ) e.DrawDefault = true;
   else
   {
     e.DrawBackground();
     // e.Graphics.FillRectangle(new SolidBrush(Color.Olive), e.Bounds); // optional
     e.Graphics.DrawString(itemToColor, listView1.Font, 
                           Brushes.Red, new PointF(e.Bounds.X, e.Bounds.Y));
   }
}

注意:根据您的需要,您可能还需要编写DrawHeaderDrawSubItem事件!

由于您实际上使用的是旧的ListBox,因此此处与ListBox.DrawItem事件的代码相同:

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    SolidBrush brush = null;
    if (listBox1.Items[e.Index].ToString() != itemToColor )
         brush = new SolidBrush(e.ForeColor);
    else brush = new SolidBrush(Color.Red);
    e.DrawBackground();
    e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), 
                          e.Font, brush, new PointF(e.Bounds.X, e.Bounds.Y));
}

不确定你是否真的想用橄榄画背景。如果是这样,您可以轻松地稍微更改一下cvode。

但是项目列表或项目索引或设置标签可能更符合您的喜好。

这是一个工作示例代码

请记住将 DrawMode 更改为 OwnerDrawFixed 并处理 DrawItem 事件。

/// <summary>
/// Handles the DrawItem event of the listBox1 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Forms.DrawItemEventArgs"/> instance containing the event data.</param>
private void listBox1_DrawItem( object sender, DrawItemEventArgs e )
{
   e.DrawBackground();
   Graphics g = e.Graphics;
    // draw the background color you want
    // mine is set to olive, change it to whatever you want
    g.FillRectangle( new SolidBrush( Color.Olive), e.Bounds );
    // draw the text of the list item, not doing this will only show
    // the background color
    // you will need to get the text of item to display
    g.DrawString( THE_LIST_ITEM_TEXT , e.Font, new SolidBrush( e.ForeColor ), new PointF( e.Bounds.X, e.Bounds.Y) );
    e.DrawFocusRectangle();
}

可能缺少e.DrawFocusRectangle()导致了问题。