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