Draw an image over child controls

Draw an image over child controls

Old forum URL: forums.lhotka.net/forums/t/6441.aspx


Lalit posted on Tuesday, February 17, 2009

Hi, 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 m drawing the image on paint event. For that i overriden OnPaint method but its not working. Please suggest something to overcome this prob. M pasting the code below.

 

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

}

}

tmg4340 replied on Tuesday, February 17, 2009

This looks to me like you're in WinForms.  If that's not the case, then you can ignore this, because it's all different in WPF - and I can't yet speak intelligently about that.

(Of course, from what I do know about WPF, you shouldn't be having this problem there...)

The problem you're seeing is that the controls contained in your panel have their own window handle, and essentially "own" the space they occupy.  Nothing you do in the graphics arena can override that.  If you want to draw "on top" of your contained controls, you will have to create another control (something like a PictureBox will probably do), draw in that control, and set its ZIndex so that it sits on top of the other controls.

HTH

- Scott

Copyright (c) Marimer LLC