/// <summary> /// /// </summary> /// <param name="G"></param> /// <param name="Item"></param> /// <param name="Selected"></param> private void DrawImage(Graphics G, ListViewItem Item, bool Selected) { ImageList imageList = Item.ImageList; Size imageSize = imageList.ImageSize; Rectangle imageRect = Item.GetBounds(ItemBoundsPortion.Icon); if (imageRect.Width > imageSize.Width) { imageRect.X += (imageRect.Width - imageSize.Width) / 2; imageRect.Width = imageSize.Width; } if (imageRect.Height > imageSize.Height) { imageRect.Y += (imageRect.Height - imageSize.Height) / 2; imageRect.Height = imageSize.Height; } int imageIndex = Item.ImageIndex != -1 ? Item.ImageIndex : imageList.Images.IndexOfKey(Item.ImageKey); if (Selected) { IntPtr hIcon = ListViewNativeMethods.ImageList_GetIcon( imageList.Handle, imageIndex, (int)ListViewNativeMethods.ImageListDrawFlags.ILD_SELECTED); G.DrawIcon(Icon.FromHandle(hIcon), imageRect); ListViewNativeMethods.DestroyIcon(hIcon); } else { Image image = imageList.Images[imageIndex]; G.DrawImage( image, imageRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel); } }
/// <summary> /// /// </summary> /// <param name="e"></param> private void DrawSubItemDetails(DrawListViewSubItemEventArgs e) { Rectangle bounds = e.Bounds; ListViewItemStates itemState = e.ItemState; Graphics g = e.Graphics; ListViewItem item = e.Item; bool bSelected = (itemState & ListViewItemStates.Selected) != 0; bool bDrawImage = false; bool bFistItem = false; int imageIndex = -1; if (e.ColumnIndex == 0) { bFistItem = true; if (item.ImageList != null) { if (item.ImageIndex != -1) { imageIndex = item.ImageIndex; } else if (!string.IsNullOrEmpty(item.ImageKey)) { imageIndex = item.ImageList.Images.IndexOfKey(item.ImageKey); } if (imageIndex != -1) { bDrawImage = true; } } } Rectangle backRect = bounds; Rectangle imageRect = Rectangle.Empty; if (bDrawImage) { imageRect = item.GetBounds(ItemBoundsPortion.Icon); backRect = item.GetBounds(ItemBoundsPortion.Label); backRect.X += 2; backRect.Width -= 2; } if (bSelected && (base.FullRowSelect || (!base.FullRowSelect && bFistItem))) { backRect.Height--; Color baseColor = this._SelectedColor; Color borderColor = this._SelectedColor; Color innerBorderColor = Color.FromArgb(150, 255, 255, 255); RenderBackgroundInternal( g, backRect, baseColor, borderColor, innerBorderColor, 0.45f, false, LinearGradientMode.Vertical); if (!base.FullRowSelect && bFistItem) { backRect.Width--; using (Pen pen = new Pen(borderColor)) { g.DrawRectangle(pen, backRect); } backRect.Width++; } else { Point[] points = new Point[4]; points[0] = new Point(backRect.X, backRect.Y); points[1] = new Point(backRect.Right, backRect.Y); points[2] = new Point(backRect.Right, backRect.Bottom); points[3] = new Point(backRect.X, backRect.Bottom); using (Pen pen = new Pen(borderColor)) { if (bFistItem) { g.DrawLine(pen, points[0], points[1]); g.DrawLine(pen, points[0], points[3]); g.DrawLine(pen, points[2], points[3]); } if (e.ColumnIndex == ColumnCount - 1) { points[1].X--; points[2].X--; g.DrawLine(pen, points[0], points[1]); g.DrawLine(pen, points[1], points[2]); g.DrawLine(pen, points[2], points[3]); } else { g.DrawLine(pen, points[0], points[1]); g.DrawLine(pen, points[2], points[3]); } } } } else { Color backColor = e.ItemIndex % 2 == 0 ? this._RowBackColor1 : this._RowBackColor2; using (SolidBrush brush = new SolidBrush(backColor)) { g.FillRectangle(brush, backRect); } } TextFormatFlags flags = GetFormatFlags(e.Header.TextAlign); if (bDrawImage) { g.InterpolationMode = InterpolationMode.HighQualityBicubic; if (bSelected) { IntPtr hIcon = ListViewNativeMethods.ImageList_GetIcon( item.ImageList.Handle, imageIndex, (int)ListViewNativeMethods.ImageListDrawFlags.ILD_SELECTED); g.DrawIcon(Icon.FromHandle(hIcon), imageRect); ListViewNativeMethods.DestroyIcon(hIcon); } else { Image image = item.ImageList.Images[imageIndex]; g.DrawImage( image, imageRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel); } Rectangle textRect = new Rectangle( imageRect.Right + 3, bounds.Y, bounds.Width - imageRect.Right - 3, bounds.Height); TextRenderer.DrawText( g, item.Text, item.Font, textRect, item.ForeColor, flags); } else { bounds.X += 3; TextRenderer.DrawText( g, e.SubItem.Text, e.SubItem.Font, bounds, e.SubItem.ForeColor, flags); } }