public partial class CSkinPanel : Panel
    {
        private Color _BaseBackColor = Color.Transparent;
        /// <summary>
        /// 背景初始色
        /// </summary>
        [Browsable(true), Category("Background"), Description("背景初始色")]
        public Color BaseBackColor
        {
            get { return _BaseBackColor; }
            set { _BaseBackColor = value; this.Refresh(); }
        }
        private Color _GradualColor = Color.White;
        /// <summary>
        /// 渐变颜色
        /// </summary>
        [Browsable(true), Category("Background"), Description("渐变颜色")]
        public Color GradualColor
        {
            get { return _GradualColor; }
            set { _GradualColor = value; this.Refresh(); }
        }
        private bool _ShowGradualBack = false;
        /// <summary>
        /// 是否应用渐变背景
        /// </summary>
        [Browsable(true), Category("Background"), Description("是否应用渐变背景")]
        public bool ShowGradualBack
        {
            get { return _ShowGradualBack; }
            set { _ShowGradualBack = value; this.Refresh(); }
        }
        private GradualType _GradualType = GradualType.LinearGradient;
        /// <summary>
        /// 渐变背景类型
        /// </summary>
        [Browsable(true), Category("Background"), Description("渐变背景类型")]
        public GradualType GradualType
        {
            get { return _GradualType; }
            set { _GradualType = value; this.Refresh(); }
        }
        private LinearGradientMode _Mode = LinearGradientMode.Vertical;
        /// <summary>
        /// 渐变模式
        /// </summary>
        [Browsable(true), Category("Background"), Description("渐变模式")]
        public LinearGradientMode Mode
        {
            get { return _Mode; }
            set { _Mode = value; this.Refresh(); }
        }
        private int _Radius = 8;
        /// <summary>
        /// 圆角大小
        /// </summary>
        [Browsable(true), Category("Shape"), Description("圆角大小")]
        public int Radius
        {
            get { return _Radius; }
            set { _Radius = value; this.Refresh(); }
        }
        private RadiusStyle _RadiusStyle = RadiusStyle.None;
        /// <summary>
        /// 圆角样式
        /// </summary>
        [Browsable(true), Category("Shape"), Description("圆角样式")]
        public RadiusStyle Style
        {
            get { return _RadiusStyle; }
            set { _RadiusStyle = value; ReRegion(); }
        }
        private int _OffSet = 0;
        /// <summary>
        /// 偏移量
        /// </summary>
        [Browsable(true), Category("Shape"), Description("偏移量")]
        public int OffSet
        {
            get { return _OffSet; }
            set { _OffSet = value; this.Refresh(); }
        }
        private bool _ShowBorder = false;
        /// <summary>
        /// 显示边框
        /// </summary>
        [Browsable(true), Category("Border"), Description("显示边框")]
        public bool ShowBorder
        {
            get { return _ShowBorder; }
            set { _ShowBorder = value; this.Refresh(); }
        }
        private Color _BorderColor = Color.Black;
        /// <summary>
        /// 边框颜色
        /// </summary>
        [Browsable(true), Category("Border"), Description("边框颜色")]
        public Color BorderColor
        {
            get { return _BorderColor; }
            set { _BorderColor = value; this.Refresh(); }
        }
        private int _BorderWidth = 1;
        /// <summary>
        /// 边框宽度
        /// </summary>
        [Browsable(true), Category("Border"), Description("边框宽度")]
        public int BorderWidth
        {
            get { return _BorderWidth; }
            set { _BorderWidth = value; this.Refresh(); }
        }
        private Color _BorderGradualColor = Color.White;
        /// <summary>
        /// 边框渐变颜色
        /// </summary>
        [Browsable(true), Category("Border"), Description("边框渐变颜色")]
        public Color BorderGradualColor
        {
            get { return _BorderGradualColor; }
            set { _BorderGradualColor = value; this.Refresh(); }
        }
        private bool _ShowBorderGradual = false;
        /// <summary>
        /// 显示边框渐变
        /// </summary>
        [Browsable(true), Category("Border"), Description("显示边框渐变")]
        public bool ShowBorderGradual
        {
            get { return _ShowBorderGradual; }
            set { _ShowBorderGradual = value; this.Refresh(); }
        }

private int LoadCount
        { get; set; }

IntPtr hHandle = IntPtr.Zero;//GetWindowDC(m.HWnd);

public CSkinPanel()
            : base()
        {
            hHandle = GetWindowDC(this.Handle);
            LoadCount = 0;
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);// 双缓冲
            //this.SetStyle(ControlStyles.ResizeRedraw, true);//调整大小时重绘
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
            //this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);// 双缓冲            
            //this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        }

protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == 0x0047)
                ReRegion();
            if (m.Msg == 0x000F || m.Msg == 0x133)
            {
                IntPtr hDC = GetWindowDC(m.HWnd);
                if (hDC.ToInt32() == 0) //如果取设备上下文失败则返回
                {
                    return;
                }
                //建立Graphics对像
                Graphics g = Graphics.FromHdc(hDC);
                //画边框
                RePaint(g);
                //释放DC 
                ReleaseDC(m.HWnd, hDC);
            }
        }

private void ReRegion()
        {
            Rectangle Rectangle = new Rectangle(OffSet, OffSet, this.Width - OffSet * 2, this.Height - OffSet * 2);
            GraphicsPath path = CreatePath(Rectangle, this.Radius, this.Style);
            if (path == null)
                return;
            this.Region = new Region(path);
            //this.Refresh();
        }

protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            //ReRegion();
            //Refresh();
        }

private void RePaint(Graphics g)
        {
            if (g == null)
                return;
            try
            {
                g.SmoothingMode = SmoothingMode.HighQuality;  //图片柔顺模式选择
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;//高质量
                g.CompositingQuality = CompositingQuality.HighQuality;//再加一点
                Rectangle Rectangle = new Rectangle(OffSet, OffSet, this.Width - OffSet * 2, this.Height - OffSet * 2);
                GraphicsPath path = CreatePath(Rectangle, this.Radius, this.Style);
                if (path == null)
                    return;
                if (ShowGradualBack)
                {
                    if (GradualType == Forms.GradualType.LinearGradient)
                    {
                        LinearGradientBrush brush = new LinearGradientBrush(Rectangle, BaseBackColor, GradualColor, Mode);
                        g.FillRectangle(brush, Rectangle);
                    }
                    else
                    {
                        Color[] surroundColor = new Color[] { BaseBackColor };
                        PathGradientBrush pb = new PathGradientBrush(path);
                        pb.CenterColor = GradualColor;
                        pb.SurroundColors = surroundColor;
                        pb.CenterPoint = new PointF(this.Width / 2, this.Height / 2);
                        g.FillPath(pb, path);
                    }
                }
                //this.Region = new Region(path);
                if (ShowBorder)
                {
                    Pen pen = null;
                    if (!ShowBorderGradual)
                        pen = new Pen(BorderColor);
                    else
                    {
                        LinearGradientBrush brush = new LinearGradientBrush(Rectangle, BorderColor, BorderGradualColor, LinearGradientMode.Vertical);
                        pen = new Pen(brush);
                    }
                    pen.Width = BorderWidth;
                    if (this.Style == RadiusStyle.None)
                    {
                        g.DrawRectangle(pen, Rectangle);
                    }
                    else
                    {
                        g.DrawPath(pen, path);
                    }
                }
            }
            catch { }
        }

