ButtonBase.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. using PaintDotNet.SystemLayer;
  2. using System;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using System.Windows.Forms.VisualStyles;
  6. namespace PaintDotNet
  7. {
  8. /// <summary>
  9. /// This class provides the logic for handling input and managing rendering
  10. /// states for a typical button type control. Just provide the rendering code!
  11. /// </summary>
  12. public abstract class ButtonBase
  13. : Control,
  14. IButtonControl
  15. {
  16. private bool isDefault = false;
  17. private bool drawPressed = false;
  18. private bool drawHover = false;
  19. private DialogResult dialogResult = DialogResult.None;
  20. public event EventHandler DialogResultChanged;
  21. protected virtual void OnDialogResultChanged()
  22. {
  23. if (DialogResultChanged != null)
  24. {
  25. DialogResultChanged(this, EventArgs.Empty);
  26. }
  27. }
  28. public DialogResult DialogResult
  29. {
  30. get
  31. {
  32. return this.dialogResult;
  33. }
  34. set
  35. {
  36. if (this.dialogResult != value)
  37. {
  38. this.dialogResult = value;
  39. OnDialogResultChanged();
  40. }
  41. }
  42. }
  43. public event EventHandler IsDefaultChanged;
  44. protected virtual void OnIsDefaultChanged()
  45. {
  46. if (IsDefaultChanged != null)
  47. {
  48. IsDefaultChanged(this, EventArgs.Empty);
  49. }
  50. }
  51. public bool IsDefault
  52. {
  53. get
  54. {
  55. return this.isDefault;
  56. }
  57. }
  58. protected internal ButtonBase()
  59. {
  60. UI.InitScaling(this);
  61. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  62. SetStyle(ControlStyles.ResizeRedraw, true);
  63. SetStyle(ControlStyles.Selectable, true);
  64. SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  65. SetStyle(ControlStyles.UserPaint, true);
  66. SetStyle(ControlStyles.StandardDoubleClick, false);
  67. InitializeComponent();
  68. }
  69. private void InitializeComponent()
  70. {
  71. this.AccessibleRole = AccessibleRole.PushButton;
  72. this.Name = "ButtonBase";
  73. this.DoubleBuffered = true;
  74. this.TabStop = true;
  75. }
  76. public void NotifyDefault(bool value)
  77. {
  78. if (this.isDefault != value)
  79. {
  80. this.isDefault = value;
  81. OnIsDefaultChanged();
  82. Invalidate(true);
  83. }
  84. }
  85. public void PerformClick()
  86. {
  87. OnClick(EventArgs.Empty);
  88. }
  89. private bool ContainsMouseCursor
  90. {
  91. get
  92. {
  93. Point mousePt = Control.MousePosition;
  94. Rectangle screenRect = this.RectangleToScreen(ClientRectangle);
  95. return screenRect.Contains(mousePt);
  96. }
  97. }
  98. protected override void OnPaint(PaintEventArgs e)
  99. {
  100. PushButtonState state;
  101. if (!Enabled)
  102. {
  103. state = PushButtonState.Disabled;
  104. }
  105. else if (this.drawPressed && ContainsMouseCursor)
  106. {
  107. state = PushButtonState.Pressed;
  108. }
  109. else if (this.drawHover)
  110. {
  111. state = PushButtonState.Hot;
  112. }
  113. else if (IsDefault)
  114. {
  115. state = PushButtonState.Default;
  116. }
  117. else
  118. {
  119. state = PushButtonState.Normal;
  120. }
  121. bool drawFocusCues = ShowFocusCues && Focused;
  122. bool drawKeyboardCues = ShowKeyboardCues;
  123. OnPaintButton(e.Graphics, state, drawFocusCues, drawKeyboardCues);
  124. base.OnPaint(e);
  125. }
  126. protected abstract void OnPaintButton(
  127. Graphics g,
  128. PushButtonState buttonState,
  129. bool drawFocusCues,
  130. bool drawKeyboardCues);
  131. protected override void OnEnabledChanged(EventArgs e)
  132. {
  133. Invalidate(true);
  134. base.OnEnabledChanged(e);
  135. }
  136. protected override void OnMouseEnter(EventArgs e)
  137. {
  138. this.drawHover = true;
  139. Invalidate(true);
  140. Update();
  141. base.OnMouseEnter(e);
  142. }
  143. protected override void OnMouseDown(MouseEventArgs mevent)
  144. {
  145. this.drawPressed = true;
  146. Invalidate(true);
  147. base.OnMouseDown(mevent);
  148. }
  149. protected override void OnMouseMove(MouseEventArgs mevent)
  150. {
  151. Invalidate(true);
  152. base.OnMouseMove(mevent);
  153. }
  154. protected override void OnMouseUp(MouseEventArgs mevent)
  155. {
  156. this.drawPressed = false;
  157. Invalidate(true);
  158. base.OnMouseUp(mevent);
  159. }
  160. protected override void OnMouseLeave(EventArgs e)
  161. {
  162. this.drawHover = false;
  163. Invalidate(true);
  164. Update();
  165. base.OnMouseLeave(e);
  166. }
  167. protected override void OnGotFocus(EventArgs e)
  168. {
  169. Invalidate(true);
  170. base.OnGotFocus(e);
  171. }
  172. protected override void OnLostFocus(EventArgs e)
  173. {
  174. this.drawPressed = false;
  175. Invalidate(true);
  176. base.OnLostFocus(e);
  177. }
  178. public int keyCode = -1; //0-±£´æ£¬1-²»±£´æ
  179. protected override void OnKeyDown(KeyEventArgs e)
  180. {
  181. if (e.KeyCode == Keys.Space)
  182. {
  183. this.drawPressed = true;
  184. Refresh();
  185. }
  186. else if (e.KeyCode == Keys.Y)
  187. {
  188. keyCode = 0;
  189. this.drawPressed = true;
  190. Refresh();
  191. PerformClick();
  192. }
  193. else if (e.KeyCode == Keys.N)
  194. {
  195. keyCode = 1;
  196. this.drawPressed = true;
  197. Refresh();
  198. PerformClick();
  199. }
  200. base.OnKeyDown(e);
  201. }
  202. protected override void OnKeyUp(KeyEventArgs e)
  203. {
  204. if (e.KeyCode == Keys.Space)
  205. {
  206. this.drawPressed = false;
  207. Refresh();
  208. PerformClick();
  209. }
  210. base.OnKeyUp(e);
  211. }
  212. protected override bool ProcessMnemonic(char charCode)
  213. {
  214. if (CanSelect && IsMnemonic(charCode, this.Text))
  215. {
  216. OnClick(EventArgs.Empty);
  217. }
  218. return base.ProcessMnemonic(charCode);
  219. }
  220. }
  221. }