using System; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Microsoft.Win32; namespace misu { public static class Utilities { #region Static Fields and Constants public static readonly string AssemblyName = Assembly.GetEntryAssembly().GetName().Name; public static readonly string AssemblyVersion = Assembly.GetEntryAssembly().GetName().Version.ToString(); #endregion #region Public Methods public static async Task LoadResource(string resource) { var assembly = Assembly.GetExecutingAssembly(); foreach (var assemblyResource in assembly.GetManifestResourceNames()) { if (!assemblyResource.Contains(resource)) { continue; } using (var manifestResourceStream = assembly.GetManifestResourceStream(assemblyResource)) { if (manifestResourceStream == null) { break; } var memoryStream = new MemoryStream(); await manifestResourceStream.CopyToAsync(memoryStream); memoryStream.Position = 0L; return memoryStream.ToArray(); } } throw new ArgumentException("Resource not found."); } public static void Execute(this Control control, Action lambda) { if (control.InvokeRequired) { control.Invoke((MethodInvoker) lambda.Invoke); } else { lambda.Invoke(); } } public static TValue MapValueToRange(this TValue value, TKey xMin, TKey xMax, TValue yMin, TValue yMax) where TKey : struct, IComparable, IConvertible where TValue : struct, IComparable, IConvertible { return (dynamic) yMin + ((dynamic) yMax - (dynamic) yMin) * ((dynamic) value - (dynamic) xMin) / ((dynamic) xMax - (dynamic) xMin); } public static IEnumerable SequenceSubtract(this IEnumerable a, IEnumerable b, Func cmp) { var eb = new List(b); using (var ea = a.GetEnumerator()) { while (ea.MoveNext()) { if (ea.Current == null) { continue; } foreach (var ib in eb) { if (cmp.Invoke(ea.Current, ib)) { continue; } yield return ea.Current; break; } } } } /// /// Determines if the sequence a is contained within b. /// /// an equatable type /// the first sequence /// the second sequence /// true if the first sequence is contained within the second sequence public static bool SequenceContains(this IEnumerable a, IEnumerable b) { using (var ea = a.GetEnumerator()) { using (var eb = b.GetEnumerator()) { var aea = ea.MoveNext(); var aeb = eb.MoveNext(); while (aea && aeb) { var eac = ea.Current; var ebc = eb.Current; if (eac.Equals(ebc)) { aea = ea.MoveNext(); aeb = eb.MoveNext(); continue; } aeb = eb.MoveNext(); } return !aea; } } } /// /// Convert Windows forms keys to a textual representation. /// /// the keys to convert /// a textual representation of the keys /// https://stackoverflow.com/questions/23170259/convert-keycode-to-char-string public static string KeysToString(this Keys key) { var keyboardState = new byte[255]; var keyboardStateStatus = Natives.GetKeyboardState(keyboardState); if (!keyboardStateStatus) { return ""; } var virtualKeyCode = (uint) key; var scanCode = Natives.MapVirtualKey(virtualKeyCode, 0); var inputLocaleIdentifier = Natives.GetKeyboardLayout(0); var result = new StringBuilder(); Natives.ToUnicodeEx(virtualKeyCode, scanCode, keyboardState, result, 5, 0, inputLocaleIdentifier); return result.ToString(); } public static bool LaunchOnBootSet(bool enable) { using (var key = Registry.CurrentUser.OpenSubKey ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) { if (key == null) { return false; } switch (enable) { case true: key.SetValue(AssemblyName, Assembly.GetEntryAssembly().Location); break; default: key.DeleteValue(AssemblyName, false); break; } } return true; } public static bool LaunchOnBootGet() { using (var key = Registry.CurrentUser.OpenSubKey ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) { return key?.GetValue(AssemblyName) != null; } } #endregion } }