ButtonBase.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using SmartCoalApplication.SystemLayer;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. using System.Windows.Forms.VisualStyles;
  10. namespace SmartCoalApplication.Core
  11. {
  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 graphics,
  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. protected override void OnKeyDown(KeyEventArgs e)
  179. {
  180. if (e.KeyCode == Keys.Space)
  181. {
  182. this.drawPressed = true;
  183. Refresh();
  184. }
  185. base.OnKeyDown(e);
  186. }
  187. protected override void OnKeyUp(KeyEventArgs e)
  188. {
  189. if (e.KeyCode == Keys.Space)
  190. {
  191. this.drawPressed = false;
  192. Refresh();
  193. PerformClick();
  194. }
  195. base.OnKeyUp(e);
  196. }
  197. protected override bool ProcessMnemonic(char charCode)
  198. {
  199. if (CanSelect && IsMnemonic(charCode, this.Text))
  200. {
  201. OnClick(EventArgs.Empty);
  202. }
  203. return base.ProcessMnemonic(charCode);
  204. }
  205. }
  206. }