Sign in to follow this  
paic

[.net] Painting something on a form

Recommended Posts

paic    645
Hi, Here is my problem : I have a form with a various number of controls / usercontrols. And I'd like to draw an ellipse over one of those controls. So, I overrided the OnPaint method of my form :
protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    Rectangle rectangle = new Rectangle(10, 10, 6, 6);
    Pen pen = new Pen(Color.FromArgb(100, 100, 100), 2.0f);
    e.Graphics.DrawEllipse(pen, rectangle);
}

The problem is that the ellipse is drawn under the control ... and I have absolutely no idea why : I called the base.OnPaint method before drawing the ellipse, so the ellipse should be drawn on top, right ? Any help / explanation is welcomed :) Thx in advance.

Share this post


Link to post
Share on other sites
Machaira    1033
If you want to draw an ellipse over one of the controls, you need to draw on the control, not the form. Put the code in the user control's Paint event and you'll be fine.

Share this post


Link to post
Share on other sites
paic    645
Quote:
Original post by Machaira
If you want to draw an ellipse over one of the controls, you need to draw on the control, not the form. Put the code in the user control's Paint event and you'll be fine.


Yes, I know I could do that, but the ellipse will be clipped to the control's region, and I want this ellipse to be able to be drawn on the the edge of the control (half the ellipse is over the control, the other half outside. I don't know the correct words to say that :p)

Isn't there a way to specify the Z order of all the elements that are drawn ? ... or to flush the graphics, to force the controls to draw themselves, and then draw the ellipse on top ? (I tried the Graphics.Flush() method, but it doesn't work)

Share this post


Link to post
Share on other sites
Bob Janova    769
Controls are drawn on top of their parents, that's just how controls work (otherwise you couldn't see them). I think the only way to do what you want is to draw the ellipse on both the form and the control. (Also, some controls like buttons won't let you draw random stuff on them if I remember right.)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this