Lalit
Lalit

Reputation: 337

Draw image on top of child controls

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);
    }
}

Upvotes: 1

Views: 3826

Answers (2)

jgallant
jgallant

Reputation: 11273

The best way to do this, is to use the BringToFront() method on the image.

It will draw the image on top of all the controls. However, this will not work unless the controls are all located on the same parent control.

Upvotes: 0

casperOne
casperOne

Reputation: 74530

The Paint event (or an override of the OnPaint method) 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 OnPaint method, and then call the base implementation. Then draw your image, and it will be drawn on top of everything else.

Upvotes: 1

Related Questions