void HistoryListView_DrawItem(object sender, DrawListViewItemEventArgs e) { if (e.ItemIndex < 0) { return; } if (this.View == View.Tile) { // draw the background and focus rectangle for selected and non-selected states if (this.SelectedIndices.Contains(e.ItemIndex)) { e.Graphics.FillRectangle(System.Drawing.Brushes.LightGray, e.Bounds); ControlPaint.DrawFocusRectangle(e.Graphics, e.Bounds); } else { e.DrawBackground(); } // draw icon PastNotification pn = (PastNotification)e.Item.Tag; int newX = e.Bounds.Left; if (pn != null) { System.Drawing.Image img = PastNotificationManager.GetImage(pn); using (img) { if (img != null) { int x = e.Bounds.Left + 1; int y = e.Bounds.Top + 1; e.Graphics.DrawImage(img, x, y); newX = e.Bounds.Left + img.Width + this.Margin.Right; } } } // draw main text System.Drawing.RectangleF rect = new System.Drawing.RectangleF(newX, e.Bounds.Top, e.Bounds.Right - newX, e.Item.Font.Height); System.Drawing.StringFormat sf = new System.Drawing.StringFormat(); sf.Trimming = System.Drawing.StringTrimming.EllipsisCharacter; sf.FormatFlags = System.Drawing.StringFormatFlags.NoClip; System.Drawing.SolidBrush foreBrush = new System.Drawing.SolidBrush(e.Item.ForeColor); string text = e.Item.Text.Replace("\r", " - "); using (foreBrush) { e.Graphics.DrawString(text, e.Item.Font, foreBrush, rect, sf); } // draw subitems System.Drawing.Color subColor = System.Drawing.Color.FromArgb(System.Drawing.SystemColors.GrayText.ToArgb()); System.Drawing.SolidBrush subBrush = new System.Drawing.SolidBrush(subColor); using (subBrush) { for (int i = 1; i < this.Columns.Count; i++) { if (i < e.Item.SubItems.Count) { text = e.Item.SubItems[i].Text.Replace("\r", " - "); rect.Offset(0, e.Item.Font.Height); e.Graphics.DrawString(text, e.Item.Font, subBrush, rect, sf); } } } } else { // DO NOT call e.DrawDefault or the DrawSubItem event will not be fired //e.DrawDefault = true; } }
void HistoryListView_DrawSubItem(object sender, DrawListViewSubItemEventArgs e) { if (e.ItemIndex < 0) { return; } //Console.WriteLine(e.ItemIndex + "-" + e.Bounds); if (this.SelectedIndices.Contains(e.ItemIndex)) { e.Graphics.FillRectangle(System.Drawing.Brushes.LightGray, e.Bounds); ControlPaint.DrawFocusRectangle(e.Graphics, e.Item.Bounds); } else { e.DrawBackground(); } // if this is the first column, we want to draw an icon as well int newX = e.Bounds.Left; if (e.ColumnIndex == 0) { // draw the icon newX = newX + 20; PastNotification pn = (PastNotification)e.Item.Tag; if (pn != null) { System.Drawing.Image img = PastNotificationManager.GetImage(pn); using (img) { if (img != null) { int x = e.Bounds.Left + 2; int y = e.Bounds.Top; e.Graphics.DrawImage(img, new System.Drawing.Rectangle(x, y, 16, 16)); } } } } // draw text string text = e.SubItem.Text.Replace("\r", " - "); System.Drawing.Rectangle rect = new System.Drawing.Rectangle(newX, e.Bounds.Top, e.Bounds.Right - newX, e.Item.Font.Height); System.Drawing.StringFormat sf = new System.Drawing.StringFormat(); sf.Trimming = System.Drawing.StringTrimming.EllipsisCharacter; sf.FormatFlags = System.Drawing.StringFormatFlags.NoClip | System.Drawing.StringFormatFlags.NoWrap; System.Drawing.Color color = (e.ColumnIndex == 0 ? e.Item.ForeColor : System.Drawing.Color.FromArgb(System.Drawing.SystemColors.GrayText.ToArgb())); System.Drawing.SolidBrush foreBrush = new System.Drawing.SolidBrush(color); using (foreBrush) { //TextFormatFlags flags = TextFormatFlags.Default | TextFormatFlags.ExternalLeading | TextFormatFlags.GlyphOverhangPadding | TextFormatFlags.NoClipping | TextFormatFlags.EndEllipsis | TextFormatFlags.LeftAndRightPadding | TextFormatFlags.SingleLine; //TextRenderer.DrawText(e.Graphics, text, e.SubItem.Font, rect, e.SubItem.ForeColor, System.Drawing.Color.Transparent, flags); e.Graphics.DrawString(text, e.SubItem.Font, foreBrush, rect, sf); } }