CommandButton.cs 9.3 KB

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