-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowHelper.cs
More file actions
343 lines (275 loc) · 10.4 KB
/
Copy pathWindowHelper.cs
File metadata and controls
343 lines (275 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
using System.Runtime.InteropServices;
using System.Text;
namespace LittleSwitcher;
public static class WindowHelper
{
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetTopWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
public static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
public static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("user32.dll")]
public static extern int GetSystemMetrics(int nIndex);
[DllImport("user32.dll")]
public static extern IntPtr MonitorFromWindow(IntPtr hWnd, uint dwFlags);
[DllImport("user32.dll")]
public static extern IntPtr MonitorFromPoint(POINT pt, uint dwFlags);
[DllImport("user32.dll")]
public static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFO lpmi);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct MONITORINFO
{
public uint cbSize;
public RECT rcMonitor;
public RECT rcWork;
public uint dwFlags;
}
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string? lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindowEx(IntPtr hWndParent, IntPtr hWndChildAfter, string lpszClass, string? lpszWindow);
[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
public static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int dwProcessId);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool CloseHandle(IntPtr hObject);
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool OpenProcessToken(IntPtr processHandle, uint desiredAccess, out IntPtr tokenHandle);
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool GetTokenInformation(IntPtr tokenHandle, int tokenInformationClass,
out TOKEN_ELEVATION tokenInformation, int tokenInformationLength, out int returnLength);
[DllImport("dwmapi.dll")]
private static extern int DwmGetWindowAttribute(IntPtr hwnd, int dwAttribute, out int pvAttribute, int cbAttribute);
public const int SW_HIDE = 0;
public const int SW_RESTORE = 9;
public const int SM_CMONITORS = 80;
public const uint MONITOR_DEFAULTTONULL = 0;
public const uint MONITOR_DEFAULTTONEAREST = 2;
public const uint GW_HWNDNEXT = 2;
public const int GWL_STYLE = -16;
public const int GWL_EXSTYLE = -20;
public const int WS_CAPTION = 0x00C00000;
public const int WS_EX_TOPMOST = 0x00000008;
public const int DWMWA_CLOAKED = 14;
public const uint PROCESS_QUERY_LIMITED_INFORMATION = 0x1000;
public const uint TOKEN_QUERY = 0x0008;
public const int TokenElevation = 20;
public const uint SWP_FRAMECHANGED = 0x0020;
public const uint SWP_NOMOVE = 0x0002;
public const uint SWP_NOSIZE = 0x0001;
public const uint SWP_NOZORDER = 0x0004;
public const uint SWP_NOACTIVATE = 0x0010;
public static readonly IntPtr HWND_BOTTOM = new(1);
public static string GetWindowTitle(IntPtr hWnd)
{
var length = GetWindowTextLength(hWnd);
if (length == 0) return string.Empty;
var sb = new StringBuilder(length + 1);
GetWindowText(hWnd, sb, sb.Capacity);
return sb.ToString();
}
[StructLayout(LayoutKind.Sequential)]
private struct TOKEN_ELEVATION
{
public int TokenIsElevated;
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
}
public static string GetWindowClassName(IntPtr hWnd)
{
var sb = new StringBuilder(256);
return GetClassName(hWnd, sb, sb.Capacity) > 0 ? sb.ToString() : string.Empty;
}
public static string GetWindowProcessName(IntPtr hWnd)
{
try
{
var processId = GetWindowProcessId(hWnd);
if (processId == 0)
return string.Empty;
using var process = System.Diagnostics.Process.GetProcessById(processId);
return process.ProcessName;
}
catch
{
return string.Empty;
}
}
public static int GetWindowProcessId(IntPtr hWnd)
{
GetWindowThreadProcessId(hWnd, out var processId);
return (int)processId;
}
public static bool IsCurrentProcessElevated() => IsProcessElevated(Environment.ProcessId, out var elevated) && elevated;
public static bool IsProcessElevated(int processId, out bool elevated)
{
elevated = false;
var processHandle = IntPtr.Zero;
var tokenHandle = IntPtr.Zero;
try
{
processHandle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, processId);
if (processHandle == IntPtr.Zero)
return false;
if (!OpenProcessToken(processHandle, TOKEN_QUERY, out tokenHandle))
return false;
if (!GetTokenInformation(tokenHandle, TokenElevation, out var tokenElevation,
Marshal.SizeOf<TOKEN_ELEVATION>(), out _))
return false;
elevated = tokenElevation.TokenIsElevated != 0;
return true;
}
finally
{
if (tokenHandle != IntPtr.Zero)
CloseHandle(tokenHandle);
if (processHandle != IntPtr.Zero)
CloseHandle(processHandle);
}
}
public static int GetMonitorCount()
{
return GetSystemMetrics(SM_CMONITORS);
}
public static IntPtr GetWindowMonitor(IntPtr hWnd)
{
return MonitorFromWindow(hWnd, MONITOR_DEFAULTTONULL);
}
public static bool IsCloaked(IntPtr hWnd)
{
try
{
return DwmGetWindowAttribute(hWnd, DWMWA_CLOAKED, out var cloaked, sizeof(int)) == 0 && cloaked != 0;
}
catch
{
return false;
}
}
public static bool HasUsableRect(IntPtr hWnd)
{
if (!GetWindowRect(hWnd, out var rect))
return false;
return rect.Right > rect.Left && rect.Bottom > rect.Top;
}
public static IEnumerable<IntPtr> EnumerateTopLevelWindowsByZOrder()
{
var hwnd = GetTopWindow(IntPtr.Zero);
while (hwnd != IntPtr.Zero)
{
yield return hwnd;
hwnd = GetWindow(hwnd, GW_HWNDNEXT);
}
}
public static void ToggleTaskbar()
{
var hwnd = FindWindow("Shell_TrayWnd", null);
if (hwnd == IntPtr.Zero) return;
int cmd = IsWindowVisible(hwnd) ? SW_HIDE : SW_RESTORE;
ShowWindow(hwnd, cmd);
var prev = IntPtr.Zero;
while (true)
{
prev = FindWindowEx(IntPtr.Zero, prev, "Shell_SecondaryTrayWnd", null);
if (prev == IntPtr.Zero) break;
ShowWindow(prev, cmd);
}
}
public static void ShowTaskbar()
{
var hwnd = FindWindow("Shell_TrayWnd", null);
if (hwnd != IntPtr.Zero)
ShowWindow(hwnd, SW_RESTORE);
var prev = IntPtr.Zero;
while (true)
{
prev = FindWindowEx(IntPtr.Zero, prev, "Shell_SecondaryTrayWnd", null);
if (prev == IntPtr.Zero) break;
ShowWindow(prev, SW_RESTORE);
}
}
public static bool HasTitleBar(IntPtr hWnd)
{
var style = GetWindowLong(hWnd, GWL_STYLE);
return (style & WS_CAPTION) == WS_CAPTION;
}
public static bool IsTopMost(IntPtr hWnd)
{
var exStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
return (exStyle & WS_EX_TOPMOST) == WS_EX_TOPMOST;
}
public static void SetTitleBarVisible(IntPtr hWnd, bool visible)
{
var style = GetWindowLong(hWnd, GWL_STYLE);
if (visible)
style |= WS_CAPTION;
else
style &= ~WS_CAPTION;
SetWindowLong(hWnd, GWL_STYLE, style);
SetWindowPos(hWnd, IntPtr.Zero, 0, 0, 0, 0,
SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
}
public static void FocusWindow(IntPtr hWnd)
{
if (IsIconic(hWnd))
{
ShowWindow(hWnd, SW_RESTORE);
}
SetForegroundWindow(hWnd);
if (GetWindowRect(hWnd, out RECT rect))
{
int width = rect.Right - rect.Left;
int height = rect.Bottom - rect.Top;
if (width > 0 && height > 0)
{
SetCursorPos(rect.Left + width / 2, rect.Top + height / 2);
}
}
}
public static void SendWindowToBottom(IntPtr hWnd)
{
SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
}
}