private void DrawImage(Graphics g, Rectangle imageRect, Image image, ImageList imageList, int imageIndex, bool focus) { using (var brush = new SolidBrush(_Owner.BackColor)) { g.FillRectangle(brush, imageRect); } if (image == null) { return; } using (var graphics = new InterpolationModeGraphics(g, InterpolationMode.HighQualityBicubic)) { if (focus) { IntPtr hIcon = NativeMethods.ImageList_GetIcon(imageList.Handle, imageIndex, (int)NativeMethods.ImageListDrawFlags.ILD_SELECTED); g.DrawIcon(Icon.FromHandle(hIcon), imageRect); NativeMethods.DestroyIcon(hIcon); } else { g.DrawImage(image, imageRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel); } } }
protected override void OnRenderItemImage(ToolStripItemImageRenderEventArgs e) { ToolStrip toolStrip = e.ToolStrip; Graphics g = e.Graphics; if (toolStrip is ToolStripDropDown && e.Item is ToolStripMenuItem) { bool bDrawLogo = NeedDrawLogo(toolStrip); int offsetMargin = bDrawLogo ? OffsetMargin : 0; ToolStripMenuItem item = (ToolStripMenuItem)e.Item; if (item.Checked) { return; } Rectangle rect = e.ImageRectangle; if (e.Item.RightToLeft == RightToLeft.Yes) { rect.X -= offsetMargin + 2; } else { rect.X += offsetMargin + 2; } using (InterpolationModeGraphics ig = new InterpolationModeGraphics(g)) { ToolStripItemImageRenderEventArgs ne = new ToolStripItemImageRenderEventArgs(g, e.Item, e.Image, rect); base.OnRenderItemImage(ne); } } else { base.OnRenderItemImage(e); } }
protected override void OnDrawItem(DrawItemEventArgs e) { if (e.Index != -1) { ImageComboBoxItem item = Items[e.Index]; Graphics g = e.Graphics; Rectangle bounds = e.Bounds; int indentOffset = Indent * item.Level; if ((e.State & DrawItemState.ComboBoxEdit) == DrawItemState.ComboBoxEdit) { indentOffset = 0; } int imageWidth = bounds.Height; TextFormatFlags format = TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine | TextFormatFlags.WordBreak; Rectangle imageRect = new Rectangle(bounds.Left + indentOffset + 2, bounds.Top, imageWidth, imageWidth); Rectangle textRect = new Rectangle(imageRect.Right + 3, bounds.Y, bounds.Width - imageRect.Width - indentOffset - 5, bounds.Height); var backRect = new Rectangle(textRect.X, textRect.Y + 1, textRect.Width, textRect.Height - 2); backRect.Width = TextRenderer.MeasureText(item.Text, e.Font, textRect.Size, format).Width; if (base.RightToLeft == RightToLeft.Yes) { imageRect.X = bounds.Right - imageRect.Right; textRect.X = bounds.Right - textRect.Right; backRect.X = textRect.Right - backRect.Width; } bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected); Color backColor = selected ? SystemColors.Highlight : base.BackColor; using (Brush backBrush = new SolidBrush(backColor)) { g.FillRectangle(backBrush, backRect); } if (selected) { ControlPaint.DrawFocusRectangle(g, backRect); } Image image = item.Image; if (image != null) { using (var graphics = new InterpolationModeGraphics(g, InterpolationMode.HighQualityBicubic)) { if (selected) { IntPtr hIcon = NativeMethods.ImageList_GetIcon(ImageList.Handle, item.ImageIndexer.ActualIndex, (int)NativeMethods.ImageListDrawFlags.ILD_SELECTED); g.DrawIcon(Icon.FromHandle(hIcon), imageRect); NativeMethods.DestroyIcon(hIcon); } else { g.DrawImage(image, imageRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel); } } } TextRenderer.DrawText(g, item.Text, e.Font, textRect, base.ForeColor, format); } }