博客
关于我
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/

你可能感兴趣的文章
MySQL Server 5.5安装记录
查看>>
mysql server has gone away
查看>>
mysql slave 停了_slave 停止。求解决方法
查看>>
MySQL SQL 优化指南:主键、ORDER BY、GROUP BY 和 UPDATE 优化详解
查看>>
MYSQL sql语句针对数据记录时间范围查询的效率对比
查看>>
mysql sum 没返回,如果没有找到任何值,我如何在MySQL中获得SUM函数以返回'0'?
查看>>
mysql Timestamp时间隔了8小时
查看>>
Mysql tinyint(1)与tinyint(4)的区别
查看>>
mysql union orderby 无效
查看>>
mysql v$session_Oracle 进程查看v$session
查看>>
mysql where中如何判断不为空
查看>>
MySQL Workbench 使用手册:从入门到精通
查看>>
mysql workbench6.3.5_MySQL Workbench
查看>>
MySQL Workbench安装教程以及菜单汉化
查看>>
MySQL Xtrabackup 安装、备份、恢复
查看>>
mysql [Err] 1436 - Thread stack overrun: 129464 bytes used of a 286720 byte stack, and 160000 bytes
查看>>
MySQL _ MySQL常用操作
查看>>
MySQL – 导出数据成csv
查看>>
MySQL —— 在CentOS9下安装MySQL
查看>>
MySQL —— 视图
查看>>