tags:

views:

469

answers:

3

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. Please suggest something to overcome this prob.


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);
    }
}
+1  A: 

The paint event 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 over everything else.

casperOne
Hi casperOnethanks for a quick answer as i am waiting eagerly. I had tried that thing bt failed. Its not working anything. :(
Lalit
@Lalit: If it failed, you should show your code that is showing the failure. Otherwise, we can't help you figure out where the problem is.
casperOne
Hi,M posting the code as another answer as comment length is restricting me. It will be a gr8 help to get it fixed. :)
Lalit
@Lalit: When you step through this code in the debugger, is it throwing an exception?
casperOne
Hi Casper,Its not throwing any exception.
Lalit
Actually i found the prob. As i added a table ayout panel in my panel and docked it to fill the panel, without any padding, OnPaint is not firing of parent Panel. I can not add any control in my parent panel just to show an image as it is a generic control and should not restrict developer.
Lalit
from developing screen as (s)he wishes.
Lalit
@Lalit: Why not make the parent control a custom user control with a panel in that, and then override the OnPaint method of the user control and not the panel?
casperOne
A: 

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.

Jon
Hi Jon,Actually m drawing a polygon and a rectangle on a panel. I think i can not bring it to front. Please rectify me if m wrong n tell me the way.
Lalit
Sorry, I was thinking of a Picturebox.
Jon
A: 
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);
    }
}
Lalit