using SmartCoalApplication.Resources; using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace SmartCoalApplication.Core { public sealed class Utility { private Utility() { } public static Keys LetterOrDigitCharToKeys(char str) { if (str >= 'a' && str <= 'z') { return (Keys)((int)(str - 'a') + (int)Keys.A); } else if (str >= 'A' && str <= 'Z') { return (Keys)((int)(str - 'A') + (int)Keys.A); } else if (str >= '0' && str <= '9') { return (Keys)((int)(str - '0') + (int)Keys.D0); } else { return Keys.None; } } public static Control FindFocus() { foreach (Form form in Application.OpenForms) { Control focused = FindFocus(form); if (focused != null) { return focused; } } return null; } private static Control FindFocus(Control c) { if (c.Focused) { return c; } foreach (Control child in c.Controls) { Control f = FindFocus(child); if (f != null) { return f; } } return null; } public static Font CreateFont(string name, float size, FontStyle style) { Font returnFont; try { returnFont = new Font(name, size, style); } catch (Exception) { returnFont = new Font(FontFamily.GenericSansSerif, size); } return returnFont; } public static readonly Color TransparentKey = Color.FromArgb(192, 192, 192); private static bool allowGCFullCollect = true; public static bool AllowGCFullCollect { get { return allowGCFullCollect; } set { allowGCFullCollect = value; } } public static void GCFullCollect() { /*if (AllowGCFullCollect) { GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); }*/ } public static bool IsArrowKey(Keys keyData) { Keys key = keyData & Keys.KeyCode; if (key == Keys.Up || key == Keys.Down || key == Keys.Left || key == Keys.Right) { return true; } else { return false; } } public static bool DoesControlHaveMouseCaptured(Control control) { bool result = false; result |= control.Capture; foreach (Control c in control.Controls) { result |= DoesControlHaveMouseCaptured(c); } return result; } public static void SplitRectangle(Rectangle rect, Rectangle[] rects) { int height = rect.Height; for (int i = 0; i < rects.Length; ++i) { Rectangle newRect = Rectangle.FromLTRB(rect.Left, rect.Top + ((height * i) / rects.Length), rect.Right, rect.Top + ((height * (i + 1)) / rects.Length)); rects[i] = newRect; } } public static void ErrorBox(IWin32Window parent, string message) { MessageBox.Show(parent, message, PdnInfo.GetBareProductName(), MessageBoxButtons.OK, MessageBoxIcon.Error); } public static DialogResult AskYesNo(IWin32Window parent, string question) { return MessageBox.Show(parent, question, PdnInfo.GetBareProductName(), MessageBoxButtons.YesNo, MessageBoxIcon.Question); } public static Icon ImageToIcon(Image image) { return ImageToIcon(image, Utility.TransparentKey); } public static Icon ImageToIcon(Image image, bool disposeImage) { return ImageToIcon(image, Utility.TransparentKey, disposeImage); } public static Icon ImageToIcon(Image image, Color seeThru) { return ImageToIcon(image, seeThru, false); } /// /// Converts an Image to an Icon. /// /// The Image to convert to an icon. Must be an appropriate icon size (32x32, 16x16, etc). /// The color that will be treated as transparent in the icon. /// Whether or not to dispose the passed-in Image. /// An Icon representation of the Image. public static Icon ImageToIcon(Image image, Color seeThru, bool disposeImage) { Bitmap bitmap = new Bitmap(image); for (int y = 0; y < bitmap.Height; ++y) { for (int x = 0; x < bitmap.Width; ++x) { if (bitmap.GetPixel(x, y) == seeThru) { bitmap.SetPixel(x, y, Color.FromArgb(0)); } } } Icon icon = Icon.FromHandle(bitmap.GetHicon()); bitmap.Dispose(); if (disposeImage) { image.Dispose(); } return icon; } /// /// Converts a RectangleF to RectangleF by rounding down the Location and rounding /// up the Size. /// public static Rectangle RoundRectangle(RectangleF rectF) { float left = (float)Math.Floor(rectF.Left); float top = (float)Math.Floor(rectF.Top); float right = (float)Math.Ceiling(rectF.Right); float bottom = (float)Math.Ceiling(rectF.Bottom); return Rectangle.Truncate(RectangleF.FromLTRB(left, top, right, bottom)); } public static int Clamp(int xvalue, int min, int max) { if (xvalue < min) { return min; } else if (xvalue > max) { return max; } else { return xvalue; } } public static double Lerp(double from, double to, double frac) { return (from + frac * (to - from)); } public static void Swap(ref T pa, ref T pb) { T t; t = pa; pa = pb; pb = t; } public static void DrawColorRectangle(Graphics g, Rectangle rect, Color color, bool drawBorder) { int inflateAmt = drawBorder ? -2 : 0; Rectangle colorRectangle = Rectangle.Inflate(rect, inflateAmt, inflateAmt); Brush colorBrush = new LinearGradientBrush(colorRectangle, Color.FromArgb(255, color), color, 90.0f, false); HatchBrush backgroundBrush = new HatchBrush(HatchStyle.LargeCheckerBoard, Color.FromArgb(191, 191, 191), Color.FromArgb(255, 255, 255)); if (drawBorder) { g.DrawRectangle(Pens.Black, rect.Left, rect.Top, rect.Width - 1, rect.Height - 1); g.DrawRectangle(Pens.White, rect.Left + 1, rect.Top + 1, rect.Width - 3, rect.Height - 3); } PixelOffsetMode oldPOM = g.PixelOffsetMode; g.PixelOffsetMode = PixelOffsetMode.Half; g.FillRectangle(backgroundBrush, colorRectangle); g.FillRectangle(colorBrush, colorRectangle); g.PixelOffsetMode = oldPOM; backgroundBrush.Dispose(); colorBrush.Dispose(); } public static bool CheckNumericUpDown(NumericUpDown upDown) { int a; bool result = int.TryParse(upDown.Text, out a); if (result && (a <= (int)upDown.Maximum) && (a >= (int)upDown.Minimum)) { return true; } else { return false; } } public static void SetNumericUpDownValue(NumericUpDown upDown, decimal newValue) { if (upDown.Value != newValue) { upDown.Value = newValue; } } public static void SetNumericUpDownValue(NumericUpDown upDown, int newValue) { SetNumericUpDownValue(upDown, (decimal)newValue); } } }