I need to draw an image on a panel, containing some child controls. I dont want to let it hide behind those controls, which is the case right now when I’m drawing the image in the Paint event.
Here is the code I used to override the OnPaint method of my panel:
protected override void OnPaint(PaintEventArgs e) { try { base.OnPaint(e); //Draw icon for views at top right corner of the panel. e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; if (_viewButton != ViewButton.None) { Brush menuBrush = new SolidBrush(_viewButton == ViewButton.Highlighted ? Color.Black : Color.Gray); e.Graphics.FillPolygon(menuBrush, new Point[] { new Point(this.Width - 29, 03), new Point(this.Width - 19, 03), new Point(this.Width - 24, 09) }); menuBrush.Dispose(); //Draw border of the panel. Rectangle borderRect = new Rectangle(0, 0, this.Width - 1, this.Height - 1); GraphicsPath borderPath = GetRoundPath(borderRect, 8); e.Graphics.DrawPath(new Pen(Color.LightGray), borderPath); } } catch (Exception ex) { throw new IDSException(ex); } }
protected override void OnPaint(PaintEventArgs e) { try { base.OnPaint(e); //Draw icon for views at top right corner of the panel. e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; if (_viewButton != ViewButton.None) { Brush menuBrush = new SolidBrush(_viewButton == ViewButton.Highlighted ? Color.Black : Color.Gray); e.Graphics.FillPolygon(menuBrush, new Point[] { new Point(this.Width - 29, 03), new Point(this.Width - 19, 03), new Point(this.Width - 24, 09) }); menuBrush.Dispose(); //Draw border of the panel. Rectangle borderRect = new Rectangle(0, 0, this.Width - 1, this.Height - 1); GraphicsPath borderPath = GetRoundPath(borderRect, 8); e.Graphics.DrawPath(new Pen(Color.LightGray), borderPath); } } catch (Exception ex) { throw new IDSException(ex); } }
The
Paintevent (or an override of theOnPaintmethod) is the wrong place to do this kind of thing, because you need to make sure that your painting routine is called last.Rather, if you have access to the parent control, override the
OnPaintmethod, and then call the base implementation. Then draw your image, and it will be drawn on top of everything else.