protected override void OnMouseEnter(EventArgs e)
 {
     base.OnMouseEnter(e);
     if (this.Enabled && !this.keyPressed)
     {
         this.drawState = ButtonDrawState.Hot;
         Invalidate();
     }
 }
 protected override void OnLostFocus(EventArgs e)
 {
     base.OnLostFocus(e);
     if (this.Enabled)
     {
         this.drawState = ButtonDrawState.Normal;
         Invalidate();
     }
 }
 protected override void OnEnabledChanged(EventArgs e)
 {
     base.OnEnabledChanged(e);
     if (this.Enabled)
     {
         this.drawState = ButtonDrawState.Normal;
     }
     else
     {
         this.drawState = ButtonDrawState.Disabled;
     }
     Invalidate();
 }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            if (this.Enabled)
            {
                if (this.drawState == ButtonDrawState.Pressed)
                {
                    OnClick(EventArgs.Empty);
                }

                this.drawState = ButtonDrawState.Focused;
                Invalidate();
            }
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (!this.Enabled)
            {
                return;
            }

            if (e.Button == MouseButtons.Left)
            {
                this.drawState = ButtonDrawState.Pressed;
                Invalidate();
            }
        }
 protected override void OnKeyUp(KeyEventArgs e)
 {
     base.OnKeyUp(e);
     if (e.KeyCode == Keys.Space)
     {
         this.keyPressed = false;
         this.drawState  = ButtonDrawState.Focused;
         Invalidate();
         OnClick(EventArgs.Empty);
     }
     else if (e.KeyCode == Keys.Enter)
     {
         OnClick(EventArgs.Empty);
     }
 }
        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (!this.Enabled)
            {
                return;
            }

            if (e.KeyCode == Keys.Space)
            {
                this.keyPressed = true;
                this.drawState  = ButtonDrawState.Pressed;
                Invalidate();
            }
        }
 protected override void OnMouseLeave(EventArgs e)
 {
     base.OnMouseLeave(e);
     if (this.Enabled && !this.keyPressed)
     {
         if (this.IsDefault)
         {
             this.drawState = ButtonDrawState.Focused;
         }
         else
         {
             this.drawState = ButtonDrawState.Normal;
         }
         Invalidate();
     }
 }
        public ImageButton()
        {
            SetStyle(ControlStyles.Selectable |
                     ControlStyles.StandardClick |
                     ControlStyles.ResizeRedraw |
                     ControlStyles.AllPaintingInWmPaint |
                     ControlStyles.DoubleBuffer |
                     ControlStyles.OptimizedDoubleBuffer |
                     ControlStyles.UserPaint,
                     true);


            // This call is required by the Windows.Forms Form Designer.
            InitializeComponent();
            this.drawState       = ButtonDrawState.Normal;
            this.backgroundBrush = new SolidBrush(this.BackColor);
            this.hotTrackBrush   = new SolidBrush(Color.FromArgb(128, SystemColors.HotTrack));
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (!this.Enabled)
            {
                return;
            }

            if (e.Button == MouseButtons.Left && this.Bounds.Contains(e.X, e.Y))
            {
                this.drawState = ButtonDrawState.Pressed;
            }
            else
            {
                if (this.keyPressed)
                {
                    return;
                }

                this.drawState = ButtonDrawState.Hot;
            }
            Invalidate();
        }