[.net] Painting something on a form

Started by
2 comments, last by Bob Janova 17 years, 4 months ago
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.
Advertisement
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.

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

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)

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

This topic is closed to new replies.

Advertisement