Utility.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. using SmartCoalApplication.Resources;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace SmartCoalApplication.Core
  11. {
  12. public sealed class Utility
  13. {
  14. private Utility()
  15. {
  16. }
  17. public static Keys LetterOrDigitCharToKeys(char str)
  18. {
  19. if (str >= 'a' && str <= 'z')
  20. {
  21. return (Keys)((int)(str - 'a') + (int)Keys.A);
  22. }
  23. else if (str >= 'A' && str <= 'Z')
  24. {
  25. return (Keys)((int)(str - 'A') + (int)Keys.A);
  26. }
  27. else if (str >= '0' && str <= '9')
  28. {
  29. return (Keys)((int)(str - '0') + (int)Keys.D0);
  30. }
  31. else
  32. {
  33. return Keys.None;
  34. }
  35. }
  36. public static Control FindFocus()
  37. {
  38. foreach (Form form in Application.OpenForms)
  39. {
  40. Control focused = FindFocus(form);
  41. if (focused != null)
  42. {
  43. return focused;
  44. }
  45. }
  46. return null;
  47. }
  48. private static Control FindFocus(Control c)
  49. {
  50. if (c.Focused)
  51. {
  52. return c;
  53. }
  54. foreach (Control child in c.Controls)
  55. {
  56. Control f = FindFocus(child);
  57. if (f != null)
  58. {
  59. return f;
  60. }
  61. }
  62. return null;
  63. }
  64. public static Font CreateFont(string name, float size, FontStyle style)
  65. {
  66. Font returnFont;
  67. try
  68. {
  69. returnFont = new Font(name, size, style);
  70. }
  71. catch (Exception)
  72. {
  73. returnFont = new Font(FontFamily.GenericSansSerif, size);
  74. }
  75. return returnFont;
  76. }
  77. public static readonly Color TransparentKey = Color.FromArgb(192, 192, 192);
  78. private static bool allowGCFullCollect = true;
  79. public static bool AllowGCFullCollect
  80. {
  81. get
  82. {
  83. return allowGCFullCollect;
  84. }
  85. set
  86. {
  87. allowGCFullCollect = value;
  88. }
  89. }
  90. public static void GCFullCollect()
  91. {
  92. /*if (AllowGCFullCollect)
  93. {
  94. GC.Collect();
  95. GC.WaitForPendingFinalizers();
  96. GC.Collect();
  97. GC.WaitForPendingFinalizers();
  98. }*/
  99. }
  100. public static bool IsArrowKey(Keys keyData)
  101. {
  102. Keys key = keyData & Keys.KeyCode;
  103. if (key == Keys.Up || key == Keys.Down || key == Keys.Left || key == Keys.Right)
  104. {
  105. return true;
  106. }
  107. else
  108. {
  109. return false;
  110. }
  111. }
  112. public static bool DoesControlHaveMouseCaptured(Control control)
  113. {
  114. bool result = false;
  115. result |= control.Capture;
  116. foreach (Control c in control.Controls)
  117. {
  118. result |= DoesControlHaveMouseCaptured(c);
  119. }
  120. return result;
  121. }
  122. public static void SplitRectangle(Rectangle rect, Rectangle[] rects)
  123. {
  124. int height = rect.Height;
  125. for (int i = 0; i < rects.Length; ++i)
  126. {
  127. Rectangle newRect = Rectangle.FromLTRB(rect.Left,
  128. rect.Top + ((height * i) / rects.Length),
  129. rect.Right,
  130. rect.Top + ((height * (i + 1)) / rects.Length));
  131. rects[i] = newRect;
  132. }
  133. }
  134. public static void ErrorBox(IWin32Window parent, string message)
  135. {
  136. MessageBox.Show(parent, message, PdnInfo.GetBareProductName(), MessageBoxButtons.OK, MessageBoxIcon.Error);
  137. }
  138. public static DialogResult AskYesNo(IWin32Window parent, string question)
  139. {
  140. return MessageBox.Show(parent, question, PdnInfo.GetBareProductName(), MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  141. }
  142. public static Icon ImageToIcon(Image image)
  143. {
  144. return ImageToIcon(image, Utility.TransparentKey);
  145. }
  146. public static Icon ImageToIcon(Image image, bool disposeImage)
  147. {
  148. return ImageToIcon(image, Utility.TransparentKey, disposeImage);
  149. }
  150. public static Icon ImageToIcon(Image image, Color seeThru)
  151. {
  152. return ImageToIcon(image, seeThru, false);
  153. }
  154. /// <summary>
  155. /// Converts an Image to an Icon.
  156. /// </summary>
  157. /// <param name="image">The Image to convert to an icon. Must be an appropriate icon size (32x32, 16x16, etc).</param>
  158. /// <param name="seeThru">The color that will be treated as transparent in the icon.</param>
  159. /// <param name="disposeImage">Whether or not to dispose the passed-in Image.</param>
  160. /// <returns>An Icon representation of the Image.</returns>
  161. public static Icon ImageToIcon(Image image, Color seeThru, bool disposeImage)
  162. {
  163. Bitmap bitmap = new Bitmap(image);
  164. for (int y = 0; y < bitmap.Height; ++y)
  165. {
  166. for (int x = 0; x < bitmap.Width; ++x)
  167. {
  168. if (bitmap.GetPixel(x, y) == seeThru)
  169. {
  170. bitmap.SetPixel(x, y, Color.FromArgb(0));
  171. }
  172. }
  173. }
  174. Icon icon = Icon.FromHandle(bitmap.GetHicon());
  175. bitmap.Dispose();
  176. if (disposeImage)
  177. {
  178. image.Dispose();
  179. }
  180. return icon;
  181. }
  182. /// <summary>
  183. /// Converts a RectangleF to RectangleF by rounding down the Location and rounding
  184. /// up the Size.
  185. /// </summary>
  186. public static Rectangle RoundRectangle(RectangleF rectF)
  187. {
  188. float left = (float)Math.Floor(rectF.Left);
  189. float top = (float)Math.Floor(rectF.Top);
  190. float right = (float)Math.Ceiling(rectF.Right);
  191. float bottom = (float)Math.Ceiling(rectF.Bottom);
  192. return Rectangle.Truncate(RectangleF.FromLTRB(left, top, right, bottom));
  193. }
  194. public static int Clamp(int xvalue, int min, int max)
  195. {
  196. if (xvalue < min)
  197. {
  198. return min;
  199. }
  200. else if (xvalue > max)
  201. {
  202. return max;
  203. }
  204. else
  205. {
  206. return xvalue;
  207. }
  208. }
  209. public static double Lerp(double from, double to, double frac)
  210. {
  211. return (from + frac * (to - from));
  212. }
  213. public static void Swap<T>(ref T pa, ref T pb)
  214. {
  215. T t;
  216. t = pa;
  217. pa = pb;
  218. pb = t;
  219. }
  220. public static void DrawColorRectangle(Graphics g, Rectangle rect, Color color, bool drawBorder)
  221. {
  222. int inflateAmt = drawBorder ? -2 : 0;
  223. Rectangle colorRectangle = Rectangle.Inflate(rect, inflateAmt, inflateAmt);
  224. Brush colorBrush = new LinearGradientBrush(colorRectangle, Color.FromArgb(255, color), color, 90.0f, false);
  225. HatchBrush backgroundBrush = new HatchBrush(HatchStyle.LargeCheckerBoard, Color.FromArgb(191, 191, 191), Color.FromArgb(255, 255, 255));
  226. if (drawBorder)
  227. {
  228. g.DrawRectangle(Pens.Black, rect.Left, rect.Top, rect.Width - 1, rect.Height - 1);
  229. g.DrawRectangle(Pens.White, rect.Left + 1, rect.Top + 1, rect.Width - 3, rect.Height - 3);
  230. }
  231. PixelOffsetMode oldPOM = g.PixelOffsetMode;
  232. g.PixelOffsetMode = PixelOffsetMode.Half;
  233. g.FillRectangle(backgroundBrush, colorRectangle);
  234. g.FillRectangle(colorBrush, colorRectangle);
  235. g.PixelOffsetMode = oldPOM;
  236. backgroundBrush.Dispose();
  237. colorBrush.Dispose();
  238. }
  239. public static bool CheckNumericUpDown(NumericUpDown upDown)
  240. {
  241. int a;
  242. bool result = int.TryParse(upDown.Text, out a);
  243. if (result && (a <= (int)upDown.Maximum) && (a >= (int)upDown.Minimum))
  244. {
  245. return true;
  246. }
  247. else
  248. {
  249. return false;
  250. }
  251. }
  252. public static void SetNumericUpDownValue(NumericUpDown upDown, decimal newValue)
  253. {
  254. if (upDown.Value != newValue)
  255. {
  256. upDown.Value = newValue;
  257. }
  258. }
  259. public static void SetNumericUpDownValue(NumericUpDown upDown, int newValue)
  260. {
  261. SetNumericUpDownValue(upDown, (decimal)newValue);
  262. }
  263. }
  264. }