private GraphicsPath CreatePath(Rectangle rectangle, int radius, RadiusStyle style)
        {
            try
            {
                int lr = 2 * radius;
                GraphicsPath path = new GraphicsPath();
                if (style == RadiusStyle.None)
                {
                    path.AddLine(rectangle.X, rectangle.Y, rectangle.Right, rectangle.Y);
                    path.AddLine(rectangle.Right, rectangle.Y, rectangle.Right, rectangle.Bottom);
                    path.AddLine(rectangle.Right, rectangle.Bottom, rectangle.X, rectangle.Bottom);
                    path.AddLine(rectangle.X, rectangle.Bottom, rectangle.X, rectangle.Y);
                }
                else if (style == RadiusStyle.All)
                {
                    //上
                    path.AddLine(new Point(rectangle.X + radius, rectangle.Y), new Point(rectangle.Right - radius, rectangle.Y));
                    path.AddArc(new Rectangle(rectangle.Right - lr, rectangle.Y, lr, lr), 270F, 90F);
                    //右
                    path.AddLine(new Point(rectangle.Right, rectangle.Y + radius), new Point(rectangle.Right, rectangle.Bottom - radius));
                    path.AddArc(new Rectangle(rectangle.Right - lr, rectangle.Bottom - lr, lr, lr), 0F, 90F);
                    //下
                    path.AddLine(new Point(rectangle.Right - radius, rectangle.Bottom), new Point(rectangle.X + radius, rectangle.Bottom));
                    path.AddArc(new Rectangle(rectangle.X, rectangle.Bottom - lr, lr, lr), 90F, 90F);
                    //左
                    path.AddLine(new Point(rectangle.X, rectangle.Bottom - radius), new Point(rectangle.X, rectangle.Y + radius));
                    path.AddArc(new Rectangle(rectangle.X, rectangle.Y, lr, lr), 180F, 90F);
                }
                else if (style == RadiusStyle.Left)
                {
                    //上
                    path.AddLine(new Point(rectangle.X + radius, rectangle.Y), new Point(rectangle.Right, rectangle.Y));
                    //右
                    path.AddLine(new Point(rectangle.Right, rectangle.Y), new Point(rectangle.Right, rectangle.Bottom));
                    //下
                    path.AddLine(new Point(rectangle.Right, rectangle.Bottom), new Point(rectangle.X + radius, rectangle.Bottom));
                    path.AddArc(new Rectangle(rectangle.X, rectangle.Bottom - lr, lr, lr), 90F, 90F);
                    //左
                    path.AddLine(new Point(rectangle.X, rectangle.Bottom - radius), new Point(rectangle.X, rectangle.Y + radius));
                    path.AddArc(new Rectangle(rectangle.X, rectangle.Y, lr, lr), 180F, 90F);
                }
                else if (style == RadiusStyle.Top)
                {
                    //上
                    path.AddLine(new Point(rectangle.X + radius, rectangle.Y), new Point(rectangle.Right - radius, rectangle.Y));
                    path.AddArc(new Rectangle(rectangle.Right - lr, rectangle.Y, lr, lr), 270F, 90F);
                    //右
                    path.AddLine(new Point(rectangle.Right, rectangle.Y + radius), new Point(rectangle.Right, rectangle.Bottom));
                    //下
                    path.AddLine(new Point(rectangle.Right, rectangle.Bottom), new Point(rectangle.X, rectangle.Bottom));
                    //左
                    path.AddLine(new Point(rectangle.X, rectangle.Bottom - radius), new Point(rectangle.X, rectangle.Y + radius));
                    path.AddArc(new Rectangle(rectangle.X, rectangle.Y, lr, lr), 180F, 90F);
                }
                else if (style == RadiusStyle.Right)
                {
                    //上
                    path.AddLine(new Point(rectangle.X, rectangle.Y), new Point(rectangle.Right - radius, rectangle.Y));
                    path.AddArc(new Rectangle(rectangle.Right - lr, rectangle.Y, lr, lr), 270F, 90F);
                    //右
                    path.AddLine(new Point(rectangle.Right, rectangle.Y + radius), new Point(rectangle.Right, rectangle.Bottom - radius));
                    path.AddArc(new Rectangle(rectangle.Right - lr, rectangle.Bottom - lr, lr, lr), 0F, 90F);
                    //下
                    path.AddLine(new Point(rectangle.Right - radius, rectangle.Bottom), new Point(rectangle.X, rectangle.Bottom));
                    //左
                    path.AddLine(new Point(rectangle.X, rectangle.Bottom), new Point(rectangle.X, rectangle.Y));
                }
                else if (style == RadiusStyle.Bottom)
                {
                    //上
                    path.AddLine(new Point(rectangle.X, rectangle.Y), new Point(rectangle.Right, rectangle.Y));
                    //右
                    path.AddLine(new Point(rectangle.Right, rectangle.Y), new Point(rectangle.Right, rectangle.Bottom - radius));
                    path.AddArc(new Rectangle(rectangle.Right - lr, rectangle.Bottom - lr, lr, lr), 0F, 90F);
                    //下
                    path.AddLine(new Point(rectangle.Right - radius, rectangle.Bottom), new Point(rectangle.X + radius, rectangle.Bottom));
                    path.AddArc(new Rectangle(rectangle.X, rectangle.Bottom - lr, lr, lr), 90F, 90F);
                    //左
                    path.AddLine(new Point(rectangle.X, rectangle.Bottom - radius), new Point(rectangle.X, rectangle.Y));
                }
                else if (style == RadiusStyle.LeftTop)
                {
                    //上
                    path.AddLine(new Point(rectangle.X + radius, rectangle.Y), new Point(rectangle.Right, rectangle.Y));
                    //右
                    path.AddLine(new Point(rectangle.Right, rectangle.Y), new Point(rectangle.Right, rectangle.Bottom));
                    //下
                    path.AddLine(new Point(rectangle.Right, rectangle.Bottom), new Point(rectangle.X, rectangle.Bottom));
                    //左
                    path.AddLine(new Point(rectangle.X, rectangle.Bottom), new Point(rectangle.X, rectangle.Y + radius));
                    path.AddArc(new Rectangle(rectangle.X, rectangle.Y, lr, lr), 180F, 90F);
                }
                else if (style == RadiusStyle.LeftBottom)
                {
                    //上
                    path.AddLine(new Point(rectangle.X, rectangle.Y), new Point(rectangle.Right, rectangle.Y));
                    //右
                    path.AddLine(new Point(rectangle.Right, rectangle.Y), new Point(rectangle.Right, rectangle.Bottom));
                    //下
                    path.AddLine(new Point(rectangle.Right, rectangle.Bottom), new Point(rectangle.X + radius, rectangle.Bottom));
                    path.AddArc(new Rectangle(rectangle.X, rectangle.Bottom - lr, lr, lr), 90F, 90F);
                    //左
                    path.AddLine(new Point(rectangle.X, rectangle.Bottom - radius), new Point(rectangle.X, rectangle.Y));
                }
                else if (style == RadiusStyle.RightTop)
                {
                    //上
                    path.AddLine(new Point(rectangle.X, rectangle.Y), new Point(rectangle.Right - radius, rectangle.Y));
                    path.AddArc(new Rectangle(rectangle.Right - lr, rectangle.Y, lr, lr), 270F, 90F);
                    //右
                    path.AddLine(new Point(rectangle.Right, rectangle.Y + radius), new Point(rectangle.Right, rectangle.Bottom));
                    //下
                    path.AddLine(new Point(rectangle.Right - radius, rectangle.Bottom), new Point(rectangle.X, rectangle.Bottom));
                    //左
                    path.AddLine(new Point(rectangle.X, rectangle.Bottom), new Point(rectangle.X, rectangle.Y));
                }
                else
                {
                    //上
                    path.AddLine(new Point(rectangle.X, rectangle.Y), new Point(rectangle.Right, rectangle.Y));
                    //右
                    path.AddLine(new Point(rectangle.Right, rectangle.Y), new Point(rectangle.Right, rectangle.Bottom - radius));
                    path.AddArc(new Rectangle(rectangle.Right - lr, rectangle.Bottom - lr, lr, lr), 0F, 90F);
                    //下
                    path.AddLine(new Point(rectangle.Right - radius, rectangle.Bottom), new Point(rectangle.X, rectangle.Bottom));
                    //左
                    path.AddLine(new Point(rectangle.X, rectangle.Bottom), new Point(rectangle.X, rectangle.Y));
                }
                return path;
            }
            catch
            {
                return null;
            }
        }

