画多边形form并填充背景色(可以实现圆角边框 有锯齿)

 public Form1()
        {
            InitializeComponent();

            this.BackColor = ColorTranslator.FromHtml("#F7F1F1");
            this.TransparencyKey = ColorTranslator.FromHtml("#F7F1F1");
            this.label1.Text = "ccccccccccccccccccccccccccccc";
            this.label1.BackColor = Color.White;
            this.Opacity = 1;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            //ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, Color.Orange, ButtonBorderStyle.Solid);
            //Draw(e.ClipRectangle, e.Graphics, 80);
            //Bitmap bitmap = new Bitmap(384, 305);
            //for (int i = 0; i < bitmap.Height; i++)
            //{
            //    bitmap.SetPixel(i, i, Color.Black);
            //}
            //bitmap.SetPixel(2, 2, Color.Black);
            //e.Graphics.DrawImage(bitmap, new Point(0, 0));
            //bitmap.Save(@"d:\c.png");
            SolidBrush solidBrush = new SolidBrush(Color.White);
            var gpath = DrawRoundRect(e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height, 50);
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            e.Graphics.FillPath(solidBrush, gpath);
            e.Graphics.DrawPath(Pens.White, gpath);
        }

        /// <summary>
        /// 设置窗体的Region
        /// </summary>
        public void SetWindowRegion()
        {
            GraphicsPath FormPath;
            Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
            FormPath = GetRoundedRectPath(rect, 80);
            this.Region = new Region(FormPath);

        }
        /// <summary>
        /// 绘制圆角路径
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="radius"></param>
        /// <returns></returns>
        private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
        {
            int diameter = radius;
            Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
            GraphicsPath path = new GraphicsPath();

            // 左上角
            path.AddArc(arcRect, 180, 90);

            // 右上角
            arcRect.X = rect.Right - diameter;
            path.AddArc(arcRect, 270, 90);

            // 右下角
            arcRect.Y = rect.Bottom - diameter;
            path.AddArc(arcRect, 0, 90);

            // 左下角
            arcRect.X = rect.Left;
            path.AddArc(arcRect, 90, 90);
            path.CloseFigure();//闭合曲线
            return path;
        }

        /// <summary>
        /// 窗体size发生改变时重新设置Region属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Resize(object sender, EventArgs e)
        {
            //SetWindowRegion();
        }

        //重绘边框
        private void Draw(Rectangle rectangle, Graphics g, int radius)
        {
            SolidBrush solidBrush = new SolidBrush(Color.White);
            Pen shadowPen = new Pen(solidBrush, 1);
            g.SmoothingMode = SmoothingMode.HighQuality;

            g.CompositingQuality = CompositingQuality.HighQuality;
            g.DrawPath(shadowPen, DrawRoundRect(rectangle.X, rectangle.Y, rectangle.Width - 2, rectangle.Height - 1, radius));
        }
        public static GraphicsPath DrawRoundRect(int x, int y, int width, int height, int radius)
        {
            GraphicsPath gp = new GraphicsPath();
            //gp.AddArc(x, y, radius, radius, 180, 90);
            //gp.AddArc(width - radius, y, radius, radius, 270, 90);
            //gp.AddArc(width - radius, height - radius, radius, radius, 0, 90);
            //gp.AddArc(x, height - radius, radius, radius, 90, 90);
            //gp.CloseAllFigures();
            gp.AddArc(x, y, radius, radius, 180, 90);
            gp.AddArc(width - 200, y, radius, radius, 270, 90);
            gp.AddArc(width - radius, height - radius, radius, radius, 0, 90);
            gp.AddArc(x, height - radius, radius, radius, 90, 90);
            gp.CloseAllFigures();
            return gp;
        }

 

posted @ 2019-01-23 21:00  fabc  阅读(497)  评论(0编辑  收藏  举报