C#重写控件之按钮和checkBox

  先上代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace ControlPaint
{
    public partial class ControlButton :Button
    {
       public ControlButton()
        {
            this.SetStyle(
                ControlStyles.UserPaint|
                ControlStyles.AllPaintingInWmPaint|
                ControlStyles.OptimizedDoubleBuffer|
                ControlStyles.ResizeRedraw|
                ControlStyles.SupportsTransparentBackColor,true);

            FlatStyle = FlatStyle.Flat;
            FlatAppearance.BorderSize = 1;
            FlatAppearance.BorderColor = Color.FromArgb(102, 102, 102);
            FlatAppearance.MouseOverBackColor = Color.FromArgb(102, 102, 102);
            FlatAppearance.MouseDownBackColor = Color.FromArgb(150, 150, 150);
        }
       private bool _isClicked = false;
       public bool IsClicked
       {
           get { return _isClicked; }
           set { _isClicked = value; }
       }

       private bool _inClient = false;
       private bool _mouseDown = false;

       private bool _enableClicked = false;
       [Category("自定义属性"), Description("启用按下状态")]
       public bool EnableClicked
       {
           get { return _enableClicked; }
           set { _enableClicked = value; }
       }

       private Color _fontColor = Color.Gainsboro;
       [Category("自定义属性"),Description("按下状态字体颜色")]
       
        public Color ClickedForeColor
       {
           get { return _fontColor; }
           set { _fontColor = value; }
       }

       private Size _imgSize;
       [Category("自定义属性"), Description("图片尺寸")]
       public Size ImageSize
       {
           get { return _imgSize; }
           set
           {
               _imgSize = value;
               Invalidate();
           }
       }

       private Image _enterImage = null;
       [Category("自定义属性"),Description("鼠标悬浮时图片")]
       
        public Image EnterImage
        {
           get { return _enterImage; }
           set
           {
               _enterImage = value;
           }
         }
       private Image _downImage = null;
       [Category("自定义属性"), Description("鼠标按下时图片")]
       public Image DownImage
       {
           get { return _downImage; }
           set { _downImage = value; }
       }


       protected override void OnMouseDown(MouseEventArgs mevent)
       {
           IsClicked = !IsClicked;
           _mouseDown = true;
           base.OnMouseDown(mevent);
       }

       protected override void OnMouseUp(MouseEventArgs mevent)
       {
           _mouseDown = false;
           base.OnMouseUp(mevent);
       }

       protected override void OnMouseEnter(EventArgs e)
       {
           _inClient = true;
           base.OnMouseEnter(e);
       }

       protected override void OnMouseLeave(EventArgs e)
       {
           _inClient = false;
           base.OnMouseLeave(e);
       }
    }
}

        C# winform自带的界面很丑陋,所以才会想要去重绘。那么这种情况人家早已想到,所以winform中的控件基类Control类中,把许多方法定义成虚方法,我们重写就可以了。

   忽然感觉没什么好写的了,这里还是强调一下,重写控件,最好自己单独建一个组件。

猜你喜欢

转载自blog.csdn.net/qq_33324878/article/details/81065310