diff --git a/src/modules/fancyzones/FancyZonesLib/ZoneSet.cpp b/src/modules/fancyzones/FancyZonesLib/ZoneSet.cpp index 6d049da872..1d8c936421 100644 --- a/src/modules/fancyzones/FancyZonesLib/ZoneSet.cpp +++ b/src/modules/fancyzones/FancyZonesLib/ZoneSet.cpp @@ -594,19 +594,17 @@ ZoneSet::CycleTabs(HWND window, bool reverse) noexcept for (;;) { auto next = GetNextTab(indexSet, window, reverse); - if (next == NULL) - { - break; - } - auto success = SetForegroundWindow(next); - if (!success && GetLastError() == ERROR_INVALID_WINDOW_HANDLE) + // Determine whether the window still exists + if (!IsWindow(next)) { // Dismiss the encountered window since it was probably closed DismissWindow(next); continue; } + SwitchToWindow(next); + break; } } diff --git a/src/modules/fancyzones/FancyZonesLib/util.cpp b/src/modules/fancyzones/FancyZonesLib/util.cpp index dac0c1a4c2..cae3c92da0 100644 --- a/src/modules/fancyzones/FancyZonesLib/util.cpp +++ b/src/modules/fancyzones/FancyZonesLib/util.cpp @@ -369,6 +369,22 @@ namespace FancyZonesUtils ::SetWindowPlacement(window, &placement); } + void SwitchToWindow(HWND window) noexcept + { + // Check if the window is minimized + if (IsIconic(window)) + { + // Show the window since SetForegroundWindow fails on minimized windows + ShowWindow(window, SW_RESTORE); + } + + // This is a hack to bypass the restriction on setting the foreground window + INPUT inputs[1] = { { .type = INPUT_MOUSE } }; + SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT)); + + SetForegroundWindow(window); + } + bool HasNoVisibleOwner(HWND window) noexcept { auto owner = GetWindow(window, GW_OWNER); diff --git a/src/modules/fancyzones/FancyZonesLib/util.h b/src/modules/fancyzones/FancyZonesLib/util.h index 5898586e61..de1c2bd939 100644 --- a/src/modules/fancyzones/FancyZonesLib/util.h +++ b/src/modules/fancyzones/FancyZonesLib/util.h @@ -195,6 +195,8 @@ namespace FancyZonesUtils // Parameter rect must be in screen coordinates (e.g. obtained from GetWindowRect) void SizeWindowToRect(HWND window, RECT rect) noexcept; + void SwitchToWindow(HWND window) noexcept; + bool HasNoVisibleOwner(HWND window) noexcept; bool IsStandardWindow(HWND window); bool IsCandidateForLastKnownZone(HWND window, const std::vector& excludedApps) noexcept;