Advertisement
AhmedSokr

Untitled

Nov 25th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.14 KB | None | 0 0
  1. namespace FormStyle
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8. using System.Drawing.Drawing2D;
  9. using System.Drawing.Text;
  10. using System.Runtime.CompilerServices;
  11. using System.Windows.Forms;
  12.  
  13. internal class FlatButton : Control
  14. {
  15. private static List<WeakReference> __ENCList = new List<WeakReference>();
  16. private Color _BaseColor;
  17. private bool _Rounded;
  18. private Color _TextColor;
  19. private int H;
  20. private MouseState State;
  21. private int W;
  22.  
  23. public FlatButton()
  24. {
  25. __ENCAddToList(this);
  26. this._Rounded = false;
  27. this.State = MouseState.None;
  28. this._BaseColor = Color.FromArgb(30, 30, 30);
  29. this._TextColor = Color.FromArgb(0xd3, 0xd3, 0xd3);
  30. this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.SupportsTransparentBackColor | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true);
  31. this.DoubleBuffered = true;
  32. Size size2 = new Size(0x6a, 0x20);
  33. this.Size = size2;
  34. this.BackColor = Color.Transparent;
  35. this.Font = new Font("Segoe UI", 12f);
  36. this.Cursor = Cursors.Hand;
  37. }
  38.  
  39. [DebuggerNonUserCode]
  40. private static void __ENCAddToList(object value)
  41. {
  42. List<WeakReference> list = __ENCList;
  43. lock (list)
  44. {
  45. if (__ENCList.Count == __ENCList.Capacity)
  46. {
  47. int index = 0;
  48. int num3 = __ENCList.Count - 1;
  49. for (int i = 0; i <= num3; i++)
  50. {
  51. WeakReference reference = __ENCList[i];
  52. if (reference.IsAlive)
  53. {
  54. if (i != index)
  55. {
  56. __ENCList[index] = __ENCList[i];
  57. }
  58. index++;
  59. }
  60. }
  61. __ENCList.RemoveRange(index, __ENCList.Count - index);
  62. __ENCList.Capacity = __ENCList.Count;
  63. }
  64. __ENCList.Add(new WeakReference(RuntimeHelpers.GetObjectValue(value)));
  65. }
  66. }
  67.  
  68. protected override void OnMouseDown(MouseEventArgs e)
  69. {
  70. base.OnMouseDown(e);
  71. this.State = MouseState.Down;
  72. this.Invalidate();
  73. }
  74.  
  75. protected override void OnMouseEnter(EventArgs e)
  76. {
  77. base.OnMouseEnter(e);
  78. this.State = MouseState.Over;
  79. this.Invalidate();
  80. }
  81.  
  82. protected override void OnMouseLeave(EventArgs e)
  83. {
  84. base.OnMouseLeave(e);
  85. this.State = MouseState.None;
  86. this.Invalidate();
  87. }
  88.  
  89. protected override void OnMouseUp(MouseEventArgs e)
  90. {
  91. base.OnMouseUp(e);
  92. this.State = MouseState.Over;
  93. this.Invalidate();
  94. }
  95.  
  96. protected override void OnPaint(PaintEventArgs e)
  97. {
  98. Helpers.B = new Bitmap(this.Width, this.Height);
  99. Helpers.G = Graphics.FromImage(Helpers.B);
  100. this.W = this.Width - 1;
  101. this.H = this.Height - 1;
  102. GraphicsPath path = new GraphicsPath();
  103. Rectangle rect = new Rectangle(0, 0, this.W, this.H);
  104. Graphics g = Helpers.G;
  105. g.SmoothingMode = SmoothingMode.HighQuality;
  106. g.PixelOffsetMode = PixelOffsetMode.HighQuality;
  107. g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
  108. g.Clear(this.BackColor);
  109. switch (this.State)
  110. {
  111. case MouseState.None:
  112. if (!this.Rounded)
  113. {
  114. g.FillRectangle(new SolidBrush(this._BaseColor), rect);
  115. g.DrawString(this.Text, this.Font, new SolidBrush(this._TextColor), rect, Helpers.CenterSF);
  116. break;
  117. }
  118. path = Helpers.RoundRec(rect, 6);
  119. g.FillPath(new SolidBrush(this._BaseColor), path);
  120. g.DrawString(this.Text, this.Font, new SolidBrush(this._TextColor), rect, Helpers.CenterSF);
  121. break;
  122.  
  123. case MouseState.Over:
  124. if (!this.Rounded)
  125. {
  126. g.FillRectangle(new SolidBrush(this._BaseColor), rect);
  127. g.FillRectangle(new SolidBrush(Color.FromArgb(20, Color.White)), rect);
  128. g.DrawString(this.Text, this.Font, new SolidBrush(this._TextColor), rect, Helpers.CenterSF);
  129. break;
  130. }
  131. path = Helpers.RoundRec(rect, 6);
  132. g.FillPath(new SolidBrush(this._BaseColor), path);
  133. g.FillPath(new SolidBrush(Color.FromArgb(20, Color.White)), path);
  134. g.DrawString(this.Text, this.Font, new SolidBrush(this._TextColor), rect, Helpers.CenterSF);
  135. break;
  136.  
  137. case MouseState.Down:
  138. if (!this.Rounded)
  139. {
  140. g.FillRectangle(new SolidBrush(this._BaseColor), rect);
  141. g.FillRectangle(new SolidBrush(Color.FromArgb(20, Color.Black)), rect);
  142. g.DrawString(this.Text, this.Font, new SolidBrush(this._TextColor), rect, Helpers.CenterSF);
  143. break;
  144. }
  145. path = Helpers.RoundRec(rect, 6);
  146. g.FillPath(new SolidBrush(this._BaseColor), path);
  147. g.FillPath(new SolidBrush(Color.FromArgb(20, Color.Black)), path);
  148. g.DrawString(this.Text, this.Font, new SolidBrush(this._TextColor), rect, Helpers.CenterSF);
  149. break;
  150. }
  151. g = null;
  152. base.OnPaint(e);
  153. Helpers.G.Dispose();
  154. e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
  155. e.Graphics.DrawImageUnscaled(Helpers.B, 0, 0);
  156. Helpers.B.Dispose();
  157. }
  158.  
  159. [Category("Colors")]
  160. public Color BaseColor
  161. {
  162. get
  163. {
  164. return this._BaseColor;
  165. }
  166. set
  167. {
  168. this._BaseColor = value;
  169. }
  170. }
  171.  
  172. [Category("Options")]
  173. public bool Rounded
  174. {
  175. get
  176. {
  177. return this._Rounded;
  178. }
  179. set
  180. {
  181. this._Rounded = value;
  182. }
  183. }
  184.  
  185. [Category("Colors")]
  186. public Color TextColor
  187. {
  188. get
  189. {
  190. return this._TextColor;
  191. }
  192. set
  193. {
  194. this._TextColor = value;
  195. }
  196. }
  197. }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement