博客
关于我
C#winform可设置四个角倒角的panel控件
阅读量:544 次
发布时间:2019-03-09

本文共 8094 字,大约阅读时间需要 26 分钟。

using System;using System.ComponentModel;using System.Drawing;using System.Runtime.InteropServices;using System.Windows.Forms;namespace WindowsFormsApp3{    public partial class RoundPanel : Panel    {        [DllImport("user32.dll")]        static extern IntPtr GetWindowDC(IntPtr hwnd);        [DllImport("user32.dll")]        static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);        Color _borderColor = Color.Black;        int _borderWidth = 1;        [Description("组件的边框颜色。"), Category("Appearance")]        public Color BorderColor        {            get            {                return _borderColor;            }            set            {                _borderColor = value;                this.Invalidate();            }        }        [Description("组件的边框宽度。"), Category("Appearance")]        public int BorderWidth        {            get            {                return _borderWidth;            }            set            {                _borderWidth = value;                this.Invalidate();            }        }        Color _currBColor = Color.Empty;        public void SetBackColorImg(Color color)        {            if (_currBColor != color)            {                drawBackColor(100, color);                _currBColor = color;            }        }        public RoundPanel()        {            SetStyle(ControlStyles.DoubleBuffer, true);            SetStyle(ControlStyles.AllPaintingInWmPaint, false);            SetStyle(ControlStyles.ResizeRedraw, true);            SetStyle(ControlStyles.UserPaint, true);            SetStyle(ControlStyles.SupportsTransparentBackColor, true);            this.Padding = new System.Windows.Forms.Padding(0, 0, 0, 0);            this.Margin = new System.Windows.Forms.Padding(0, 0, 0, 0);            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;            this.Paint += new PaintEventHandler(PanelEx_Paint);        }        ///         /// 绘制背景色,以图片填充        ///         void drawBackColor(int percentage, Color drawColor)        {            percentage = Math.Min(percentage, 100);            Bitmap b = new Bitmap(this.Width, this.Height);            Graphics g = Graphics.FromImage(b);            int height = (int)(this.Height * (double)((100 - percentage) / (double)100));            Rectangle rectangle = new Rectangle(new Point(0, height), new Size(this.Width, this.Height));            g.DrawRectangle(new Pen(drawColor), rectangle);            g.FillRectangle(new SolidBrush(drawColor), rectangle);            g.Dispose();            this.BackgroundImage = b;        }        [Browsable(true)]        [Category("外观"), Description("生成该颜色的图片填充背景图片")]        public Color BackImgColor        {            get { return _currBColor; }            set            {                SetBackColorImg(value);            }        }        void PanelEx_Paint(object sender, PaintEventArgs e)        {            if (this.BorderStyle == BorderStyle.FixedSingle)            {                IntPtr hDC = GetWindowDC(this.Handle);                Graphics g = Graphics.FromHdc(hDC);                ControlPaint.DrawBorder(                 g,                 new Rectangle(0, 0, this.Width, this.Height),                 _borderColor,                 _borderWidth,                 ButtonBorderStyle.Solid,                 _borderColor,                 _borderWidth,                 ButtonBorderStyle.Solid,                 _borderColor,                 _borderWidth,                 ButtonBorderStyle.Solid,                 _borderColor,                 _borderWidth,                 ButtonBorderStyle.Solid);                g.Dispose();                ReleaseDC(Handle, hDC);            }        }        //        //解决加载闪烁,背景透明等问题        //        protected override CreateParams CreateParams        {            get            {                var parms = base.CreateParams;                parms.Style &= ~0x02000000;                return parms;            }        }        uint _topLeftRadius;        [Browsable(true)]        [Category("倒角"), Description("左上圆角弧度(0为不要圆角)")]        public uint TopLeftRadius        {            get            {                return _topLeftRadius;            }            set            {                _topLeftRadius = value;                radiusChanged(value);            }        }        uint _topRightRadius;        [Browsable(true)]        [Category("倒角"), Description("右上圆角弧度(0为不要圆角)")]        public uint TopRightRadius        {            get            {                return _topRightRadius;            }            set            {                _topRightRadius = value;                radiusChanged(value);            }        }        uint _bottomLeftRadius;        [Browsable(true)]        [Category("倒角"), Description("左下圆角弧度(0为不要圆角)")]        public uint BottomLeftRadius        {            get            {                return _bottomLeftRadius;            }            set            {                _bottomLeftRadius = value;                radiusChanged(value);            }        }        uint _bottomRightRadius;        [Browsable(true)]        [Category("倒角"), Description("右下圆角弧度(0为不要圆角)")]        public uint BottomRightRadius        {            get            {                return _bottomRightRadius;            }            set            {                _bottomRightRadius = value;                radiusChanged(value);            }        }        void radiusChanged(uint radius)        {            base.Refresh();        }        uint _allRound = 0;        [Browsable(true)]        [Category("倒角"), Description("统一圆角弧度")]        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]        public uint AllRound        {            get { return _allRound; }            set            {                _allRound = value;                if (value != 0)                {                    _topLeftRadius = _allRound;                    _topRightRadius = _allRound;                    _bottomLeftRadius = _allRound;                    _bottomRightRadius = _allRound;                    radiusChanged(value);                }            }        }        // 圆角代码        public void Round(System.Drawing.Region region)        {            System.Drawing.Drawing2D.GraphicsPath oPath = new System.Drawing.Drawing2D.GraphicsPath();             int x = 0;            int y = 0;            int thisWidth = this.Width;            int thisHeight = this.Height;            if (_topLeftRadius > 0)            {                oPath.AddArc(x, y, _topLeftRadius, _topLeftRadius, 180, 90);                                 // 左上角            }            oPath.AddLine(x + _topLeftRadius, y, thisWidth - _topRightRadius, y);                         // 顶端            if (_topRightRadius > 0)            {                oPath.AddArc(thisWidth - _topRightRadius, y, _topRightRadius, _topRightRadius, 270, 90);                 // 右上角            }            oPath.AddLine(thisWidth, y + _topRightRadius, thisWidth, thisHeight - _bottomRightRadius);        // 右边            if (_bottomRightRadius > 0)            {                oPath.AddArc(thisWidth - _bottomRightRadius, thisHeight - _bottomRightRadius, _bottomRightRadius, _bottomRightRadius, 0, 90);  // 右下角            }            oPath.AddLine(thisWidth - _bottomRightRadius, thisHeight, x + _bottomLeftRadius, thisHeight);       // 底边            if (_bottomLeftRadius > 0)            {                oPath.AddArc(x, thisHeight - _bottomLeftRadius, _bottomLeftRadius, _bottomLeftRadius, 90, 90);                 // 左下角            }            oPath.AddLine(x, thisHeight - _bottomLeftRadius, x, y + _topLeftRadius);                        // 左边            oPath.CloseAllFigures();            this.Region = new System.Drawing.Region(oPath);        }        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)        {            base.OnPaint(e);            Round(this.Region);  // 圆角        }        protected override void OnResize(EventArgs eventargs)        {            base.OnResize(eventargs);            base.Refresh();        }    }}

 

转载地址:http://ihksz.baihongyu.com/

你可能感兴趣的文章
Nginx安装SSL模块 nginx: the “ssl” parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx
查看>>
nginx安装stream模块配置tcp/udp端口转发
查看>>
nginx安装Stream模块配置tcp/udp端口转发
查看>>
Nginx安装与常见命令
查看>>
nginx安装与配置
查看>>
【Flink】Flink 2023 Flink 到 Doris 实时写入实践
查看>>
Nginx安装及配置详解
查看>>
nginx安装并配置实现端口转发
查看>>
nginx安装配置
查看>>
Nginx实战之1.1-1.6 Nginx介绍,安装及配置文件详解
查看>>
Nginx实战经验分享:从小白到专家的成长历程!
查看>>
nginx实现二级域名转发
查看>>
Nginx实现动静分离
查看>>
Nginx实现反向代理负载均衡
查看>>
nginx实现负载均衡
查看>>
Nginx实现限流
查看>>
Nginx将https重定向为http进行访问的配置(附Demo)
查看>>
nginx工作笔记004---配置https_ssl证书_视频服务器接口等
查看>>
nginx工作笔记005---nginx配置负载均衡_在微服务中实现网关集群_实现TCP传输层协议__http协议的负载均衡
查看>>
nginx常用命令及简单配置
查看>>