-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowManager.cs
More file actions
421 lines (373 loc) · 16.3 KB
/
WindowManager.cs
File metadata and controls
421 lines (373 loc) · 16.3 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
namespace bws
{
public class WindowItem
{
public IntPtr Hwnd { get; set; }
public string Title { get; set; } = string.Empty;
public BitmapSource? Icon { get; set; }
}
public static class WindowManager
{
public static bool ShowAllWindows { get; set; } = true;
public static bool DebugExeTitle { get; set; } = false;
private static HashSet<string> _blacklist = new(StringComparer.OrdinalIgnoreCase);
private static Dictionary<IntPtr, BitmapSource> _iconCache = new();
private static List<IntPtr> _mruList = new List<IntPtr>();
private static Win32Interop.WinEventDelegate? _winEventDelegate;
private static IntPtr _hWinEventHook = IntPtr.Zero;
public static void InitializeMruTracking()
{
LoadBlacklist();
lock (_mruList)
{
_mruList.Clear();
IntPtr hWnd = Win32Interop.GetTopWindow(IntPtr.Zero);
while (hWnd != IntPtr.Zero)
{
_mruList.Add(hWnd);
hWnd = Win32Interop.GetWindow(hWnd, Win32Interop.GW_HWNDNEXT);
}
}
_winEventDelegate = new Win32Interop.WinEventDelegate(WinEventProc);
_hWinEventHook = Win32Interop.SetWinEventHook(
Win32Interop.EVENT_SYSTEM_FOREGROUND, Win32Interop.EVENT_SYSTEM_FOREGROUND,
IntPtr.Zero, _winEventDelegate, 0, 0,
Win32Interop.WINEVENT_OUTOFCONTEXT | Win32Interop.WINEVENT_SKIPOWNPROCESS);
}
private static void LoadBlacklist()
{
try
{
string exeDir = AppDomain.CurrentDomain.BaseDirectory;
string blacklistPath = Path.Combine(exeDir, "blacklist.txt");
if (File.Exists(blacklistPath))
{
var lines = File.ReadAllLines(blacklistPath);
foreach (var line in lines)
{
var trimmed = line.Trim();
if (!string.IsNullOrEmpty(trimmed) && !trimmed.StartsWith("#"))
{
_blacklist.Add(trimmed);
}
}
Console.WriteLine($"[BWS] Loaded {_blacklist.Count} entries from blacklist.");
}
else
{
// Create default blacklist
File.WriteAllText(blacklistPath, "TextInputHost.exe\nPopupHost\n");
}
}
catch (Exception ex)
{
Console.WriteLine($"[BWS] Failed to load blacklist: {ex}");
}
}
public static void ShutdownMruTracking()
{
if (_hWinEventHook != IntPtr.Zero)
{
Win32Interop.UnhookWinEvent(_hWinEventHook);
_hWinEventHook = IntPtr.Zero;
}
}
private static void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
if (eventType == Win32Interop.EVENT_SYSTEM_FOREGROUND)
{
lock (_mruList)
{
_mruList.Remove(hwnd);
_mruList.Insert(0, hwnd);
}
// Log the focused process for user blacklist profiling
string procName = GetProcessName(hwnd);
if (!string.IsNullOrEmpty(procName))
{
Console.WriteLine($"[BWS FOCUS ACTIVATED] Process: {procName} | Title: {GetWindowTitle(hwnd)}");
}
}
}
public static List<WindowItem> GetOpenWindows()
{
var windows = new List<WindowItem>();
IntPtr hWnd = Win32Interop.GetTopWindow(IntPtr.Zero);
while (hWnd != IntPtr.Zero)
{
if (IsAppWindow(hWnd))
{
string title = DebugExeTitle ? GetProcessName(hWnd) : GetWindowTitle(hWnd);
if (!string.IsNullOrWhiteSpace(title))
{
windows.Add(new WindowItem
{
Hwnd = hWnd,
Title = title,
Icon = GetWindowIcon(hWnd)
});
}
}
hWnd = Win32Interop.GetWindow(hWnd, Win32Interop.GW_HWNDNEXT);
}
lock (_mruList)
{
windows = windows.OrderBy(w =>
{
int index = _mruList.IndexOf(w.Hwnd);
return index == -1 ? int.MaxValue : index;
}).ToList();
}
return windows;
}
private static bool IsAppWindow(IntPtr hWnd)
{
if (!Win32Interop.IsWindowVisible(hWnd))
return false;
uint style = Win32Interop.GetWindowLong(hWnd, Win32Interop.GWL_STYLE);
uint exStyle = Win32Interop.GetWindowLong(hWnd, Win32Interop.GWL_EXSTYLE);
// Ignore tool windows
if ((exStyle & Win32Interop.WS_EX_TOOLWINDOW) != 0)
return false;
// Check if cloaked by DWM (e.g. background UWP apps)
Win32Interop.DwmGetWindowAttribute(hWnd, Win32Interop.DWMWA_CLOAKED, out int cloaked, sizeof(int));
if (cloaked != 0)
{
if (ShowAllWindows && cloaked == Win32Interop.DWM_CLOAKED_SHELL)
{
// It's cloaked by the shell (likely on another virtual desktop).
// Allow it since the user explicitly requested all windows.
}
else
{
return false;
}
}
// Must have a title
if (Win32Interop.GetWindowTextLength(hWnd) == 0)
return false;
// Check Blacklist
string procName = GetProcessName(hWnd);
if (!string.IsNullOrEmpty(procName) && _blacklist.Contains(procName))
return false;
string windowTitle = GetWindowTitle(hWnd);
if (!string.IsNullOrEmpty(windowTitle) && _blacklist.Contains(windowTitle))
return false;
return true;
}
private static string GetProcessName(IntPtr hWnd)
{
try
{
Win32Interop.GetWindowThreadProcessId(hWnd, out uint pid);
if (pid != 0)
{
IntPtr hProc = Win32Interop.OpenProcess(Win32Interop.PROCESS_QUERY_LIMITED_INFORMATION, false, pid);
if (hProc != IntPtr.Zero)
{
try
{
StringBuilder sb = new StringBuilder(1024);
if (Win32Interop.GetModuleFileNameEx(hProc, IntPtr.Zero, sb, sb.Capacity) > 0)
{
return Path.GetFileName(sb.ToString());
}
}
finally
{
Win32Interop.CloseHandle(hProc);
}
}
}
}
catch {}
return string.Empty;
}
private static string GetWindowTitle(IntPtr hWnd)
{
int length = Win32Interop.GetWindowTextLength(hWnd);
if (length == 0) return string.Empty;
StringBuilder sb = new StringBuilder(length + 1);
Win32Interop.GetWindowText(hWnd, sb, sb.Capacity);
return sb.ToString();
}
private static BitmapSource? GetWindowIcon(IntPtr hWnd)
{
// Check cache first (covers cloaked UWP windows on other desktops)
if (_iconCache.TryGetValue(hWnd, out var cachedIcon))
{
return cachedIcon;
}
var result = GetWindowIconInternal(hWnd);
// Cache the result if we got an icon
if (result != null)
{
_iconCache[hWnd] = result;
}
// Periodically clean cache of dead window handles
if (_iconCache.Count > 100)
{
var deadHandles = _iconCache.Keys
.Where(h => !Win32Interop.IsWindowVisible(h))
.ToList();
foreach (var h in deadHandles)
_iconCache.Remove(h);
}
return result;
}
private static BitmapSource? GetWindowIconInternal(IntPtr hWnd)
{
// 0. Try modern Shell API first (catches UWP and modern apps)
try
{
string aumid = Win32Interop.GetAppUserModelId(hWnd);
if (string.IsNullOrEmpty(aumid))
{
StringBuilder sbClass = new StringBuilder(256);
Win32Interop.GetClassName(hWnd, sbClass, sbClass.Capacity);
if (sbClass.ToString() == "ApplicationFrameWindow")
{
// Try child window enumeration first (works for UWP on current desktop)
string childProcAumid = "";
Win32Interop.EnumChildWindows(hWnd, (childHwnd, lParam) => {
Win32Interop.GetWindowThreadProcessId(childHwnd, out uint cPid);
string pAumid = Win32Interop.GetAumidFromProcess(cPid);
if (!string.IsNullOrEmpty(pAumid)) {
childProcAumid = pAumid;
return false; // stop enumeration
}
return true;
}, IntPtr.Zero);
aumid = childProcAumid;
// Fallback: for cloaked UWP on other desktops, child windows may not be accessible.
// Try getting AUMID from the window's own process (ApplicationFrameHost)
if (string.IsNullOrEmpty(aumid))
{
Win32Interop.GetWindowThreadProcessId(hWnd, out uint hostPid);
aumid = Win32Interop.GetAumidFromProcess(hostPid);
}
}
}
if (!string.IsNullOrEmpty(aumid))
{
var appInfo = Windows.ApplicationModel.AppInfo.GetFromAppUserModelId(aumid);
if (appInfo != null)
{
var logoStreamRef = appInfo.DisplayInfo.GetLogo(new Windows.Foundation.Size(256, 256));
if (logoStreamRef != null)
{
var task = logoStreamRef.OpenReadAsync().AsTask();
task.Wait();
var stream = task.Result;
using (var managedStream = stream.AsStreamForRead())
{
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.DecodePixelWidth = 256;
bitmapImage.DecodePixelHeight = 256;
bitmapImage.StreamSource = managedStream;
bitmapImage.EndInit();
bitmapImage.Freeze();
// To make the icon LARGER, we need a SMALLER crop window to zoom in further, cutting away MORE padding margin.
var targetWidth = 96;
var targetHeight = 96;
var offset = (256 - targetWidth) / 2; // 80 offset on all sides
var croppedBitmap = new CroppedBitmap(bitmapImage, new System.Windows.Int32Rect(offset, offset, targetWidth, targetHeight));
var drawVisual = new System.Windows.Media.DrawingVisual();
using (var drawingContext = drawVisual.RenderOpen())
{
drawingContext.DrawImage(croppedBitmap, new System.Windows.Rect(0, 0, 256, 256));
}
var renderTarget = new RenderTargetBitmap(256, 256, 96, 96, System.Windows.Media.PixelFormats.Pbgra32);
renderTarget.Render(drawVisual);
renderTarget.Freeze();
return renderTarget;
}
}
}
}
}
catch
{
// Fallback to Win32 if ShellObject fails or doesn't have an AUMID
}
IntPtr hIcon = IntPtr.Zero;
// 1. Try WM_GETICON (Big)
hIcon = Win32Interop.SendMessage(hWnd, Win32Interop.WM_GETICON, (IntPtr)Win32Interop.ICON_BIG, IntPtr.Zero);
// 2. Try WM_GETICON (Small)
if (hIcon == IntPtr.Zero)
hIcon = Win32Interop.SendMessage(hWnd, Win32Interop.WM_GETICON, (IntPtr)Win32Interop.ICON_SMALL, IntPtr.Zero);
// 3. Try GCLP_HICON (Class Big)
if (hIcon == IntPtr.Zero)
hIcon = Win32Interop.GetClassLongSafe(hWnd, Win32Interop.GCLP_HICON);
// 4. Try GCLP_HICONSM (Class Small)
if (hIcon == IntPtr.Zero)
hIcon = Win32Interop.GetClassLongSafe(hWnd, Win32Interop.GCLP_HICONSM);
if (hIcon != IntPtr.Zero)
{
try
{
var icon = Icon.FromHandle(hIcon);
var bitmap = icon.ToBitmap();
var source = Imaging.CreateBitmapSourceFromHBitmap(
bitmap.GetHbitmap(),
IntPtr.Zero,
System.Windows.Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
source.Freeze();
return source;
}
catch
{
// Fallback or ignore
}
}
// 5. Final Fallback: Try extracting the default icon from the executable file itself
try
{
Win32Interop.GetWindowThreadProcessId(hWnd, out uint processId);
var processHandle = Win32Interop.OpenProcess(0x0400 | 0x0010, false, processId);
if (processHandle != IntPtr.Zero)
{
StringBuilder sb = new StringBuilder(1024);
int capacity = sb.Capacity;
if (Win32Interop.QueryFullProcessImageName(processHandle, 0, sb, ref capacity))
{
string exePath = sb.ToString();
var exeIcon = Icon.ExtractAssociatedIcon(exePath);
if (exeIcon != null)
{
var bitmap = exeIcon.ToBitmap();
var source = Imaging.CreateBitmapSourceFromHBitmap(
bitmap.GetHbitmap(),
IntPtr.Zero,
System.Windows.Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
source.Freeze();
Win32Interop.CloseHandle(processHandle);
return source;
}
}
Win32Interop.CloseHandle(processHandle);
}
}
catch
{
// Everything failed
}
return null;
}
}
}