2014-12-04 56 views
0

我有這樣的方法:如何在listBox中的特定情況下爲特定項目的文本着色?

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

我想以紅色顏色的文本:文件已經存在,並沒有被覆蓋:

我在設計器中將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中的文字?文件已存在且未被覆蓋: 因此,只有在出現的方法中出現該文件時纔會將其着色。

+0

它是如何_Not working_?如果只有一個項目應該是不同的顏色,你將需要知道哪一個;你可以保留一個inices列表或設置標籤。如果只有一部分文本應該是紅色的,你將需要解析這些部分並對每個部分使用DrawString調用。 - 你可以使用e中的參數來知道您正在繪製哪個項目,但代碼必須適用於所有項目!對於正常的項目,你可以簡單地調用'e.DrawDefault();' – TaW 2014-12-04 10:03:34

回答

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()導致您的問題。

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.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一點...

但項目或ItemIndices或設置標籤的列表可以根據自己的喜好..

+0

TaW e沒有Item屬性,並且DrawDefault沒有e.Item而不是e.DrawDefault – 2014-12-04 12:36:49

+0

哎呀,你是對的;我正在編寫一個'ListView',這個btw幾乎總是更好的選擇。對不起,看到了最後的答案! – TaW 2014-12-04 14:52:14