Пример #1
0
 /// <summary>
 /// Adds a round rectangle to the graphics path where the integer radius specified determines how rounded the rectangle should become.
 /// This can be thought of rounded arcs connected by straight lines.
 /// </summary>
 /// <param name="self"></param>
 /// <param name="bounds"></param>
 /// <param name="radius"></param>
 public static void AddRoundedRectangle(this GraphicsPath self, Rectangle bounds, int radius)
 {
     if (radius * 2 > bounds.Width)
     {
         self.AddEllipse(bounds);
         return;
     }
     if (radius <= 0)
     {
         self.AddRectangle(bounds);
         return;
     }
     int w = radius * 2;
     Rectangle br = new Rectangle(bounds.Right - w, bounds.Bottom - w, w, w);
     Rectangle bl = new Rectangle(bounds.Left, bounds.Bottom - w, w, w);
     Rectangle tl = new Rectangle(bounds.Left, bounds.Top, w, w);
     Rectangle tr = new Rectangle(bounds.Right - w, bounds.Top, w, w);
     self.AddArc(br, 0, 90F);
     self.AddLine(new Point(bounds.Right - radius, bounds.Bottom), new Point(bounds.Left + radius, bounds.Bottom));
     self.AddArc(bl, 90F, 90F);
     self.AddLine(new Point(bounds.Left, bounds.Bottom - radius), new Point(bounds.Left, bounds.Top + radius));
     self.AddArc(tl, 180F, 90F);
     self.AddLine(new Point(bounds.Left + radius, bounds.Top), new Point(bounds.Right - radius, bounds.Top));
     self.AddArc(tr, 270F, 90F);
     self.AddLine(new Point(bounds.Right, bounds.Top + radius), new Point(bounds.Right, bounds.Bottom - radius));
     self.CloseFigure();
 }
        /// <summary>
        /// Adds a rectangle with round corners to the current GraphicsPath.
        /// </summary>
        /// <param name="gp">The current GraphicsPath object.</param>
        /// <param name="point">The coordinates of the upper left corner.</param>
        /// <param name="size">The dimension of the rectangle.</param>
        /// <param name="radius">The radius of the different corners starting with the upper left corner.</param>
        /// <returns>The current GraphicsPath object.</returns>
        public static GraphicsPath AddRoundRectangle(this GraphicsPath gp, PointF point, SizeF size, params float[] radius)
        {
            var r = new float[4];
            var d = new float[4];
            var bottom = point.Y + size.Height;
            var right = point.X + size.Width;

            for (int i = 0; i < r.Length; i++)
            {
                r[i] = radius.Length > 0 ? radius[i % radius.Length] : 0f;
                d[i] = 2f * r[i];
            }

            if (r[0] > 0f)
                gp.AddArc(new RectangleF(point.X, point.Y, d[0], d[0]), 180, 90);

            gp.AddLine(new PointF(point.X + r[0], point.Y), new PointF(right - r[1], point.Y));

            if (r[1] > 0f)
                gp.AddArc(new RectangleF(right - d[1], point.Y, d[1], d[1]), 270, 90);

            gp.AddLine(new PointF(right, point.Y + r[1]), new PointF(right, bottom - r[2]));

            if (r[2] > 0f)
                gp.AddArc(new RectangleF(right - d[2], bottom - d[2], d[2], d[2]), 0, 90);

            gp.AddLine(new PointF(right - r[2], bottom), new PointF(point.X + r[3], bottom));

            if (r[3] > 0f)
                gp.AddArc(new RectangleF(point.X, bottom - d[3], d[3], d[3]), 90, 90);

            gp.AddLine(new PointF(point.X, bottom - r[3]), new PointF(point.X, point.Y + r[0]));
            gp.CloseFigure();
            return gp;
        }
Пример #3
0
        public static void AddCapsule(this GraphicsPath graphicsPath, RectangleF rect)
        {
            float diameter;
            RectangleF arc;

            try
            {
                if (rect.Width > rect.Height)
                {
                    // Horizontal capsule
                    diameter = rect.Height;
                    SizeF sizeF = new SizeF(diameter, diameter);
                    arc = new RectangleF(rect.Location, sizeF);
                    graphicsPath.AddArc(arc, 90, 180);
                    arc.X = rect.Right - diameter;
                    graphicsPath.AddArc(arc, 270, 180);
                }
                else if (rect.Width < rect.Height)
                {
                    // Vertical capsule
                    diameter = rect.Width;
                    SizeF sizeF = new SizeF(diameter, diameter);
                    arc = new RectangleF(rect.Location, sizeF);
                    graphicsPath.AddArc(arc, 180, 180);
                    arc.Y = rect.Bottom - diameter;
                    graphicsPath.AddArc(arc, 0, 180);
                }
                else
                {
                    // Circle
                    graphicsPath.AddEllipse(rect);
                }
            }
            catch
            {
                graphicsPath.AddEllipse(rect);
            }

            graphicsPath.CloseFigure();
        }
Пример #4
0
        public static void AddRoundedRectangle(this GraphicsPath path, RectangleF rect, int cornerRadius)
        {
            if (cornerRadius > 0)
            {
                path.StartFigure();
                path.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
                path.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
                path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
                path.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);

                path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);

                path.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
                path.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
                path.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
                path.CloseFigure();
            }
            else
            {
                path.AddRectangle(rect);
            }
        }
Пример #5
0
        public static void AddRoundedRectangle(this GraphicsPath graphicsPath, RectangleF rect, float radius)
        {
            if (radius <= 0f)
            {
                graphicsPath.AddRectangle(rect);
            }
            else
            {
                // If the corner radius is greater than or equal to
                // half the width, or height (whichever is shorter)
                // then return a capsule instead of a lozenge
                if (radius >= (Math.Min(rect.Width, rect.Height) / 2.0f))
                {
                    graphicsPath.AddCapsule(rect);
                }
                else
                {
                    // Create the arc for the rectangle sides and declare
                    // a graphics path object for the drawing
                    float diameter = radius * 2.0f;
                    SizeF size = new SizeF(diameter, diameter);
                    RectangleF arc = new RectangleF(rect.Location, size);

                    // Top left arc
                    graphicsPath.AddArc(arc, 180, 90);

                    // Top right arc
                    arc.X = rect.Right - diameter;
                    graphicsPath.AddArc(arc, 270, 90);

                    // Bottom right arc
                    arc.Y = rect.Bottom - diameter;
                    graphicsPath.AddArc(arc, 0, 90);

                    // Bottom left arc
                    arc.X = rect.Left;
                    graphicsPath.AddArc(arc, 90, 90);

                    graphicsPath.CloseFigure();
                }
            }
        }