Winify – Blame information for rev 28

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
2 using System.Collections.Generic;
3 using System.Drawing;
4 using System.IO;
5 using System.Reflection;
6 using System.Runtime.InteropServices;
7 using System.Threading.Tasks;
8 using System.Windows.Forms;
9 using Microsoft.Win32;
10  
11 namespace Winify.Utilities
12 {
13 public static class Miscellaneous
14 {
15 #region Public Methods
16  
17 public static TimeSpan GetIdleTime()
18 {
19 var lastInPut = new Natives.LASTINPUTINFO();
28 office 20 lastInPut.cbSize = (uint)Marshal.SizeOf(lastInPut);
1 office 21 Natives.GetLastInputInfo(ref lastInPut);
22  
28 office 23 return TimeSpan.FromMilliseconds((uint)Environment.TickCount - lastInPut.dwTime);
1 office 24 }
25  
26 public static bool LaunchOnBootSet(bool enable)
27 {
28 using (var key = Registry.CurrentUser.OpenSubKey
28 office 29 ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
1 office 30 {
28 office 31 if (key == null) return false;
1 office 32  
33 switch (enable)
34 {
35 case true:
36 key.SetValue(Constants.AssemblyName, Assembly.GetEntryAssembly().Location);
37 break;
38 default:
39 key.DeleteValue(Constants.AssemblyName, false);
40 break;
41 }
42 }
43  
44 return true;
45 }
46  
47 public static bool LaunchOnBootGet()
48 {
49 using (var key = Registry.CurrentUser.OpenSubKey
28 office 50 ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
1 office 51 {
52 return key?.GetValue(Constants.AssemblyName) != null;
53 }
54 }
55  
56 /// <summary>
57 /// Enable double buffering for an arbitrary control.
58 /// </summary>
59 /// <param name="control">the control to enable double buffering for</param>
60 /// <returns>true on success</returns>
61 /// <remarks>Do not enable double buffering on RDP: https://devblogs.microsoft.com/oldnewthing/20060103-12/?p=32793</remarks>
62 public static bool SetDoubleBuffered(this Control control)
63 {
28 office 64 if (SystemInformation.TerminalServerSession) return false;
1 office 65  
66 var dgvType = control.GetType();
67 var pi = dgvType.GetProperty("DoubleBuffered",
68 BindingFlags.Instance | BindingFlags.NonPublic);
28 office 69 if (pi == null) return false;
1 office 70  
71 pi.SetValue(control, true, null);
72  
73 return true;
74 }
75  
76 public static async Task<Icon> CreateIconFromResource(string resource)
77 {
78 var iconBytes = await LoadResource(resource);
79 using (var iconMemoryStream = new MemoryStream(iconBytes))
80 {
28 office 81 var bitmap = (Bitmap)Image.FromStream(iconMemoryStream);
1 office 82 var bitmapIntPtr = bitmap.GetHicon();
83 var icon = Icon.FromHandle(bitmapIntPtr);
84 return icon;
85 }
86 }
87  
88 public static async Task<byte[]> LoadResource(string resource)
89 {
90 var assembly = Assembly.GetExecutingAssembly();
91  
92 using (var manifestResourceStream = assembly.GetManifestResourceStream(resource))
93 {
28 office 94 if (manifestResourceStream == null) return null;
1 office 95  
96 var memoryStream = new MemoryStream();
97  
98 await manifestResourceStream.CopyToAsync(memoryStream);
99  
100 memoryStream.Position = 0L;
101  
102 return memoryStream.ToArray();
103 }
104 }
105  
106 public static void InvokeIfRequired<T>(this T control, Action<T> action) where T : Control
107 {
108 if (control.InvokeRequired)
109 {
28 office 110 control.BeginInvoke((MethodInvoker)delegate { action(control); });
1 office 111 return;
112 }
113  
114 action(control);
115 }
116  
117 public static T MapValueToRange<T>(this T value, T xMin, T xMax, T yMin, T yMax)
118 where T : struct, IComparable<T>, IConvertible
119 {
28 office 120 return (dynamic)yMin +
121 ((dynamic)yMax - (dynamic)yMin) * ((dynamic)value - (dynamic)xMin) /
122 ((dynamic)xMax - (dynamic)xMin);
1 office 123 }
124  
125 public static IEnumerable<TU> SequenceSubtract<TU, TV>(this IEnumerable<TU> a,
28 office 126 IEnumerable<TV> b,
127 Func<TU, TV, bool> cmp)
1 office 128 {
129 var eb = new List<TV>(b);
130  
131 using (var ea = a.GetEnumerator())
132 {
133 while (ea.MoveNext())
134 {
28 office 135 if (ea.Current == null) continue;
1 office 136  
137 foreach (var ib in eb)
138 {
28 office 139 if (cmp.Invoke(ea.Current, ib)) continue;
1 office 140  
141 yield return ea.Current;
142  
143 break;
144 }
145 }
146 }
147 }
148  
14 office 149 /// <summary>
150 /// Returns the machine GUID.
151 /// </summary>
152 /// <returns>the GUID of the machine</returns>
153 /// <remarks>https://stackoverflow.com/questions/2333149/how-to-fast-get-hardware-id-in-c</remarks>
154 public static string GetMachineGuid()
155 {
156 var location = @"SOFTWARE\Microsoft\Cryptography";
157 var name = "MachineGuid";
158  
159 using (var localMachineX64View =
28 office 160 RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
14 office 161 {
162 using (var rk = localMachineX64View.OpenSubKey(location))
163 {
164 if (rk == null)
165 throw new KeyNotFoundException(
166 string.Format("Key Not Found: {0}", location));
167  
168 var machineGuid = rk.GetValue(name);
169 if (machineGuid == null)
170 throw new IndexOutOfRangeException(
171 string.Format("Index Not Found: {0}", name));
172  
173 return machineGuid.ToString();
174 }
175 }
176 }
177  
1 office 178 #endregion
179 }
180 }