簡體   English   中英

列表視圖中的多色子項目-有可能嗎?

[英]multicolor subitem in listview - is it possible?

問題直截了當:是否可以在每個leter具有不同顏色的子項中插入字符串?

我想用顏色來表示時間延遲。 示例:子項目字符串“ 10 14 50”,值10和50帶有紅色,值14帶有綠色。

嘗試將OwnerDraw模式設置為true並自己提供繪圖例程:

listView1.OwnerDraw = true;
listView1.DrawColumnHeader += listView1_DrawColumnHeader;
listView1.DrawSubItem += listView1_DrawSubItem;

void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e) {
  e.DrawDefault = true;
}

void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e) {
  if (e.ColumnIndex == 1) {
    e.Graphics.SetClip(e.Bounds);
    using (SolidBrush br = new SolidBrush(listView1.BackColor)) {
      e.Graphics.FillRectangle(br, e.Bounds);
    }
    int textLeft = e.Bounds.Left;
    string[] subItems = e.Item.SubItems[1].Text.Split(' ');
    for (int i = 0; i < subItems.Length; ++i) {
      int textWidth = TextRenderer.MeasureText(subItems[i], listView1.Font).Width;
      TextRenderer.DrawText(e.Graphics, subItems[i], listView1.Font,
          new Rectangle(textLeft, e.Bounds.Top, textWidth, e.Bounds.Height),
          i == 0 ? Color.Red : i == subItems.Length - 1 ? Color.Green : Color.Black, 
          Color.Empty,
          TextFormatFlags.VerticalCenter | TextFormatFlags.PreserveGraphicsClipping);
      textLeft += textWidth;
    }
    e.Graphics.ResetClip();
  } else {
    e.DrawDefault = true;
  }
}

結果:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM