Wednesday, February 5, 2014

How to draw rounded rectangles / borders?

using System.Drawing.Drawing2D;     

 public void DrawRoundRect(Graphics g, Pen p, float X, float Y, float width, float height, float radius, bool fill, System.Drawing.Brush brush)
        {
            try
            {
                GraphicsPath gp = new GraphicsPath();
                gp.AddLine(X + radius, Y, X + width - (radius * 2), Y);
                gp.AddArc(X + width - (radius * 2), Y, radius * 2, radius * 2, 270, 90);
                gp.AddLine(X + width, Y + radius, X + width, Y + height - (radius * 2));
                gp.AddArc(X + width - (radius * 2), Y + height - (radius * 2), radius * 2, radius * 2, 0, 90);
                gp.AddLine(X + width - (radius * 2), Y + height, X + radius, Y + height);
                gp.AddArc(X, Y + height - (radius * 2), radius * 2, radius * 2, 90, 90);
                gp.AddLine(X, Y + height - (radius * 2), X, Y + radius);
                gp.AddArc(X, Y, radius * 2, radius * 2, 180, 90);
                gp.CloseFigure();
                if (fill)
                    g.FillPath(brush, gp);
                g.DrawPath(p, gp);
                gp.Dispose();
            }
            catch (Exception ee)
            {
                MessageBox.Show(this, "DrawRoundRect" + ee.Message, "Exception");
            }
        }

        private void backPanel_Paint(object sender, PaintEventArgs e)
        {
            Graphics v = e.Graphics;
            v.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            Pen pathPen = new Pen(Color.Black, 1);
            DrawRoundRect(v, pathPen, backPanel.Left - 2, backPanel.Top - 2, backPanel.Width - 2, backPanel.Height - 2, 6, false, null);
            base.OnPaint(e);
        }

No comments:

Post a Comment