123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- 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);
- }
- /// <summary>
- /// Converts an Image to an Icon.
- /// </summary>
- /// <param name="image">The Image to convert to an icon. Must be an appropriate icon size (32x32, 16x16, etc).</param>
- /// <param name="seeThru">The color that will be treated as transparent in the icon.</param>
- /// <param name="disposeImage">Whether or not to dispose the passed-in Image.</param>
- /// <returns>An Icon representation of the Image.</returns>
- 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;
- }
- /// <summary>
- /// Converts a RectangleF to RectangleF by rounding down the Location and rounding
- /// up the Size.
- /// </summary>
- 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<T>(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);
- }
- }
- }
|