123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- using PaintDotNet.SystemLayer;
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- using System.Windows.Forms.VisualStyles;
- namespace PaintDotNet
- {
- /// <summary>
- /// This class provides the logic for handling input and managing rendering
- /// states for a typical button type control. Just provide the rendering code!
- /// </summary>
- public abstract class ButtonBase
- : Control,
- IButtonControl
- {
- private bool isDefault = false;
- private bool drawPressed = false;
- private bool drawHover = false;
- private DialogResult dialogResult = DialogResult.None;
- public event EventHandler DialogResultChanged;
- protected virtual void OnDialogResultChanged()
- {
- if (DialogResultChanged != null)
- {
- DialogResultChanged(this, EventArgs.Empty);
- }
- }
- public DialogResult DialogResult
- {
- get
- {
- return this.dialogResult;
- }
- set
- {
- if (this.dialogResult != value)
- {
- this.dialogResult = value;
- OnDialogResultChanged();
- }
- }
- }
- public event EventHandler IsDefaultChanged;
- protected virtual void OnIsDefaultChanged()
- {
- if (IsDefaultChanged != null)
- {
- IsDefaultChanged(this, EventArgs.Empty);
- }
- }
- public bool IsDefault
- {
- get
- {
- return this.isDefault;
- }
- }
- protected internal ButtonBase()
- {
- UI.InitScaling(this);
- SetStyle(ControlStyles.AllPaintingInWmPaint, true);
- SetStyle(ControlStyles.ResizeRedraw, true);
- SetStyle(ControlStyles.Selectable, true);
- SetStyle(ControlStyles.SupportsTransparentBackColor, true);
- SetStyle(ControlStyles.UserPaint, true);
- SetStyle(ControlStyles.StandardDoubleClick, false);
- InitializeComponent();
- }
- private void InitializeComponent()
- {
- this.AccessibleRole = AccessibleRole.PushButton;
- this.Name = "ButtonBase";
- this.DoubleBuffered = true;
- this.TabStop = true;
- }
- public void NotifyDefault(bool value)
- {
- if (this.isDefault != value)
- {
- this.isDefault = value;
- OnIsDefaultChanged();
- Invalidate(true);
- }
- }
- public void PerformClick()
- {
- OnClick(EventArgs.Empty);
- }
- private bool ContainsMouseCursor
- {
- get
- {
- Point mousePt = Control.MousePosition;
- Rectangle screenRect = this.RectangleToScreen(ClientRectangle);
- return screenRect.Contains(mousePt);
- }
- }
- protected override void OnPaint(PaintEventArgs e)
- {
- PushButtonState state;
- if (!Enabled)
- {
- state = PushButtonState.Disabled;
- }
- else if (this.drawPressed && ContainsMouseCursor)
- {
- state = PushButtonState.Pressed;
- }
- else if (this.drawHover)
- {
- state = PushButtonState.Hot;
- }
- else if (IsDefault)
- {
- state = PushButtonState.Default;
- }
- else
- {
- state = PushButtonState.Normal;
- }
- bool drawFocusCues = ShowFocusCues && Focused;
- bool drawKeyboardCues = ShowKeyboardCues;
- OnPaintButton(e.Graphics, state, drawFocusCues, drawKeyboardCues);
- base.OnPaint(e);
- }
- protected abstract void OnPaintButton(
- Graphics g,
- PushButtonState buttonState,
- bool drawFocusCues,
- bool drawKeyboardCues);
- protected override void OnEnabledChanged(EventArgs e)
- {
- Invalidate(true);
- base.OnEnabledChanged(e);
- }
- protected override void OnMouseEnter(EventArgs e)
- {
- this.drawHover = true;
- Invalidate(true);
- Update();
- base.OnMouseEnter(e);
- }
- protected override void OnMouseDown(MouseEventArgs mevent)
- {
- this.drawPressed = true;
- Invalidate(true);
- base.OnMouseDown(mevent);
- }
- protected override void OnMouseMove(MouseEventArgs mevent)
- {
- Invalidate(true);
- base.OnMouseMove(mevent);
- }
- protected override void OnMouseUp(MouseEventArgs mevent)
- {
- this.drawPressed = false;
- Invalidate(true);
- base.OnMouseUp(mevent);
- }
- protected override void OnMouseLeave(EventArgs e)
- {
- this.drawHover = false;
- Invalidate(true);
- Update();
- base.OnMouseLeave(e);
- }
- protected override void OnGotFocus(EventArgs e)
- {
- Invalidate(true);
- base.OnGotFocus(e);
- }
- protected override void OnLostFocus(EventArgs e)
- {
- this.drawPressed = false;
- Invalidate(true);
- base.OnLostFocus(e);
- }
- public int keyCode = -1; //0-±£´æ£¬1-²»±£´æ
- protected override void OnKeyDown(KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Space)
- {
- this.drawPressed = true;
- Refresh();
- }
- else if (e.KeyCode == Keys.Y)
- {
- keyCode = 0;
- this.drawPressed = true;
- Refresh();
- PerformClick();
- }
- else if (e.KeyCode == Keys.N)
- {
- keyCode = 1;
- this.drawPressed = true;
- Refresh();
- PerformClick();
- }
- base.OnKeyDown(e);
- }
- protected override void OnKeyUp(KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Space)
- {
- this.drawPressed = false;
- Refresh();
- PerformClick();
- }
- base.OnKeyUp(e);
- }
- protected override bool ProcessMnemonic(char charCode)
- {
- if (CanSelect && IsMnemonic(charCode, this.Text))
- {
- OnClick(EventArgs.Empty);
- }
- return base.ProcessMnemonic(charCode);
- }
- }
- }
|