c#绘制扇形,圆角矩形,多边形

c#绘制扇形,圆角矩形,多边形_第1张图片

  class Class1 : Control
    {
     
        public Class1()
        {
     
            this.SetStyle(ControlStyles.ResizeRedraw, true);//空间大小改变时,控件会重绘
        }

        protected override void OnPaint(PaintEventArgs e)
        {
     
            base.OnPaint(e);


            Graphics graphics = this.CreateGraphics();
            graphics.SmoothingMode = SmoothingMode.AntiAlias;
            int width = this.Width;
            int height = this.Height;

            Rectangle rct = new Rectangle(0, 0, width / 4, height / 4);
            Pen pen = new Pen(Color.Red);
            graphics.DrawArc(pen, rct, 0, 120);//绘制弧线,弧线是由Rectangle构成的椭圆的的弧线组成,顺时针从0到360,水平向右是0度。

            rct = new Rectangle(0, 0, width / 3, width / 3);
            Brush brush = new SolidBrush(Color.Green);
            graphics.DrawPie(pen, rct, 0, 120);//绘制扇形

            graphics.FillPie(brush, rct, 0, 120);//填充扇形;

            GraphicsPath graphicsPath = DrawAnyShapeWithPath(new Rectangle(200, 200, 100, 100), 10);
            graphics.DrawPath(pen, graphicsPath);//绘制圆角矩形

            graphics.FillPath(brush , graphicsPath);//填充圆角矩形



            graphics.DrawPolygon(pen,new Point[] {
      new Point (50,50),new Point (60,50),new Point (50,70)});//绘制多边形



            pen.Dispose();
            graphics.Dispose();
        }

        /// 
        /// 默认各个路径的首位会相连
        /// 
        /// 
        /// 
        GraphicsPath DrawAnyShapeWithPath(Rectangle rectangle, int radius)
        {
     
            int r;
            if(4*radius <rectangle .Width && 4*radius <rectangle .Height  )
            {
     
                r = radius;
            }
            else
            {
     
                r = rectangle.Width / 4;
            }
            GraphicsPath graphicsPath = new GraphicsPath();
            graphicsPath.AddArc(new Rectangle(rectangle.X, rectangle.Y, 2 * r, 2 * r), 180, 90);
            graphicsPath.AddArc(new Rectangle(rectangle.X + rectangle.Width - 2 * r, rectangle.Y, 2 * r, 2 * r), 270, 90);
            graphicsPath.AddArc(new Rectangle(rectangle.X + rectangle.Width - 2 * r, rectangle.Y + rectangle.Height - 2 * r, 2 * r, 2 * r), 0, 90);
            graphicsPath.AddArc(new Rectangle(rectangle.X, rectangle.Y + rectangle.Height - 2 * r, 2 * r, 2 * r), 90, 90);
            graphicsPath.CloseFigure();
            return graphicsPath;
        }



    }

你可能感兴趣的:(c#)