Winform中创建自定义控件

    public partial class Label4L1 : PictureBox
    {
        [Browsable(true), Description("鼠标悬浮,填充图片")]
        [Category("Appearance")]
        public Image HoverImage { get; set; }

        public Label4L1()
        {
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.UserPaint, true);
            InitializeComponent();
        }

        //protected override void OnPaint(PaintEventArgs pe)
        //{            
        //    base.OnPaint(pe);   
        //}

        /// <summary>
        /// 鼠标进入按钮
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseEnter(EventArgs e)
        {
            this.BackgroundImage = HoverImage;
            base.OnMouseEnter(e);
        }

        /// <summary>
        /// 鼠标离开控件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeave(EventArgs e)
        {
            this.BackgroundImage = null;
            base.OnMouseLeave(e);
        }
    }

 

posted @ 2021-03-12 10:11  水墨晨诗  阅读(437)  评论(0编辑  收藏  举报