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