CommandButton.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. using SmartCoalApplication.SystemLayer;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Drawing.Text;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using System.Windows.Forms.VisualStyles;
  13. namespace SmartCoalApplication.Core
  14. {
  15. public sealed class CommandButton
  16. : ButtonBase
  17. {
  18. private Font actionTextFont;
  19. private string actionText;
  20. private Font explanationTextFont;
  21. private string explanationText;
  22. private Image actionImage;
  23. private Image actionImageDisabled;
  24. [Browsable(true)]
  25. [EditorBrowsable(EditorBrowsableState.Always)]
  26. [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
  27. public override bool AutoSize
  28. {
  29. get
  30. {
  31. return base.AutoSize;
  32. }
  33. set
  34. {
  35. base.AutoSize = value;
  36. PerformLayout();
  37. Invalidate(true);
  38. }
  39. }
  40. public string ActionText
  41. {
  42. get
  43. {
  44. return this.actionText;
  45. }
  46. set
  47. {
  48. if (this.actionText != value)
  49. {
  50. this.actionText = value;
  51. this.Text = value; // ensure that mnemonics get processed correctly
  52. PerformLayout();
  53. Invalidate(true);
  54. }
  55. }
  56. }
  57. public string ExplanationText
  58. {
  59. get
  60. {
  61. return this.explanationText;
  62. }
  63. set
  64. {
  65. if (this.explanationText != value)
  66. {
  67. this.explanationText = value;
  68. PerformLayout();
  69. Invalidate(true);
  70. }
  71. }
  72. }
  73. public Image ActionImage
  74. {
  75. get
  76. {
  77. return this.actionImage;
  78. }
  79. set
  80. {
  81. if (this.actionImage != null)
  82. {
  83. this.actionImageDisabled.Dispose();
  84. this.actionImageDisabled = null;
  85. this.actionImage.Dispose();
  86. this.actionImage = null;
  87. }
  88. if (value != null)
  89. {
  90. this.actionImage = value;
  91. this.actionImageDisabled = ToolStripRenderer.CreateDisabledImage(this.actionImage);
  92. }
  93. PerformLayout();
  94. Invalidate(true);
  95. }
  96. }
  97. public CommandButton()
  98. {
  99. InitializeComponent();
  100. this.actionTextFont = new Font(this.Font.FontFamily, this.Font.Size * 1.25f, this.Font.Style);
  101. this.explanationTextFont = this.Font;
  102. }
  103. protected override void OnPaintButton(Graphics g, PushButtonState state, bool drawFocusCues, bool drawKeyboardCues)
  104. {
  105. MeasureAndDraw(g, true, state, drawFocusCues, drawKeyboardCues);
  106. }
  107. private Size MeasureAndDraw(Graphics g, bool enableDrawing, PushButtonState state, bool drawFocusCues, bool drawKeyboardCues)
  108. {
  109. if (enableDrawing)
  110. {
  111. g.PixelOffsetMode = PixelOffsetMode.Half;
  112. g.CompositingMode = CompositingMode.SourceOver;
  113. g.InterpolationMode = InterpolationMode.Bilinear;
  114. }
  115. int marginX = UI.ScaleWidth(9);
  116. int marginYTop = UI.ScaleHeight(8);
  117. int marginYBottom = UI.ScaleHeight(9);
  118. int paddingX = UI.ScaleWidth(8);
  119. int paddingY = UI.ScaleHeight(3);
  120. int offsetX = 0;
  121. int offsetY = 0;
  122. bool drawAsDefault = (state == PushButtonState.Default);
  123. if (enableDrawing)
  124. {
  125. using (Brush backBrush = new SolidBrush(this.BackColor))
  126. {
  127. CompositingMode oldCM = g.CompositingMode;
  128. g.CompositingMode = CompositingMode.SourceCopy;
  129. g.FillRectangle(backBrush, ClientRectangle);
  130. g.CompositingMode = oldCM;
  131. }
  132. Rectangle ourRect = new Rectangle(0, 0, ClientSize.Width, ClientSize.Height);
  133. if (state == PushButtonState.Pressed)
  134. {
  135. offsetX = 1;
  136. offsetY = 1;
  137. }
  138. UI.DrawCommandButton(g, state, ourRect, BackColor, this);
  139. }
  140. Rectangle actionImageRect;
  141. Brush textBrush = new SolidBrush(SystemColors.WindowText);
  142. if (this.actionImage == null)
  143. {
  144. actionImageRect = new Rectangle(offsetX, offsetY + marginYTop, 0, 0);
  145. }
  146. else
  147. {
  148. actionImageRect = new Rectangle(offsetX + marginX, offsetY + marginYTop,
  149. UI.ScaleWidth(this.actionImage.Width), UI.ScaleHeight(this.actionImage.Height));
  150. Rectangle srcRect = new Rectangle(0, 0, this.actionImage.Width, this.actionImage.Height);
  151. if (enableDrawing)
  152. {
  153. Image drawMe = Enabled ? this.actionImage : this.actionImageDisabled;
  154. if (Enabled)
  155. {
  156. actionImageRect.Y += 3;
  157. actionImageRect.X += 1;
  158. g.DrawImage(this.actionImageDisabled, actionImageRect, srcRect, GraphicsUnit.Pixel);
  159. actionImageRect.X -= 1;
  160. actionImageRect.Y -= 3;
  161. }
  162. actionImageRect.Y += 2;
  163. g.DrawImage(drawMe, actionImageRect, srcRect, GraphicsUnit.Pixel);
  164. actionImageRect.Y -= 2;
  165. }
  166. }
  167. int actionTextX = actionImageRect.Right + paddingX;
  168. int actionTextY = actionImageRect.Top;
  169. int actionTextWidth = ClientSize.Width - actionTextX - marginX + offsetX;
  170. StringFormat stringFormat = (StringFormat)StringFormat.GenericTypographic.Clone();
  171. stringFormat.HotkeyPrefix = drawKeyboardCues ? HotkeyPrefix.Show : HotkeyPrefix.Hide;
  172. SizeF actionTextSize = g.MeasureString(this.actionText, this.actionTextFont, actionTextWidth, stringFormat);
  173. Rectangle actionTextRect = new Rectangle(actionTextX, actionTextY,
  174. actionTextWidth, (int)Math.Ceiling(actionTextSize.Height));
  175. if (enableDrawing)
  176. {
  177. if (state == PushButtonState.Disabled)
  178. {
  179. ControlPaint.DrawStringDisabled(g, this.actionText, this.actionTextFont, this.BackColor, actionTextRect, stringFormat);
  180. }
  181. else
  182. {
  183. g.DrawString(this.actionText, this.actionTextFont, textBrush, actionTextRect, stringFormat);
  184. }
  185. }
  186. int descriptionTextX = actionTextX;
  187. int descriptionTextY = actionTextRect.Bottom + paddingY;
  188. int descriptionTextWidth = actionTextWidth;
  189. SizeF descriptionTextSize = g.MeasureString(this.explanationText, this.explanationTextFont,
  190. descriptionTextWidth, stringFormat);
  191. Rectangle descriptionTextRect = new Rectangle(descriptionTextX, descriptionTextY,
  192. descriptionTextWidth, (int)Math.Ceiling(descriptionTextSize.Height));
  193. if (enableDrawing)
  194. {
  195. if (state == PushButtonState.Disabled)
  196. {
  197. ControlPaint.DrawStringDisabled(g, this.explanationText, this.explanationTextFont, this.BackColor, descriptionTextRect, stringFormat);
  198. }
  199. else
  200. {
  201. g.DrawString(this.explanationText, this.explanationTextFont, textBrush, descriptionTextRect, stringFormat);
  202. }
  203. }
  204. if (enableDrawing)
  205. {
  206. if (drawFocusCues)
  207. {
  208. ControlPaint.DrawFocusRectangle(g, new Rectangle(3, 3, ClientSize.Width - 5, ClientSize.Height - 5));
  209. }
  210. }
  211. if (textBrush != null)
  212. {
  213. textBrush.Dispose();
  214. textBrush = null;
  215. }
  216. stringFormat.Dispose();
  217. stringFormat = null;
  218. Size layoutSize = new Size(ClientSize.Width, descriptionTextRect.Bottom + marginYBottom);
  219. return layoutSize;
  220. }
  221. protected override void OnLayout(LayoutEventArgs levent)
  222. {
  223. if (AutoSize)
  224. {
  225. Size layoutSize;
  226. using (Graphics g = CreateGraphics())
  227. {
  228. layoutSize = MeasureAndDraw(g, false, PushButtonState.Normal, false, false);
  229. }
  230. this.ClientSize = layoutSize;
  231. }
  232. base.OnLayout(levent);
  233. }
  234. /// <summary>
  235. /// Required method for Designer support - do not modify
  236. /// the contents of this method with the code editor.
  237. /// </summary>
  238. private void InitializeComponent()
  239. {
  240. this.AccessibleRole = AccessibleRole.PushButton;
  241. this.TabStop = true;
  242. this.DoubleBuffered = true;
  243. this.Name = "CommandButton";
  244. PerformLayout();
  245. }
  246. }
  247. }