Skip to content

Commit

Permalink
Fix window rendering (again). Retrieve window class to add a blacklis…
Browse files Browse the repository at this point in the history
…t for Win11's WinUI special "ghost" windows appearing in the Switchie UI.
  • Loading branch information
DARKGuy committed Oct 27, 2021
1 parent 75ee356 commit 7cd65d6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Core/API/WinAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public struct RECT
[DllImport("user32.dll")] public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")] public static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern IntPtr GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll")] public static extern IntPtr GetShellWindow();
[DllImport("user32.dll", SetLastError = true)] public static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
[DllImport("user32.dll", SetLastError = true)] public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
Expand Down
1 change: 1 addition & 0 deletions src/Core/Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class Window
public int ZOrder { get; set; }
public IntPtr Handle { get; set; }
public string Title { get; set; }
public string Class { get; set; }
public uint ProcessID { get; set; }
public Rectangle Dimensions { get; set; }
public Bitmap Icon { get; set; }
Expand Down
13 changes: 13 additions & 0 deletions src/Core/WindowManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public static List<Window> GetOpenWindows()
WinAPI.RECT rct = new WinAPI.RECT();
WinAPI.GetWindowRect(hWnd, ref rct);

IntPtr nRet;
StringBuilder className = new StringBuilder(256);
nRet = WinAPI.GetClassName(hWnd, className, className.Capacity);

int index = 0;
WinAPI.GetWindowThreadProcessId(hWnd, out uint pid);
try { index = WindowsVirtualDesktopManager.GetInstance().FromDesktop(WindowsVirtualDesktopManager.GetInstance().FromWindow((IntPtr)hWnd)); } catch { }
Expand All @@ -48,6 +52,7 @@ public static List<Window> GetOpenWindows()
Handle = hWnd,
Title = builder.ToString(),
ProcessID = pid,
Class = className.ToString(),
ZOrder = GetWindowZOrder(hWnd),
Icon = hIcon != 0 ? new Bitmap(Icon.FromHandle((IntPtr)hIcon).ToBitmap(), 16, 16) : null,
IsActive = hWnd == WinAPI.GetForegroundWindow(),
Expand All @@ -58,6 +63,14 @@ public static List<Window> GetOpenWindows()
return true;
}, 0);

string[] classBlacklist = new string[] {
"Windows.UI.Core.CoreWindow"
};
rv = rv
.Where(x => x.VirtualDesktopIndex >= 0)
.Where(x => !classBlacklist.Contains(x.Class))
.ToList();

return rv;
}

Expand Down
6 changes: 5 additions & 1 deletion src/GUI/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ private void OnShown(object sender, EventArgs e)
{
Invoke(new Action(() =>
{
try { Invalidate(); }
try
{
Windows = new ConcurrentBag<Window>(WindowManager.GetOpenWindows());
Invalidate();
}
catch { }
}));
await Task.Delay(100);
Expand Down

0 comments on commit 7cd65d6

Please sign in to comment.