Winify – Blame information for rev 14

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