Skip to content

Commit 7ec82a2

Browse files
shanselmanCopilot
andcommitted
Fix Explorer: move only targeted window, not all Explorer windows
Explorer uses a single process for all File Explorer windows, so the sibling-grouping logic would grab every open Explorer window and try to move them all. Some of those windows (on other desktops or shell helper windows) fail with TYPE_E_ELEMENTNOTFOUND, causing a crash. For explorer.exe, skip grouping and just move the one targeted window. Also make secondary window move failures non-fatal for other apps. Fixes #8 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent daa7a58 commit 7ec82a2

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ packages/
1818

1919
## Publish output
2020
publish/
21+
22+
mvd-debug.log

src/MaximizeToVirtualDesktop/FullScreenManager.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,22 @@ public void MaximizeToDesktop(IntPtr hwnd)
7777
return;
7878
}
7979

80-
// 1. Find all windows from the same process
81-
var allWindows = GetAllProcessWindows(hwnd);
80+
// 1. Find all windows to move together
81+
// Explorer is a single-process app — all File Explorer windows share one process,
82+
// so grouping by process would grab every Explorer window. Just move the targeted one.
83+
NativeMethods.GetWindowThreadProcessId(hwnd, out int targetPid);
84+
bool isExplorer = false;
85+
try
86+
{
87+
using var proc = Process.GetProcessById(targetPid);
88+
isExplorer = proc.ProcessName.Equals("explorer", StringComparison.OrdinalIgnoreCase);
89+
}
90+
catch { }
91+
92+
var allWindows = isExplorer
93+
? new List<IntPtr> { hwnd }
94+
: GetAllProcessWindows(hwnd);
95+
8296
if (allWindows.Count == 0)
8397
{
8498
Trace.WriteLine("FullScreenManager: No valid windows found for process, aborting.");
@@ -147,10 +161,10 @@ public void MaximizeToDesktop(IntPtr hwnd)
147161
{
148162
movedWindows.Add(window);
149163
}
150-
else
164+
else if (window == hwnd)
151165
{
152-
Trace.WriteLine($"FullScreenManager: Failed to move window {window}, rolling back.");
153-
// Rollback: move already-moved windows back
166+
// Primary window failed — must rollback
167+
Trace.WriteLine($"FullScreenManager: Failed to move primary window {window}, rolling back.");
154168
var origDesktop = _vds.FindDesktop(originalDesktopId.Value);
155169
try
156170
{
@@ -170,6 +184,11 @@ public void MaximizeToDesktop(IntPtr hwnd)
170184
Marshal.ReleaseComObject(tempDesktop);
171185
return;
172186
}
187+
else
188+
{
189+
// Secondary window failed — skip it, not critical
190+
Trace.WriteLine($"FullScreenManager: Skipping secondary window {window} (move failed).");
191+
}
173192
}
174193

175194
// 6. Switch to the new desktop

src/MaximizeToVirtualDesktop/VirtualDesktopService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public bool MoveWindowToDesktop(IntPtr hwnd, IVirtualDesktop desktop)
156156
}
157157
catch (Exception ex)
158158
{
159-
Trace.WriteLine($"VirtualDesktopService: MoveWindowToDesktop failed: {ex.Message}");
159+
Trace.WriteLine($"VirtualDesktopService: MoveWindowToDesktop failed for hwnd={hwnd}: {ex.GetType().Name}: {ex.Message}");
160160
if (view != null) { Marshal.ReleaseComObject(view); view = null; }
161161

162162
// Second attempt: try main window of the process

0 commit comments

Comments
 (0)