//导入API函数
        [System.Runtime.InteropServices.DllImport("user32.dll ")]
        static extern IntPtr GetWindowDC(IntPtr hWnd);//返回hWnd参数所指定的窗口的设备环境。
        [System.Runtime.InteropServices.DllImport("user32.dll ")]
        static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); //函数释放设备上下文环境(DC
    }

Panel自定义边框相关推荐

  1. winformbutton边框怎么改_C# WinForm窗体控件Panel修改边框颜色以及边框宽度方法

    C# WinForm窗体控件Panel修改边框颜色以及边框宽度方法 1.新建组件这里可以自定义一个Panel控件起名为PanelEx 2.增加一个BoderColor属性和BoderSize属性 pr ...

  2. Android之EditText自定义边框和边框颜色(转载)

    介绍一种比较常见的用法 第一步:准备两张图片大小一样,颜色不同的图片.图片名称分为:editbox_focus.png和editbox_normal.png 放入工程的drawable文件夹下. 第二 ...

  3. jbutton添加点击事件_electron-vue自定义边框后点击事件失效问题

    问题描述:electron-vue自定义边框后点击事件失效(不单单点击事件,窗体内所有事件都失效) 解决方案:在需要事件处理的节点上添加样式-webkit-app-region:no-drag; sr ...

  4. layui 按钮点击一次后失效_electron-vue自定义边框后点击事件失效问题

    问题描述:electron-vue自定义边框后点击事件失效(不单单点击事件,窗体内所有事件都失效) 解决方案:在需要事件处理的节点上添加样式-webkit-app-region:no-drag; sr ...

  5. android 自定义edittext方框样式,Android之EditText自定义边框和边框颜色(转载)

    介绍一种比较常见的用法 第一步:准备两张图片大小一样,颜色不同的图片.图片名称分为:editbox_focus.png和editbox_normal.png 放入工程的drawable文件夹下. 第二 ...

  6. Android TextView带背景图片和自定义边框

    TextView带背景图片和自定义边框,效果如下图: 页面xml: <android.support.constraint.ConstraintLayout xmlns:android=&quo ...

  7. 自定义边框的制作与换色(canvas与css3)

    1.先谈谈css能实现的自定义边框 (直接上图) 这是一个自定义的边框图.会发现.如果简单的强行当背景图片使用,然后撑满整个元素,在元素宽高比例不同的情况下.会出现图片拉伸情况(这里大家都能想到.我就 ...

  8. 安卓TextView文本框与自定义边框

    常用属性 自定义边框 基本使用 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:and ...

  9. Winform的Panel绘制边框

    给Panel绘制边框能够达到一定的美感,这其实很简单,只需要编写Paint事件即可. private void panel1_Paint(object sender, PaintEventArgs e ...

最新文章

  1. 【智能小车】舵机的基本原理(学习自平衡小车之家的资料)
  2. mysql数据库系统配置文件_跟我学虚拟机系统平台相关技术及应用——在CentOS系统中的MySql数据库系统配置文件中进行配置定义...
  3. 成都python数据分析师职业技能_数据分析师需要什么技能,数据分析行业都有什么职业?...
  4. Confluence5.8部分空间名称显示为问号的解决方案
  5. java getoutputstream_Java Connection.getOutputStream方法代码示例
  6. SQL那些事儿(十四)--C#调用oracle存储过程(查询非查询and有参无参)深度好文
  7. 设置MyEclipse编码、补全快捷键、字体大小
  8. libc库和系统调用
  9. 群晖消息通知 推送服务器,群晖NAS发送钉钉群消息通知教程
  10. SOLIDWORKS工程图导出DWG图纸时图层映射关系
  11. 齐聚静安,共襄盛举--「2020上海静安国际大数据论坛」成功举行
  12. 变量的定义、类型、注释、拼接字符串、选择循环结构、读取文件
  13. java——MP3转wav
  14. webots controller API(C++)
  15. 【Android】使用deviceowner 配置手机设置 (Monkey自动化测试删去状态栏、设定输入法、静音、APP自动权限授予、Kiosk模式打开)
  16. OpenCV入门系列 —— boxFilter盒子滤波
  17. Git本地文件上传到远程仓库
  18. OpenCV+kinect1.0手语识别(二)手部区域的抠图与跟踪
  19. guid主分区表损坏如何处理_电脑GUID格式GPT硬盘的引导如何修复|GUID的GPT硬盘引导损坏了怎么办...
  20. 机器学习:数据预处理之独热编码(One-Hot)详解

热门文章

  1. 【原创】软件测试经验图谱硬技能之业务逻辑
  2. 硬盘为raw格式后的修复
  3. 华为鸿蒙系统推广,未雨绸缪!为更好推广鸿蒙自研系统,华为已提前做了三大准备!...
  4. 批量修改文件后缀名 文件扩展名
  5. 感谢折磨你的人[三]
  6. 游戏辅助制作核心--植物大战僵尸逆向之阳光修改(一)
  7. 检测手机号码是否合法(正则表达式)
  8. 刘润对话卫哲:阿里巴巴唯一生产的产品,就是干部
  9. 【知识兔】会计财务最常用的10个公式函数,必须收藏
  10. BPM:系统集成平台,订单高效协同管理