/// <summary> /// Draws the specified item on the given graphics. /// </summary> /// <param name="g">The System.Drawing.Graphics to draw on.</param> /// <param name="item">The ImageListViewItem to draw.</param> /// <param name="state">The current view state of item.</param> /// <param name="bounds">The bounding rectangle of item in client coordinates.</param> public override void DrawItem(Graphics g, ImageListViewItem item, ItemState state, Rectangle bounds) { if (ImageListView.View == View.Details) { bool alternate = (item.Index % 2 == 1); List<ImageListView.ImageListViewColumnHeader> uicolumns = ImageListView.Columns.GetDisplayedColumns(); // Paint background if ((state & ItemState.Disabled) != ItemState.None) { // Disabled using (Brush bItemBack = new LinearGradientBrush(bounds, ImageListView.Colors.DisabledColor1, ImageListView.Colors.DisabledColor2, LinearGradientMode.Vertical)) { g.FillRectangle(bItemBack, bounds); } } else if (ImageListView.Focused && ((state & ItemState.Selected) != ItemState.None)) { // Focused and selected using (Brush bItemBack = new LinearGradientBrush(bounds, ImageListView.Colors.SelectedColor1, ImageListView.Colors.SelectedColor2, LinearGradientMode.Vertical)) { g.FillRectangle(bItemBack, bounds); } } else if (!ImageListView.Focused && ((state & ItemState.Selected) != ItemState.None)) { // Not focused and selected using (Brush bItemBack = new LinearGradientBrush(bounds, ImageListView.Colors.UnFocusedColor1, ImageListView.Colors.UnFocusedColor2, LinearGradientMode.Vertical)) { g.FillRectangle(bItemBack, bounds); } } else { // Not selected using (Brush bItemBack = new SolidBrush(alternate ? ImageListView.Colors.AlternateBackColor : ImageListView.Colors.BackColor)) { g.FillRectangle(bItemBack, bounds); } // Shade sort column int x = bounds.Left - 1; foreach (ImageListView.ImageListViewColumnHeader column in uicolumns) { if (ImageListView.SortOrder != SortOrder.None && ImageListView.SortColumn >= 0 && ImageListView.SortColumn < ImageListView.Columns.Count && ImageListView.Columns[ImageListView.SortColumn].Guid == column.Guid) { Rectangle subItemBounds = bounds; subItemBounds.X = x; subItemBounds.Width = column.Width; using (Brush bSort = new SolidBrush(ImageListView.Colors.ColumnSelectColor)) { g.FillRectangle(bSort, subItemBounds); } break; } x += column.Width; } } // Separators int xs = bounds.Left - 1; foreach (ImageListView.ImageListViewColumnHeader column in uicolumns) { xs += column.Width; if (!ReferenceEquals(column, uicolumns[uicolumns.Count - 1])) { using (Pen pSep = new Pen(ImageListView.Colors.ColumnSeparatorColor)) { g.DrawLine(pSep, xs, bounds.Top, xs, bounds.Bottom); } } } // Sub items Color foreColor = ImageListView.Colors.CellForeColor; if ((state & ItemState.Disabled) != ItemState.None) foreColor = ImageListView.Colors.DisabledForeColor; else if (ImageListView.Focused && (state & ItemState.Selected) != ItemState.None) foreColor = ImageListView.Colors.SelectedForeColor; else if (!ImageListView.Focused && (state & ItemState.Selected) != ItemState.None) foreColor = ImageListView.Colors.UnFocusedForeColor; else if (alternate) foreColor = ImageListView.Colors.AlternateCellForeColor; int offset = 2; int firstWidth = 0; if (uicolumns.Count > 0) firstWidth = uicolumns[0].Width; Rectangle rt = new Rectangle(bounds.Left + offset, bounds.Top, firstWidth - 2 * offset, bounds.Height); foreach (ImageListView.ImageListViewColumnHeader column in uicolumns) { rt.Width = column.Width - 2 * offset; int iconOffset = 0; if (column.Type == ColumnType.Name) { // Allocate space for checkbox and file icon if (ImageListView.ShowCheckBoxes && ImageListView.ShowFileIcons) iconOffset += 2 * 16 + 3 * 2; else if (ImageListView.ShowCheckBoxes) iconOffset += 16 + 2 * 2; else if (ImageListView.ShowFileIcons) iconOffset += 16 + 2 * 2; } rt.X += iconOffset; rt.Width -= iconOffset; // Rating stars if (column.Type == ColumnType.Rating && ImageListView.RatingImage != null && ImageListView.EmptyRatingImage != null) { int rating = item.GetSimpleRating(); if (rating > 0) { int w = ImageListView.RatingImage.Width; int y = (int)(rt.Top + (rt.Height - ImageListView.RatingImage.Height) / 2.0f); for (int i = 1; i <= 5; i++) { if (rating >= i) g.DrawImage(ImageListView.RatingImage, rt.Left + (i - 1) * w, y); else g.DrawImage(ImageListView.EmptyRatingImage, rt.Left + (i - 1) * w, y); } } } else if (column.Type == ColumnType.Custom) TextRenderer.DrawText(g, item.GetSubItemText(column.Guid), ImageListView.Font, rt, foreColor, TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter | TextFormatFlags.PreserveGraphicsClipping); else TextRenderer.DrawText(g, item.GetSubItemText(column.Type), ImageListView.Font, rt, foreColor, TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter | TextFormatFlags.PreserveGraphicsClipping); rt.X -= iconOffset; rt.X += column.Width; } // Focus rectangle if (ImageListView.Focused && ((state & ItemState.Focused) != ItemState.None)) ControlPaint.DrawFocusRectangle(g, bounds); } else // if (ImageListView.View != View.Details) { // Paint background if (ImageListView.Enabled) { using (Brush bItemBack = new SolidBrush(ImageListView.Colors.BackColor)) { g.FillRectangle(bItemBack, bounds); } } else { using (Brush bItemBack = new SolidBrush(ImageListView.Colors.DisabledBackColor)) { g.FillRectangle(bItemBack, bounds); } } // Get thumbnail Image img = item.GetCachedImage(CachedImageType.Thumbnail); // Reference text height int textHeight = ImageListView.Font.Height; // Calculate bounds Rectangle textBounds = new Rectangle(bounds.Left + 3, bounds.Bottom - (textHeight + 3), bounds.Width - 2 * 3, textHeight); Rectangle imgBounds; if (img != null) imgBounds = new Rectangle(bounds.Left + (bounds.Width - img.Width) / 2, bounds.Bottom - (img.Height + textHeight + 3 * 3), img.Width, img.Height); else imgBounds = new Rectangle(bounds.Left + 3, bounds.Top + 3, ImageListView.ThumbnailSize.Width, ImageListView.ThumbnailSize.Height); Rectangle textOutline = Rectangle.Inflate(textBounds, 3, 3); Rectangle imgOutline = Rectangle.Inflate(imgBounds, 3, 3); textOutline.Width -= 1; textOutline.Height -= 1; // Paint background if ((((state & ItemState.Disabled) != ItemState.None))) { // Disabled using (Brush bBack = new SolidBrush(ImageListView.Colors.DisabledColor1)) { Utility.FillRoundedRectangle(g, bBack, textOutline, 4); Utility.FillRoundedRectangle(g, bBack, imgOutline, 4); } } else if ((ImageListView.Focused && ((state & ItemState.Selected) != ItemState.None))) { // Focused and selected using (Brush bBack = new SolidBrush(ImageListView.Colors.SelectedColor1)) { Utility.FillRoundedRectangle(g, bBack, textOutline, 4); Utility.FillRoundedRectangle(g, bBack, imgOutline, 4); } } else if ((!ImageListView.Focused && ((state & ItemState.Selected) != ItemState.None))) { // Not focused and selected using (Brush bBack = new SolidBrush(ImageListView.Colors.UnFocusedColor1)) { Utility.FillRoundedRectangle(g, bBack, textOutline, 4); Utility.FillRoundedRectangle(g, bBack, imgOutline, 4); } } // Draw image if (img != null) { g.DrawImage(img, imgBounds.Location); } // Image border using (Pen pBorder = new Pen(ImageListView.Colors.BorderColor)) { Utility.DrawRoundedRectangle(g, pBorder, imgOutline.Left, imgOutline.Top, imgOutline.Width - 1, imgOutline.Height - 1, 3); } // Hovered state if ((state & ItemState.Hovered) != ItemState.None) { using (Brush bGlow = new SolidBrush(Color.FromArgb(24, Color.White))) { Utility.FillRoundedRectangle(g, bGlow, imgOutline, 4); } } // Item text Color foreColor = ImageListView.Colors.ForeColor; if ((state & ItemState.Disabled) != ItemState.None) foreColor = ImageListView.Colors.DisabledForeColor; else if (ImageListView.Focused && (state & ItemState.Selected) != ItemState.None) foreColor = ImageListView.Colors.SelectedForeColor; else if (!ImageListView.Focused && (state & ItemState.Selected) != ItemState.None) foreColor = ImageListView.Colors.UnFocusedForeColor; TextRenderer.DrawText(g, item.Text, ImageListView.Font, textBounds, foreColor, TextFormatFlags.EndEllipsis | TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.PreserveGraphicsClipping); // Focus rectangle if (ImageListView.Focused && ((state & ItemState.Focused) != ItemState.None)) { textOutline.Offset(1, 1); textOutline.Width -= 1; textOutline.Height -= 1; ControlPaint.DrawFocusRectangle(g, textOutline); } } }
/// <summary> /// Draws the specified item on the given graphics. /// </summary> /// <param name="g">The System.Drawing.Graphics to draw on.</param> /// <param name="item">The ImageListViewItem to draw.</param> /// <param name="state">The current view state of item.</param> /// <param name="bounds">The bounding rectangle of item in client coordinates.</param> public override void DrawItem(Graphics g, ImageListViewItem item, ItemState state, Rectangle bounds) { VisualStyleRenderer rBack; if (!ImageListView.Enabled) rBack = rItemSelectedHidden; if (((state & ItemState.Disabled) != ItemState.None)) rBack = rItemDisabled; else if (!ImageListView.Focused && ((state & ItemState.Selected) != ItemState.None)) rBack = rItemSelectedHidden; else if (((state & ItemState.Selected) != ItemState.None) && ((state & ItemState.Hovered) != ItemState.None)) rBack = rItemHoveredSelected; else if ((state & ItemState.Selected) != ItemState.None) rBack = rItemSelected; else if ((state & ItemState.Hovered) != ItemState.None) rBack = rItemHovered; else rBack = rItemNormal; if (VisualStylesEnabled && rBack != null) { // Do not draw the background of normal items if (((state & ItemState.Hovered) != ItemState.None) || ((state & ItemState.Selected) != ItemState.None)) rBack.DrawBackground(g, bounds, bounds); Size itemPadding = new Size(7, 7); // Draw the image if (ImageListView.View != View.Details) { Image img = item.GetCachedImage(CachedImageType.Thumbnail); if (img != null) { Rectangle pos = Utility.GetSizedImageBounds(img, new Rectangle(bounds.Location + itemPadding, ImageListView.ThumbnailSize)); // Image background Rectangle imgback = pos; imgback.Inflate(3, 3); g.FillRectangle(SystemBrushes.Window, imgback); // Image border if (img.Width > 32 && img.Height > 32) { using (Pen pen = new Pen(Color.FromArgb(224, 224, 244))) { g.DrawRectangle(pen, imgback.X, imgback.Y, imgback.Width - 1, imgback.Height - 1); } } // Image g.DrawImage(img, pos); } // Draw item text Color foreColor = SystemColors.ControlText; if ((state & ItemState.Disabled) != ItemState.None) foreColor = SystemColors.GrayText; Size szt = TextRenderer.MeasureText(item.Text, ImageListView.Font); Rectangle rt = new Rectangle( bounds.Left + itemPadding.Width, bounds.Top + 2 * itemPadding.Height + ImageListView.ThumbnailSize.Height, ImageListView.ThumbnailSize.Width, szt.Height); TextRenderer.DrawText(g, item.Text, ImageListView.Font, rt, foreColor, TextFormatFlags.EndEllipsis | TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine | TextFormatFlags.PreserveGraphicsClipping); } else // if (ImageListView.View == View.Details) { List<ImageListView.ImageListViewColumnHeader> uicolumns = ImageListView.Columns.GetDisplayedColumns(); // Separators int x = bounds.Left - 2; foreach (ImageListView.ImageListViewColumnHeader column in uicolumns) { x += column.Width; if (!ReferenceEquals(column, uicolumns[uicolumns.Count - 1])) { using (Pen pGray32 = new Pen(Color.FromArgb(32, 128, 128, 128))) { g.DrawLine(pGray32, x, bounds.Top, x, bounds.Bottom); } } } Size offset = new Size(2, (bounds.Height - ImageListView.Font.Height) / 2); // Sub text int firstWidth = 0; if (uicolumns.Count > 0) firstWidth = uicolumns[0].Width; Rectangle rt = new Rectangle(bounds.Left + offset.Width, bounds.Top + offset.Height, firstWidth - 2 * offset.Width, bounds.Height - 2 * offset.Height); Color foreColor = SystemColors.ControlText; if ((state & ItemState.Disabled) != ItemState.None) foreColor = SystemColors.GrayText; foreach (ImageListView.ImageListViewColumnHeader column in uicolumns) { rt.Width = column.Width - 2 * offset.Width; using (Brush bItemFore = new SolidBrush(SystemColors.ControlText)) { int iconOffset = 0; if (column.Type == ColumnType.Name) { // Allocate space for checkbox and file icon if (ImageListView.ShowCheckBoxes && ImageListView.ShowFileIcons) iconOffset += 2 * 16 + 3 * 2; else if (ImageListView.ShowCheckBoxes) iconOffset += 16 + 2 * 2; else if (ImageListView.ShowFileIcons) iconOffset += 16 + 2 * 2; } rt.X += iconOffset; rt.Width -= iconOffset; // Rating stars if (column.Type == ColumnType.Rating && ImageListView.RatingImage != null && ImageListView.EmptyRatingImage != null) { int rating = item.GetSimpleRating(); if (rating > 0) { int w = ImageListView.RatingImage.Width; int y = (int)(rt.Top + (rt.Height - ImageListView.RatingImage.Height) / 2.0f); for (int i = 1; i <= 5; i++) { if (rating >= i) g.DrawImage(ImageListView.RatingImage, rt.Left + (i - 1) * w, y); else g.DrawImage(ImageListView.EmptyRatingImage, rt.Left + (i - 1) * w, y); } } } else if (column.Type == ColumnType.Custom) TextRenderer.DrawText(g, item.GetSubItemText(column.Guid), ImageListView.Font, rt, foreColor, TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine | TextFormatFlags.PreserveGraphicsClipping); else TextRenderer.DrawText(g, item.GetSubItemText(column.Type), ImageListView.Font, rt, foreColor, TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine | TextFormatFlags.PreserveGraphicsClipping); rt.X -= iconOffset; } rt.X += column.Width; } } // Focus rectangle if (ImageListView.Focused && ((state & ItemState.Focused) != ItemState.None)) { Rectangle focusBounds = bounds; focusBounds.Inflate(-2, -2); ControlPaint.DrawFocusRectangle(g, focusBounds); } } else base.DrawItem(g, item, state, bounds); }