views:

54

answers:

1

I'm drawing a rounded rectangle on a control. At first I was only binding a paint handler to the Paint event at runtime:

this.Paint += new PaintEventHandler(Panel_Paint);

Then I figured out I can just override OnPaint which should then cause the drawing at design time, too:

 protected override void OnPaint(PaintEventArgs e) {
    base.OnPaint(e);
    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    DrawRoundedRectangle(
       e.Graphics,
       new Rectangle(0, 0, this.Width - 1, this.Height - 1),
       12, new Pen(Color.Black, 1F)
    );
 }

But it doesn't. This draws beautifully on the control at runtime, but I see no rectangle at design time.

I was looking into how to create different design-time behavior from runtime behavior with an extra class for my UserControl that subclasses ControlDesigner. But it's not working at design time at all, so I'm getting ahead of myself.

What am I doing wrong?

For reference here is my DrawRoundedRectangle function:

public static void DrawRoundedRectangle(Graphics g, Rectangle r, int diameter, Pen p){
   System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
   gp.AddArc(r.X, r.Y, diameter, diameter, 180, 90);
   gp.AddArc(r.X + r.Width - diameter, r.Y, diameter, diameter, 270, 90);
   gp.AddArc(r.X + r.Width - diameter, r.Y + r.Height - diameter, diameter, diameter, 0, 90);
   gp.AddArc(r.X, r.Y + r.Height - diameter, diameter, diameter, 90, 90);
   gp.CloseFigure();
   g.DrawPath(p, gp);
}
A: 

As Hans Passant pointed out, you might be using the wrong method. Use the methods in the Graphics object, which is a property of the PaintEventArgs object.

Graphics g = e.Graphics; 
g._drawingmethodofyourchoice_();
lb
I updated my question to explain where the DrawRoundedRectangle function is coming from.
Emtucifor