C#GDI画立体渐变圆角panel

为要处理的panel添加绘画事件,代码写在panel的Paint事件中:

 private void panel1_Paint(object sender, PaintEventArgs e)
        {

            //(new 一个矩形,范围为panel的上半部分)

            Rectangle rt1 = new Rectangle(0, 0, panel1.Width, (panel1.Height/2));

            //(new一个渐变笔刷,渐变范围为上面新建的矩形,即panel的上半部分,渐变色从白色到蓝色,渐变角度LinearGradientMode.Vertical为垂直渐变)
            LinearGradientBrush br = new LinearGradientBrush(rt1, Color.White, Color.Blue,   LinearGradientMode.Vertical);
            Graphics g = e.Graphics;//为控件创建Graphics
            g.FillRectangle(br, rt1);//填充矩形

//(下面同上面的操作,渐变panel的下半部分)
            LinearGradientBrush br2 = new LinearGradientBrush(rt1, Color.Blue, Color.White, LinearGradientMode.Vertical);
            Rectangle rt2 = new Rectangle(0,(panel1.Height/2), panel1.Width, panel1.Height/2);
            g.FillRectangle(br2, rt2);

            //实现圆角形状的panel
            List list = new List();//建立点集合list
            int width = this.panel1.Width;
            int height = this.panel1.Height;
            //panel左上的点
            list.Add(new Point(0, 4));
            list.Add(new Point(1, 4));
            list.Add(new Point(1, 3));
            list.Add(new Point(2, 2));
            list.Add(new Point(3, 1));
            list.Add(new Point(4, 1));
            list.Add(new Point(4, 0));
            //panel右上的点
            list.Add(new Point(width - 4, 0));
            list.Add(new Point(width - 4, 1));
            list.Add(new Point(width - 3, 1));
            list.Add(new Point(width - 2, 2));
            list.Add(new Point(width - 1, 3));
            list.Add(new Point(width - 1, 4));
            list.Add(new Point(width - 0, 4));
            //panel右下角的点
            list.Add(new Point(width - 0, height - 4));
            list.Add(new Point(width - 1, height - 4));
            list.Add(new Point(width - 1, height - 3));
            list.Add(new Point(width - 2, height - 2));
            list.Add(new Point(width - 3, height - 1));
            list.Add(new Point(width - 4, height - 1));
            list.Add(new Point(width - 4, height - 0));
            //panel左下角的点
            list.Add(new Point(4, height - 0));
            list.Add(new Point(4, height - 1));
            list.Add(new Point(3, height - 1));
            list.Add(new Point(2, height - 2));
            list.Add(new Point(1, height - 3));
            list.Add(new Point(1, height - 4));
            list.Add(new Point(0, height - 4));
            Point[] points = list.ToArray();//将以上圆角形状的点集合转换成点数组
            GraphicsPath shape = new GraphicsPath();//新建绘图路径对象
            shape.AddPolygon(points);将路径转换为多边形
            //将窗体的显示区域设为GraphicsPath的实例
            this.panel1.Region = new System.Drawing.Region(shape);

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值