ArrowButton.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Drawing.Imaging;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using System.Windows.Forms.VisualStyles;
  12. namespace SmartCoalApplication.Core
  13. {
  14. public sealed class ArrowButton
  15. : ButtonBase
  16. {
  17. public bool drawTransparentBackColor = true;
  18. private ArrowDirection arrowDirection = ArrowDirection.Right;
  19. private bool drawWithGradient = false;
  20. private bool reverseArrowColors = false;
  21. private float arrowOutlineWidth = 1.0f;
  22. private bool forcedPushed = false;
  23. private Surface backBufferSurface = null;
  24. private RenderArgs backBuffer = null;
  25. public bool ForcedPushedAppearance
  26. {
  27. get
  28. {
  29. return this.forcedPushed;
  30. }
  31. set
  32. {
  33. if (this.forcedPushed != value)
  34. {
  35. this.forcedPushed = value;
  36. Invalidate();
  37. }
  38. }
  39. }
  40. public bool ReverseArrowColors
  41. {
  42. get
  43. {
  44. return this.reverseArrowColors;
  45. }
  46. set
  47. {
  48. if (this.reverseArrowColors != value)
  49. {
  50. this.reverseArrowColors = value;
  51. Invalidate();
  52. }
  53. }
  54. }
  55. public float ArrowOutlineWidth
  56. {
  57. get
  58. {
  59. return this.arrowOutlineWidth;
  60. }
  61. set
  62. {
  63. if (this.arrowOutlineWidth != value)
  64. {
  65. this.arrowOutlineWidth = value;
  66. Invalidate();
  67. }
  68. }
  69. }
  70. public ArrowDirection ArrowDirection
  71. {
  72. get
  73. {
  74. return this.arrowDirection;
  75. }
  76. set
  77. {
  78. if (this.arrowDirection != value)
  79. {
  80. this.arrowDirection = value;
  81. Invalidate();
  82. }
  83. }
  84. }
  85. public bool DrawWithGradient
  86. {
  87. get
  88. {
  89. return this.drawWithGradient;
  90. }
  91. set
  92. {
  93. if (this.drawWithGradient != value)
  94. {
  95. this.drawWithGradient = value;
  96. Invalidate();
  97. }
  98. }
  99. }
  100. public ArrowButton()
  101. {
  102. InitializeComponent();
  103. }
  104. private void InitializeComponent()
  105. {
  106. this.BackColor = this.drawTransparentBackColor ? Color.Transparent : Color.FromArgb(192, SystemColors.ControlDark);
  107. }
  108. protected override void Dispose(bool disposing)
  109. {
  110. if (disposing)
  111. {
  112. if (this.backBuffer != null)
  113. {
  114. this.backBuffer.Dispose();
  115. this.backBuffer = null;
  116. }
  117. if (this.backBufferSurface != null)
  118. {
  119. this.backBufferSurface.Dispose();
  120. this.backBufferSurface = null;
  121. }
  122. }
  123. base.Dispose(disposing);
  124. }
  125. protected override void OnPaintButton(Graphics g, PushButtonState state, bool drawFocusCues, bool drawKeyboardCues)
  126. {
  127. PushButtonState newState;
  128. if (this.forcedPushed)
  129. {
  130. newState = PushButtonState.Pressed;
  131. }
  132. else
  133. {
  134. newState = state;
  135. }
  136. OnPaintButtonImpl(g, newState, drawFocusCues, drawKeyboardCues);
  137. }
  138. private void OnPaintButtonImpl(Graphics g, PushButtonState state, bool drawFocusCues, bool drawKeyboardCues)
  139. {
  140. Color backColor;
  141. Color outlineColor;
  142. Color arrowFillColor;
  143. Color arrowOutlineColor;
  144. switch (state)
  145. {
  146. case PushButtonState.Disabled:
  147. backColor = this.drawTransparentBackColor ? Color.Transparent : Color.FromArgb(192, SystemColors.ControlDark);
  148. outlineColor = BackColor;
  149. arrowFillColor = Color.Gray;
  150. arrowOutlineColor = Color.Black;
  151. break;
  152. case PushButtonState.Hot:
  153. backColor = Color.FromArgb(64, SystemColors.HotTrack);
  154. outlineColor = backColor;
  155. arrowFillColor = Color.Blue;
  156. arrowOutlineColor = Color.White;
  157. break;
  158. case PushButtonState.Default:
  159. case PushButtonState.Normal:
  160. backColor = this.drawTransparentBackColor ? Color.Transparent : Color.FromArgb(192, SystemColors.ControlDark);
  161. outlineColor = Color.Transparent;
  162. arrowFillColor = Color.Black;
  163. arrowOutlineColor = Color.White;
  164. break;
  165. case PushButtonState.Pressed:
  166. backColor = Color.FromArgb(192, SystemColors.Highlight);
  167. outlineColor = Color.FromArgb(192, SystemColors.Highlight);
  168. arrowFillColor = Color.Blue;
  169. arrowOutlineColor = Color.White;
  170. break;
  171. default:
  172. throw new InvalidEnumArgumentException("buttonState");
  173. }
  174. // Draw parent background
  175. IPaintBackground asIpb = Parent as IPaintBackground;
  176. if (!this.drawWithGradient || asIpb == null)
  177. {
  178. if (asIpb != null)
  179. {
  180. Rectangle screenRect = RectangleToScreen(ClientRectangle);
  181. Rectangle parentRect = Parent.RectangleToClient(screenRect);
  182. g.TranslateTransform(-Left, -Top, MatrixOrder.Append);
  183. asIpb.PaintBackground(g, parentRect);
  184. g.TranslateTransform(+Left, +Top, MatrixOrder.Append);
  185. }
  186. else
  187. {
  188. using (SolidBrush backBrush = new SolidBrush(BackColor))
  189. {
  190. g.FillRectangle(backBrush, ClientRectangle);
  191. }
  192. }
  193. }
  194. else
  195. {
  196. if (this.backBufferSurface != null &&
  197. (this.backBufferSurface.Width != ClientSize.Width || this.backBufferSurface.Height != ClientSize.Height))
  198. {
  199. this.backBuffer.Dispose();
  200. this.backBuffer = null;
  201. this.backBufferSurface.Dispose();
  202. this.backBufferSurface = null;
  203. }
  204. if (this.backBufferSurface == null)
  205. {
  206. this.backBufferSurface = new Surface(ClientSize.Width, ClientSize.Height);
  207. this.backBuffer = new RenderArgs(this.backBufferSurface);
  208. }
  209. Rectangle screenRect = RectangleToScreen(ClientRectangle);
  210. Rectangle parentRect = Parent.RectangleToClient(screenRect);
  211. using (Graphics bg = Graphics.FromImage(this.backBuffer.Bitmap))
  212. {
  213. bg.TranslateTransform(-Left, -Top, MatrixOrder.Append);
  214. asIpb.PaintBackground(bg, parentRect);
  215. }
  216. BitmapData bitmapData = this.backBuffer.Bitmap.LockBits(
  217. new Rectangle(0, 0, this.backBuffer.Bitmap.Width, this.backBuffer.Bitmap.Height),
  218. ImageLockMode.ReadWrite,
  219. PixelFormat.Format32bppArgb);
  220. int startAlpha;
  221. int finishAlpha;
  222. if (this.arrowDirection == ArrowDirection.Left || this.arrowDirection == ArrowDirection.Up)
  223. {
  224. startAlpha = 255;
  225. finishAlpha = 0;
  226. }
  227. else if (this.arrowDirection == ArrowDirection.Right || this.ArrowDirection == ArrowDirection.Down)
  228. {
  229. startAlpha = 0;
  230. finishAlpha = 255;
  231. }
  232. else
  233. {
  234. throw new InvalidEnumArgumentException("this.arrowDirection");
  235. }
  236. unsafe
  237. {
  238. if (this.arrowDirection == ArrowDirection.Left || this.arrowDirection == ArrowDirection.Right)
  239. {
  240. for (int x = 0; x < this.backBuffer.Bitmap.Width; ++x)
  241. {
  242. float lerp = (float)x / (float)(this.backBuffer.Bitmap.Width - 1);
  243. if (this.arrowDirection == ArrowDirection.Left)
  244. {
  245. lerp = 1.0f - (float)Math.Cos(lerp * (Math.PI / 2.0));
  246. }
  247. else
  248. {
  249. lerp = (float)Math.Sin(lerp * (Math.PI / 2.0));
  250. }
  251. byte alpha = (byte)(startAlpha + ((int)(lerp * (finishAlpha - startAlpha))));
  252. byte* pb = (byte*)bitmapData.Scan0.ToPointer() + (x * 4) + 3; // *4 because 4-bytes per pixel, +3 to get to alpha channel
  253. for (int y = 0; y < this.backBuffer.Bitmap.Height; ++y)
  254. {
  255. *pb = alpha;
  256. pb += bitmapData.Stride;
  257. }
  258. }
  259. }
  260. else if (this.arrowDirection == ArrowDirection.Up || this.arrowDirection == ArrowDirection.Down)
  261. {
  262. for (int y = 0; y < this.backBuffer.Bitmap.Height; ++y)
  263. {
  264. float lerp = (float)y / (float)(this.backBuffer.Bitmap.Height - 1);
  265. lerp = 1.0f - (float)Math.Cos(lerp * (Math.PI / 2.0));
  266. byte alpha = (byte)(startAlpha + ((int)(lerp * (finishAlpha - startAlpha))));
  267. byte* pb = (byte*)bitmapData.Scan0.ToPointer() + (y * bitmapData.Stride) + 3; // *Stride for access to start of row, +3 to get to alpha channel
  268. for (int x = 0; x < this.backBuffer.Bitmap.Width; ++x)
  269. {
  270. *pb = alpha;
  271. pb += 4; // 4 for byte size of pixel
  272. }
  273. }
  274. }
  275. }
  276. this.backBuffer.Bitmap.UnlockBits(bitmapData);
  277. bitmapData = null;
  278. g.DrawImage(this.backBuffer.Bitmap, new Point(0, 0));
  279. }
  280. using (SolidBrush fillBrush = new SolidBrush(backColor))
  281. {
  282. g.FillRectangle(fillBrush, ClientRectangle);
  283. }
  284. // Draw outline
  285. using (Pen outlinePen = new Pen(outlineColor))
  286. {
  287. g.DrawRectangle(outlinePen, new Rectangle(0, 0, ClientSize.Width - 1, ClientSize.Height - 1));
  288. }
  289. // Draw button
  290. g.SmoothingMode = SmoothingMode.AntiAlias;
  291. const int arrowInset = 3;
  292. int arrowSize = Math.Min(ClientSize.Width - arrowInset * 2, ClientSize.Height - arrowInset * 2) - 1;
  293. PointF a;
  294. PointF b;
  295. PointF c;
  296. switch (this.arrowDirection)
  297. {
  298. case ArrowDirection.Left:
  299. a = new PointF(arrowInset, ClientSize.Height / 2);
  300. b = new PointF(ClientSize.Width - arrowInset, (ClientSize.Height - arrowSize) / 2);
  301. c = new PointF(ClientSize.Width - arrowInset, (ClientSize.Height + arrowSize) / 2);
  302. break;
  303. case ArrowDirection.Right:
  304. a = new PointF(ClientSize.Width - arrowInset, ClientSize.Height / 2);
  305. b = new PointF(arrowInset, (ClientSize.Height - arrowSize) / 2);
  306. c = new PointF(arrowInset, (ClientSize.Height + arrowSize) / 2);
  307. break;
  308. case ArrowDirection.Up:
  309. a = new PointF(ClientSize.Width / 2, (ClientSize.Height - arrowSize) / 2);
  310. b = new PointF((ClientSize.Width - arrowSize) / 2, (ClientSize.Height + arrowSize) / 2);
  311. c = new PointF((ClientSize.Width + arrowSize) / 2, (ClientSize.Height + arrowSize) / 2);
  312. break;
  313. case ArrowDirection.Down:
  314. a = new PointF(ClientSize.Width / 2, (ClientSize.Height + arrowSize) / 2);
  315. b = new PointF((ClientSize.Width - arrowSize) / 2, (ClientSize.Height - arrowSize) / 2);
  316. c = new PointF((ClientSize.Width + arrowSize) / 2, (ClientSize.Height - arrowSize) / 2);
  317. break;
  318. default:
  319. throw new InvalidEnumArgumentException("this.arrowDirection");
  320. }
  321. // SPIKE in order to get this rendering correctly right away
  322. if (this.arrowDirection == ArrowDirection.Down)
  323. {
  324. SmoothingMode oldSM = g.SmoothingMode;
  325. g.SmoothingMode = SmoothingMode.None;
  326. float top = b.Y - 2;
  327. float left = b.X;
  328. float right = c.X;
  329. int squareCount = (int)((right - left) / 3);
  330. Brush outlineBrush = new SolidBrush(arrowOutlineColor);
  331. Brush interiorBrush = new SolidBrush(arrowFillColor);
  332. g.FillRectangle(interiorBrush, left, top, right - left + 1, 3);
  333. ++left;
  334. while (left < right)
  335. {
  336. RectangleF rect = new RectangleF(left, top + 1, 1, 1);
  337. g.FillRectangle(outlineBrush, rect);
  338. left += 2;
  339. }
  340. outlineBrush.Dispose();
  341. outlineBrush = null;
  342. interiorBrush.Dispose();
  343. interiorBrush = null;
  344. a.Y += 2;
  345. b.Y += 2;
  346. c.Y += 2;
  347. g.SmoothingMode = oldSM;
  348. }
  349. if (this.reverseArrowColors)
  350. {
  351. Utility.Swap(ref arrowFillColor, ref arrowOutlineColor);
  352. }
  353. using (Brush buttonBrush = new SolidBrush(arrowFillColor))
  354. {
  355. g.FillPolygon(buttonBrush, new PointF[] { a, b, c });
  356. }
  357. using (Pen buttonPen = new Pen(arrowOutlineColor, this.arrowOutlineWidth))
  358. {
  359. g.DrawPolygon(buttonPen, new PointF[] { a, b, c });
  360. }
  361. }
  362. }
  363. }