欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Winform自定义Button控件

程序员文章站 2022-03-04 11:10:20
...

Winform自定义Button控件

public partial class CircleTool : UserControl
    {
        [Browsable(true)]
        [Description("边框主题色"), Category("自定义属性")]//DefaultValue("属性默认值,重置时使用")
        public Color FrameColor 
        {
            get { return this.BackColor; }
            set { this.BackColor = value; }
        }
        [Browsable(true)]
        [Description("圆的直径"), Category("自定义属性")]
        public int CircleRadius
        {
            get { return this.lb_cir.Height > this.lb_cir.Width ? this.lb_cir.Width : this.lb_cir.Height; }
            set {
                this.lb_cir.Height = value;
                this.lb_cir.Width = value;
            }
        }
        [Browsable(true)]
        [Description("圆的开始位置坐标"), Category("自定义属性")]
        public Point CircleLocation
        {
            get { return this.lb_cir.Location; }
            set
            {
                this.lb_cir.Location = value;
            }
        }

        [Browsable(true)]
        [Description("圆的颜色"), Category("自定义属性")]
        public Color CircleColor
        {
            get { return _circleColor; }
            set
            {
                this._circleColor = value;
            }
        }

        [Browsable(true)]
        [Description("控件标题"), Category("自定义属性")]
        public string CircleTitle
        {
            get { return this.lb_title.Text; }
            set
            {
                this.lb_title.Text = value;
            }
        }

        [Browsable(true)]
        [Description("控件标题高度"), Category("自定义属性")]
        public Size CircleTitleHeight
        {
            get { return this.lb_title.Size; }
            set
            {
                this.lb_title.Size = value;
            }
        }

        [Browsable(true)]
        [Description("圆形中的图像"), Category("自定义属性")]
        public Image CircleImage
        {
            get { return this.lb_cir.Image; }
            set
            {
                this.lb_cir.Image = value;
            }
        }


        [Browsable(true)]
        [Description("圆点击事件"), Category("自定义属性")]
        public event EventHandler BtnTestClick;


        private Color _circleColor = Color.Green;
        public CircleTool()
        {
            InitializeComponent();
        }

        private void lb_cir_Paint(object sender, PaintEventArgs e)
        {
            try 
            {
                Graphics grapics = e.Graphics;
                grapics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                Brush bush = new SolidBrush(_circleColor);//填充的颜色
                int radius = this.lb_cir.Height > this.lb_cir.Width ? this.lb_cir.Width : this.lb_cir.Height;
                grapics.FillEllipse(bush, 0, 0, radius, radius);//画填充椭圆的方法,x坐标、y坐标、宽、高,如果是100,则半径为50
                Image image = this.lb_cir.Image;//global::Testdelegate.Properties.Resources.官方版本;
                //Rect rect = new Ract()
                int inmagestart_x = radius / 2 - image.Width / 2;
                int inmagestart_y = radius / 2 - image.Height / 2;
                grapics.DrawImage(image, inmagestart_x, inmagestart_y, image.Width, image.Height);
            }
            catch { }
           
        }

        private void lb_cir_Click(object sender, EventArgs e)
        {
            try { BtnTestClick(sender, e); }
            catch { }
            
        }

        private void lb_title_Click(object sender, EventArgs e)
        {
            try { BtnTestClick(sender, e); }
            catch { }
        }
        private void lb_cir_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default; 
        }

        private void lb_cir_MouseMove(object sender, MouseEventArgs e)
        {
            this.Cursor = Cursors.Hand;  
        }
    }

效果:
Winform自定义Button控件