예제 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="path"></param>
        /// <param name="layout"></param>
        /// <param name="radius"></param>
        /// <param name="corners"></param>
        public static void AddRoundedRectangle(this GraphicsPath path, Rectangle layout, int radius, RectangleCorners corners)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            if (radius < 0)
            {
                throw new ArgumentOutOfRangeException("radius");
            }

            //map the structure
            int xw   = layout.Right,
                yh   = layout.Bottom,
                xwr  = xw - radius,
                yhr  = yh - radius,
                xr   = layout.X + radius,
                yr   = layout.Y + radius,
                r2   = radius * 2,
                xwr2 = xw - r2,
                yhr2 = yh - r2;

            //create new figure
            path.StartFigure();

            //draw top left corner
            if (corners.HasFlag(RectangleCorners.TopLeft))
            {
                path.AddArc(layout.X, layout.Y, r2, r2, 180, 90);
            }
            else
            {
                path.AddLine(layout.X, yr, layout.X, layout.Y);
                path.AddLine(layout.X, layout.Y, xr, layout.Y);
            }

            //draw top line
            path.AddLine(xr, layout.Y, xwr, layout.Y);

            //draw top right corner
            if (corners.HasFlag(RectangleCorners.TopRight))
            {
                path.AddArc(xwr2, layout.Y, r2, r2, 270, 90);
            }
            else
            {
                path.AddLine(xwr, layout.Y, xw, layout.Y);
                path.AddLine(xw, layout.Y, xw, yr);
            }

            //draw right line
            path.AddLine(xw, yr, xw, yhr);

            //draw bottom right corner
            if (corners.HasFlag(RectangleCorners.BottomRight))
            {
                path.AddArc(xwr2, yhr2, r2, r2, 0, 90);
            }
            else
            {
                path.AddLine(xw, yhr, xw, yh);
                path.AddLine(xw, yh, xwr, yh);
            }

            //draw bottom line
            path.AddLine(xwr, yh, xr, yh);

            //draw bottom left corner
            if (corners.HasFlag(RectangleCorners.BottomLeft))
            {
                path.AddArc(layout.X, yhr2, r2, r2, 90, 90);
            }
            else
            {
                path.AddLine(xr, yh, layout.X, yh);
                path.AddLine(layout.X, yh, layout.X, yhr);
            }

            //draw left line
            path.AddLine(layout.X, yhr, layout.X, yr);

            //complete figure
            path.CloseFigure();
        }
예제 #2
0
        public static GraphicsPath Create(int x, int y, int width, int height, int radius, RectangleCorners corners, WhichHalf half)
        {
            if (radius <= 0)
            {
                GraphicsPath rectp = new GraphicsPath();
                rectp.AddRectangle(new Rectangle(x, y, width, height));
                return(rectp);
            }

            int dia = radius * 2;

            Rectangle TLarc = new Rectangle(x, y, dia, dia);
            Rectangle TRarc = new Rectangle(x + width - dia - 1, y, dia, dia);
            Rectangle BRarc = new Rectangle(x + width - dia - 1, y + height - dia - 1, dia, dia);
            Rectangle BLarc = new Rectangle(x, y + height - dia - 1, dia, dia);

            Rectangle TLsquare = new Rectangle(x, y, radius, radius);
            Rectangle TRsquare = new Rectangle(x + width - radius, y, radius, radius);
            Rectangle BRsquare = new Rectangle(x + width - radius, y + height - radius, radius, radius);
            Rectangle BLsquare = new Rectangle(x, y + height - radius, radius, radius);

            GraphicsPath p = new GraphicsPath();

            p.StartFigure();

            if (half == WhichHalf.Both || half == WhichHalf.TopLeft)
            {
                if (corners.HasFlag(RectangleCorners.BottomLeft))
                {
                    p.AddArc(BLarc, 135, 45);
                }
                else
                {
                    p.AddLine(BLsquare.Left, BLsquare.Bottom, BLsquare.Left, BLsquare.Top);
                }

                p.AddLine(BLsquare.Left, BLsquare.Top - 1, TLsquare.Left, TLsquare.Bottom + 1);

                if (corners.HasFlag(RectangleCorners.TopLeft))
                {
                    p.AddArc(TLarc, 180, 90);
                }
                else
                {
                    Corner(p, TLsquare.Left, TLsquare.Bottom, TLsquare.Left, TLsquare.Top, TLsquare.Right, TLsquare.Top);
                }

                p.AddLine(TLsquare.Right + 1, TLsquare.Top, TRsquare.Left - 1, TRsquare.Top);

                if (corners.HasFlag(RectangleCorners.TopRight))
                {
                    p.AddArc(TRarc, -90, 45);
                }
            }

            if (half == WhichHalf.Both || half == WhichHalf.BottomRight)
            {
                if (corners.HasFlag(RectangleCorners.TopRight))
                {
                    p.AddArc(TRarc, -45, 45);
                }
                else
                {
                    p.AddLine(TRsquare.Right, TRsquare.Top, TRsquare.Right, TRsquare.Bottom);
                }

                p.AddLine(TRsquare.Right, TRsquare.Bottom + 1, BRsquare.Right, BRsquare.Top - 1);

                if (corners.HasFlag(RectangleCorners.BottomRight))
                {
                    p.AddArc(BRarc, 0, 90);
                }
                else
                {
                    Corner(p, BRsquare.Right, BRsquare.Top, BRsquare.Right, BRsquare.Bottom, BRsquare.Left, BRsquare.Bottom);
                }

                p.AddLine(BRsquare.Left - 1, BRsquare.Bottom, BLsquare.Right + 1, BLsquare.Bottom);

                if (corners.HasFlag(RectangleCorners.BottomLeft))
                {
                    p.AddArc(BLarc, 90, 45);
                }
                else
                {
                    p.AddLine(BLsquare.Right, BLsquare.Bottom, BLsquare.Left, BLsquare.Bottom);
                }
            }

            return(p);
        }