Round edge panel: edges not smooth

M

MMahank

I am creating a round edge panel by inheriting from the panel. What I do is
create a graphics path with desired shape and in the Paint method I fill the
graphics path and draw the graphics path border with desired color. This
would look as if the panel has round edges. So far I have not set the
region. Bounds of the panel is still the rectangle.

But the problem come when I set the region of the panel to the Graphics path
region. It would not draw the borders( especially left and bottom) properly,
it would not draw the curves properly. Also if I set the background image ,
the image is not smooth along the borders even though I set smoothing mode
to AntiAliasing .

Any ideas on why it is behaving this way would be greately appreciated.

Here is the code in Paint method

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Graphics gr = e.Graphics;
gr.SmoothingMode = SmoothingMode.AntiAlias;
if(_roundedCorners)
{
// clear background with the parent's backcolor before drawing the round
edges
gr.Clear(Parent.BackColor);
gr.DrawPath(_borderPen, _roundedEdge);
// now fill with actual color
gr.FillPath( _graphicsPathBrush,_roundedEdge);
// setting the region of the control
this.Region = new Region( _roundedEdge); // this is where the problem comes
}
}

Here is the method where I create the path
protected void CreatePaths()

{

// Create _roundedEdge

_roundedEdge = new GraphicsPath();

_roundedEdge.AddLine(_topLeftEdgeRadius, 0, Width - _topRightEdgeRadius - 1,
0);

_roundedEdge.AddArc(Width - _topRightEdgeRadius - 1 - _topRightEdgeRadius,
_topRightEdgeRadius - _topRightEdgeRadius, 2 *_topRightEdgeRadius, 2
*_topRightEdgeRadius, -90, 90);

_roundedEdge.AddLine(Width - 1, _topRightEdgeRadius, Width - 1, Height -
_bottomRightEdgeRadius - 1);

_roundedEdge.AddArc(Width - _bottomRightEdgeRadius - 1 -
_bottomRightEdgeRadius, Height - _bottomRightEdgeRadius - 1 -
_bottomRightEdgeRadius, 2 *_bottomRightEdgeRadius, 2
*_bottomRightEdgeRadius, 0, 90);

_roundedEdge.AddLine(Width - _bottomRightEdgeRadius - 1, Height - 1,
_bottomLeftEdgeRadius,Height - 1);

_roundedEdge.AddArc(0, Height - _bottomLeftEdgeRadius - 1 -
_bottomLeftEdgeRadius, 2 *_bottomLeftEdgeRadius, 2 *_bottomLeftEdgeRadius,
90, 90);

_roundedEdge.AddLine(0,Height - _bottomLeftEdgeRadius - 1, 0,
_topLeftEdgeRadius);

_roundedEdge.AddArc(0, 0, 2 *_topLeftEdgeRadius, 2 *_topLeftEdgeRadius, 180,
90);

}



Thanks,
VPMahank.
 
M

Mick Doherty

Regions don't AntiAlias and so they look rough. What I Generally do is paint
the parent to the panel, so it appears transparent, and then paint the
border.

Here's an example of code added to a ScrollableControl (Panel without
BorderStyle)

\\\
protected override void OnMove(EventArgs e)
{
base.OnMove (e);
this.Invalidate();
}

protected override void OnResize(EventArgs e)
{
base.OnResize (e);
this.Invalidate();
}

protected override void OnPaintBackground(PaintEventArgs pevent)
{
//Draw the Parent onto our Control to give pseudo transparency. The
//BeginContainer and EndContainer calls stop incorrect painting of child
//controls when both the container and child have BackColor set to
//Transparent. This only happens as a result of the
//TranslateTransform() call.
System.Drawing.Drawing2D.GraphicsContainer g =
pevent.Graphics.BeginContainer();
Rectangle translateRect = this.Bounds;
pevent.Graphics.TranslateTransform(-this.Left,-this.Top);
PaintEventArgs pe = new PaintEventArgs(pevent.Graphics,translateRect);
this.InvokePaintBackground(this.Parent,pe);
this.InvokePaint(this.Parent,pe);
pevent.Graphics.ResetTransform();
pevent.Graphics.EndContainer(g);

//Define the custom Border Region, Brush and Pen.
System.Drawing.Drawing2D.GraphicsPath border;
Brush paintBrush = new SolidBrush(this.BackColor);
Pen borderPen = new Pen(this.ForeColor);
Rectangle r = this.ClientRectangle;

// //Optionally Set the Region of the Control
// this.Region = new Region(RoundRectangle(r));

r.Inflate(-1,-1);
border = RoundRectangle(r);

//Fill The Region with the Controls BackColor
pevent.Graphics.FillPath(paintBrush,border);

//Draw the Region
pevent.Graphics.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.HighQuality;

pevent.Graphics.DrawPath(borderPen,border);

//Clean Up
borderPen.Dispose();
paintBrush.Dispose();
border.Dispose();

}

private System.Drawing.Drawing2D.GraphicsPath RoundRectangle(Rectangle r)
{
System.Drawing.Drawing2D.GraphicsPath path =
new System.Drawing.Drawing2D.GraphicsPath();

path.AddArc(r.Left,r.Top,32,32,180,90);
path.AddArc(r.Right - 32,r.Top,32,32,270,90);
path.AddArc(r.Right-32,r.Bottom-32,32,32,0,90);
path.AddArc(r.Left,r.Bottom-32,32,32,90,90);
path.CloseFigure();

return path;
}
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top