如何在listBox的特定情况下为特定项目的文本着色?

时间:2014-12-04 09:38:51

标签: c# .net winforms

我有这个方法:

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();
        }

我想在红色中调出Invoke:

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

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

我在设计器中将listbox1绘制模式更改为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); }));

但它不起作用。

如何只用红色调色Invoke中的文字?文件已存在且未被覆盖: 因此,只有当它出现在方法中时才会着色。

2 个答案:

答案 0 :(得分:1)

如果您没有任何其他指示项目应该是红色的,那么这是一种低技术方式:

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

使用ListView(因为我误解了你),这就是代码:

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));
}

不确定你是否真的想在Olive中绘制背景..如果是这样你可以轻松地改变cvode ..

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

答案 1 :(得分:0)

这是一个工作示例代码

请记住将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()会导致问题。