From 5bf30fe39eeefc34484a1fa7191cab4e0d38f8fc Mon Sep 17 00:00:00 2001 From: HarcourtC Date: Sat, 18 Apr 2026 12:44:07 +0800 Subject: [PATCH 1/7] =?UTF-8?q?feat(Win32):=E6=88=AA=E5=9B=BE=E5=89=8D?= =?UTF-8?q?=E5=81=9A=E4=B8=80=E6=AC=A1=E7=AA=97=E5=8F=A3=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/MaaWin32ControlUnit/Manager/Win32ControlUnitMgr.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/source/MaaWin32ControlUnit/Manager/Win32ControlUnitMgr.cpp b/source/MaaWin32ControlUnit/Manager/Win32ControlUnitMgr.cpp index 0ffd53a429..adfa60e965 100644 --- a/source/MaaWin32ControlUnit/Manager/Win32ControlUnitMgr.cpp +++ b/source/MaaWin32ControlUnit/Manager/Win32ControlUnitMgr.cpp @@ -10,6 +10,7 @@ #include "Input/LegacyEventInput.h" #include "Input/MessageInput.h" #include "Input/SeizeInput.h" +#include "Input/InputUtils.h" #include "Screencap/DesktopDupScreencap.h" #include "Screencap/DesktopDupWindowScreencap.h" #include "Screencap/FramePoolScreencap.h" @@ -182,6 +183,12 @@ std::shared_ptr LogInfo << VAR(method) << VAR(duration); }; + // Best-effort foreground recovery before speed test. + // Do not fail speed test on recovery failure. + if (!units.empty()) { + ::MaaNS::CtrlUnitNs::ensure_foreground_and_topmost(hwnd_); + } + for (auto& [method, unit] : units) { LogInfo << "Warming up" << method; if (!unit->screencap()) { From 9df467a3964159fed9d4556e66232c81d03b9600 Mon Sep 17 00:00:00 2001 From: HarcourtC Date: Mon, 18 May 2026 01:40:00 +0800 Subject: [PATCH 2/7] =?UTF-8?q?refactor(Win32):=20=E5=B0=86ensure=5Fwindow?= =?UTF-8?q?=E4=BD=9C=E4=B8=BAbase=20-=20=E7=A7=BB=E9=99=A4=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E6=97=B6=E5=89=8D=E7=BD=AE=E8=BF=99=E4=B8=80=E9=80=BB?= =?UTF-8?q?=E8=BE=91=20-=20=E7=8E=B0=E5=9C=A8=E8=BF=9E=E6=8E=A5=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E6=97=B6=EF=BC=8C=E6=AF=8F5s=E5=B0=9D=E8=AF=95?= =?UTF-8?q?=E5=89=8D=E7=BD=AE=E4=B8=80=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Base/ForegroundUtils.h | 36 +++++++++++ source/MaaWin32ControlUnit/Input/InputUtils.h | 30 +-------- .../Manager/Win32ControlUnitMgr.cpp | 14 ++--- .../Screencap/DesktopDupWindowScreencap.cpp | 1 - .../Screencap/DesktopDupWindowScreencap.h | 1 - .../Screencap/ForegroundScreencap.h | 61 +++++++++++++++++++ .../Screencap/ScreenDCScreencap.cpp | 1 - .../Screencap/ScreenDCScreencap.h | 1 - 8 files changed, 103 insertions(+), 42 deletions(-) create mode 100644 source/MaaWin32ControlUnit/Base/ForegroundUtils.h create mode 100644 source/MaaWin32ControlUnit/Screencap/ForegroundScreencap.h diff --git a/source/MaaWin32ControlUnit/Base/ForegroundUtils.h b/source/MaaWin32ControlUnit/Base/ForegroundUtils.h new file mode 100644 index 0000000000..d6a33fea5a --- /dev/null +++ b/source/MaaWin32ControlUnit/Base/ForegroundUtils.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include + +#include "MaaUtils/SafeWindows.hpp" + +#include "Common/Conf.h" + +MAA_CTRL_UNIT_NS_BEGIN + +inline void ensure_foreground_and_topmost(HWND hwnd) +{ + if (!hwnd) { + return; + } + + // 如果窗口不在前台,先将其置顶 + if (hwnd != GetForegroundWindow()) { + // 将窗口移到 Z 序顶部 + SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW); + std::this_thread::sleep_for(std::chrono::milliseconds(5)); + + // 尝试设置为前台窗口 + SetForegroundWindow(hwnd); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + + // 再次检查,如果仍然不在前台,再次置顶 + if (hwnd != GetForegroundWindow()) { + SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); + std::this_thread::sleep_for(std::chrono::milliseconds(5)); + } + } +} + +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaWin32ControlUnit/Input/InputUtils.h b/source/MaaWin32ControlUnit/Input/InputUtils.h index 4e74b58a67..594764344a 100644 --- a/source/MaaWin32ControlUnit/Input/InputUtils.h +++ b/source/MaaWin32ControlUnit/Input/InputUtils.h @@ -1,8 +1,6 @@ #pragma once -#include -#include - +#include "Base/ForegroundUtils.h" #include "Common/Conf.h" #include "MaaControlUnit/ControlUnitAPI.h" #include "MaaUtils/SafeWindows.hpp" @@ -27,32 +25,6 @@ inline void send_activate_message(HWND hwnd, bool use_post = false) std::this_thread::sleep_for(std::chrono::milliseconds(10)); } -// 窗口激活并置顶工具函数(强化版本,用于需要前台的物理输入方式) -// 用于 LegacyEventInput 和 SeizeInput,因为它们使用 SendInput/mouse_event 等物理输入 API -inline void ensure_foreground_and_topmost(HWND hwnd) -{ - if (!hwnd) { - return; - } - - // 如果窗口不在前台,先将其置顶 - if (hwnd != GetForegroundWindow()) { - // 将窗口移到 Z 序顶部 - SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW); - std::this_thread::sleep_for(std::chrono::milliseconds(5)); - - // 尝试设置为前台窗口 - SetForegroundWindow(hwnd); - std::this_thread::sleep_for(std::chrono::milliseconds(10)); - - // 再次检查,如果仍然不在前台,再次置顶 - if (hwnd != GetForegroundWindow()) { - SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); - std::this_thread::sleep_for(std::chrono::milliseconds(5)); - } - } -} - // Contact 到 WM_* 消息的转换结果 struct MouseMessageInfo { diff --git a/source/MaaWin32ControlUnit/Manager/Win32ControlUnitMgr.cpp b/source/MaaWin32ControlUnit/Manager/Win32ControlUnitMgr.cpp index adfa60e965..c361195434 100644 --- a/source/MaaWin32ControlUnit/Manager/Win32ControlUnitMgr.cpp +++ b/source/MaaWin32ControlUnit/Manager/Win32ControlUnitMgr.cpp @@ -10,11 +10,11 @@ #include "Input/LegacyEventInput.h" #include "Input/MessageInput.h" #include "Input/SeizeInput.h" -#include "Input/InputUtils.h" #include "Screencap/DesktopDupScreencap.h" #include "Screencap/DesktopDupWindowScreencap.h" #include "Screencap/FramePoolScreencap.h" #include "Screencap/FramePoolWithPseudoMinimizeScreencap.h" +#include "Screencap/ForegroundScreencap.h" #include "Screencap/GdiScreencap.h" #include "Screencap/HwndUtils.hpp" #include "Screencap/PrintWindowScreencap.h" @@ -98,12 +98,14 @@ std::unordered_map(hwnd_); }); add(MaaWin32ScreencapMethod_DXGI_DesktopDup_Window, ScreencapMethod::DXGI_DesktopDup_Window, [this] { - return std::make_shared(hwnd_); + return std::make_shared(hwnd_, std::make_shared(hwnd_)); }); add(MaaWin32ScreencapMethod_PrintWindow, ScreencapMethod::PrintWindow, [this] { return std::make_shared(hwnd_); }); - add(MaaWin32ScreencapMethod_ScreenDC, ScreencapMethod::ScreenDC, [this] { return std::make_shared(hwnd_); }); + add(MaaWin32ScreencapMethod_ScreenDC, ScreencapMethod::ScreenDC, [this] { + return std::make_shared(hwnd_, std::make_shared(hwnd_)); + }); return units; } @@ -183,12 +185,6 @@ std::shared_ptr LogInfo << VAR(method) << VAR(duration); }; - // Best-effort foreground recovery before speed test. - // Do not fail speed test on recovery failure. - if (!units.empty()) { - ::MaaNS::CtrlUnitNs::ensure_foreground_and_topmost(hwnd_); - } - for (auto& [method, unit] : units) { LogInfo << "Warming up" << method; if (!unit->screencap()) { diff --git a/source/MaaWin32ControlUnit/Screencap/DesktopDupWindowScreencap.cpp b/source/MaaWin32ControlUnit/Screencap/DesktopDupWindowScreencap.cpp index 31d2b08914..9791e212bb 100644 --- a/source/MaaWin32ControlUnit/Screencap/DesktopDupWindowScreencap.cpp +++ b/source/MaaWin32ControlUnit/Screencap/DesktopDupWindowScreencap.cpp @@ -110,4 +110,3 @@ RECT DesktopDupWindowScreencap::get_output_desktop_coordinates() const } MAA_CTRL_UNIT_NS_END - diff --git a/source/MaaWin32ControlUnit/Screencap/DesktopDupWindowScreencap.h b/source/MaaWin32ControlUnit/Screencap/DesktopDupWindowScreencap.h index e183473883..02ec93499d 100644 --- a/source/MaaWin32ControlUnit/Screencap/DesktopDupWindowScreencap.h +++ b/source/MaaWin32ControlUnit/Screencap/DesktopDupWindowScreencap.h @@ -27,4 +27,3 @@ class DesktopDupWindowScreencap : public DesktopDupScreencap }; MAA_CTRL_UNIT_NS_END - diff --git a/source/MaaWin32ControlUnit/Screencap/ForegroundScreencap.h b/source/MaaWin32ControlUnit/Screencap/ForegroundScreencap.h new file mode 100644 index 0000000000..5dba45b662 --- /dev/null +++ b/source/MaaWin32ControlUnit/Screencap/ForegroundScreencap.h @@ -0,0 +1,61 @@ +#pragma once + +#include +#include +#include + +#include "Base/ForegroundUtils.h" +#include "Base/UnitBase.h" + +#include "Common/Conf.h" + +MAA_CTRL_UNIT_NS_BEGIN + +class ForegroundScreencap : public ScreencapBase +{ +public: + ForegroundScreencap(HWND hwnd, std::shared_ptr inner) + : hwnd_(hwnd) + , inner_(std::move(inner)) + { + } + + virtual ~ForegroundScreencap() override = default; + +public: // from ScreencapBase + virtual std::optional screencap() override + { + if (!ensure_foreground_with_cooldown()) { + return std::nullopt; + } + return inner_->screencap(); + } + + virtual void inactive() override { inner_->inactive(); } + +private: + bool ensure_foreground_with_cooldown() + { + constexpr auto kForegroundRecoveryInterval = std::chrono::seconds(5); + + if (hwnd_ == GetForegroundWindow()) { + return true; + } + + auto now = std::chrono::steady_clock::now(); + if (last_foreground_attempt_ != std::chrono::steady_clock::time_point::min() && + now - last_foreground_attempt_ < kForegroundRecoveryInterval) { + return false; + } + + last_foreground_attempt_ = now; + ensure_foreground_and_topmost(hwnd_); + return hwnd_ == GetForegroundWindow(); + } + + HWND hwnd_ = nullptr; + std::shared_ptr inner_ = nullptr; + std::chrono::steady_clock::time_point last_foreground_attempt_ = std::chrono::steady_clock::time_point::min(); +}; + +MAA_CTRL_UNIT_NS_END diff --git a/source/MaaWin32ControlUnit/Screencap/ScreenDCScreencap.cpp b/source/MaaWin32ControlUnit/Screencap/ScreenDCScreencap.cpp index 536b7be3fa..47a11270ce 100644 --- a/source/MaaWin32ControlUnit/Screencap/ScreenDCScreencap.cpp +++ b/source/MaaWin32ControlUnit/Screencap/ScreenDCScreencap.cpp @@ -107,4 +107,3 @@ std::optional ScreenDCScreencap::screencap() } MAA_CTRL_UNIT_NS_END - diff --git a/source/MaaWin32ControlUnit/Screencap/ScreenDCScreencap.h b/source/MaaWin32ControlUnit/Screencap/ScreenDCScreencap.h index 20d3e89ab6..a08e3de9d1 100644 --- a/source/MaaWin32ControlUnit/Screencap/ScreenDCScreencap.h +++ b/source/MaaWin32ControlUnit/Screencap/ScreenDCScreencap.h @@ -25,4 +25,3 @@ class ScreenDCScreencap : public ScreencapBase }; MAA_CTRL_UNIT_NS_END - From 893d0d3826687b2b9a416e1dd42825fb91e1158e Mon Sep 17 00:00:00 2001 From: HarcourtC Date: Mon, 18 May 2026 10:53:10 +0800 Subject: [PATCH 3/7] =?UTF-8?q?chore(format):=20=E8=A1=A5=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E6=A0=BC=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/MaaWin32ControlUnit/Manager/Win32ControlUnitMgr.cpp | 2 +- source/MaaWin32ControlUnit/Screencap/ForegroundScreencap.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/source/MaaWin32ControlUnit/Manager/Win32ControlUnitMgr.cpp b/source/MaaWin32ControlUnit/Manager/Win32ControlUnitMgr.cpp index c361195434..68a2037774 100644 --- a/source/MaaWin32ControlUnit/Manager/Win32ControlUnitMgr.cpp +++ b/source/MaaWin32ControlUnit/Manager/Win32ControlUnitMgr.cpp @@ -12,9 +12,9 @@ #include "Input/SeizeInput.h" #include "Screencap/DesktopDupScreencap.h" #include "Screencap/DesktopDupWindowScreencap.h" +#include "Screencap/ForegroundScreencap.h" #include "Screencap/FramePoolScreencap.h" #include "Screencap/FramePoolWithPseudoMinimizeScreencap.h" -#include "Screencap/ForegroundScreencap.h" #include "Screencap/GdiScreencap.h" #include "Screencap/HwndUtils.hpp" #include "Screencap/PrintWindowScreencap.h" diff --git a/source/MaaWin32ControlUnit/Screencap/ForegroundScreencap.h b/source/MaaWin32ControlUnit/Screencap/ForegroundScreencap.h index 5dba45b662..c8dab63071 100644 --- a/source/MaaWin32ControlUnit/Screencap/ForegroundScreencap.h +++ b/source/MaaWin32ControlUnit/Screencap/ForegroundScreencap.h @@ -43,8 +43,8 @@ class ForegroundScreencap : public ScreencapBase } auto now = std::chrono::steady_clock::now(); - if (last_foreground_attempt_ != std::chrono::steady_clock::time_point::min() && - now - last_foreground_attempt_ < kForegroundRecoveryInterval) { + if (last_foreground_attempt_ != std::chrono::steady_clock::time_point::min() + && now - last_foreground_attempt_ < kForegroundRecoveryInterval) { return false; } From 0891cdf3191d1ff9349580a67344bc8fd9895768 Mon Sep 17 00:00:00 2001 From: HarcourtC Date: Tue, 19 May 2026 08:19:58 +0800 Subject: [PATCH 4/7] =?UTF-8?q?refactor(win32):=20=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E8=BF=87=E5=BA=A6=E5=8C=85=E8=A3=85=EF=BC=8C=E7=B2=BE=E7=AE=80?= =?UTF-8?q?=E5=BB=B6=E6=97=B6=E9=80=BB=E8=BE=91=20-=20ForegroundScreenCap?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E7=A7=BB=E9=99=A4=EF=BC=8C=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E5=9C=A8DesktopDupWindowScreenCap=E4=B8=AD=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E8=B0=83=E7=94=A8ForegroundUtils=E8=8E=B7=E5=8F=96=E5=89=8D?= =?UTF-8?q?=E5=8F=B0=E7=AA=97=E5=8F=A3=E4=BF=A1=E6=81=AF=20-=20=E5=BB=B6?= =?UTF-8?q?=E8=BF=9F=E5=86=B7=E5=8D=B4=E8=B0=83=E6=95=B4=E5=88=B0Foregroun?= =?UTF-8?q?dUtils=E4=B8=AD=EF=BC=8C=E7=AE=80=E5=8C=96ScreenCap=E7=9A=84?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Base/ForegroundUtils.h | 23 +++++++ .../Manager/Win32ControlUnitMgr.cpp | 7 +-- .../Screencap/DesktopDupWindowScreencap.cpp | 5 ++ .../Screencap/ForegroundScreencap.h | 61 ------------------- .../Screencap/ScreenDCScreencap.cpp | 5 ++ 5 files changed, 35 insertions(+), 66 deletions(-) delete mode 100644 source/MaaWin32ControlUnit/Screencap/ForegroundScreencap.h diff --git a/source/MaaWin32ControlUnit/Base/ForegroundUtils.h b/source/MaaWin32ControlUnit/Base/ForegroundUtils.h index d6a33fea5a..82e3c32b50 100644 --- a/source/MaaWin32ControlUnit/Base/ForegroundUtils.h +++ b/source/MaaWin32ControlUnit/Base/ForegroundUtils.h @@ -33,4 +33,27 @@ inline void ensure_foreground_and_topmost(HWND hwnd) } } +inline bool ensure_foreground_with_cooldown(HWND hwnd) +{ + constexpr DWORD kForegroundRecoveryInterval = 5000; + static DWORD last_foreground_attempt = 0; + + if (!hwnd || !IsWindow(hwnd)) { + return false; + } + + if (hwnd == GetForegroundWindow()) { + return true; + } + + DWORD now = GetTickCount(); + if (last_foreground_attempt != 0 && now - last_foreground_attempt < kForegroundRecoveryInterval) { + return false; + } + + last_foreground_attempt = now; + ensure_foreground_and_topmost(hwnd); + return hwnd == GetForegroundWindow(); +} + MAA_CTRL_UNIT_NS_END diff --git a/source/MaaWin32ControlUnit/Manager/Win32ControlUnitMgr.cpp b/source/MaaWin32ControlUnit/Manager/Win32ControlUnitMgr.cpp index 68a2037774..0ffd53a429 100644 --- a/source/MaaWin32ControlUnit/Manager/Win32ControlUnitMgr.cpp +++ b/source/MaaWin32ControlUnit/Manager/Win32ControlUnitMgr.cpp @@ -12,7 +12,6 @@ #include "Input/SeizeInput.h" #include "Screencap/DesktopDupScreencap.h" #include "Screencap/DesktopDupWindowScreencap.h" -#include "Screencap/ForegroundScreencap.h" #include "Screencap/FramePoolScreencap.h" #include "Screencap/FramePoolWithPseudoMinimizeScreencap.h" #include "Screencap/GdiScreencap.h" @@ -98,14 +97,12 @@ std::unordered_map(hwnd_); }); add(MaaWin32ScreencapMethod_DXGI_DesktopDup_Window, ScreencapMethod::DXGI_DesktopDup_Window, [this] { - return std::make_shared(hwnd_, std::make_shared(hwnd_)); + return std::make_shared(hwnd_); }); add(MaaWin32ScreencapMethod_PrintWindow, ScreencapMethod::PrintWindow, [this] { return std::make_shared(hwnd_); }); - add(MaaWin32ScreencapMethod_ScreenDC, ScreencapMethod::ScreenDC, [this] { - return std::make_shared(hwnd_, std::make_shared(hwnd_)); - }); + add(MaaWin32ScreencapMethod_ScreenDC, ScreencapMethod::ScreenDC, [this] { return std::make_shared(hwnd_); }); return units; } diff --git a/source/MaaWin32ControlUnit/Screencap/DesktopDupWindowScreencap.cpp b/source/MaaWin32ControlUnit/Screencap/DesktopDupWindowScreencap.cpp index 9791e212bb..bd36fc6aaf 100644 --- a/source/MaaWin32ControlUnit/Screencap/DesktopDupWindowScreencap.cpp +++ b/source/MaaWin32ControlUnit/Screencap/DesktopDupWindowScreencap.cpp @@ -2,6 +2,7 @@ #include +#include "Base/ForegroundUtils.h" #include "HwndUtils.hpp" #include "MaaUtils/Logger.h" @@ -14,6 +15,10 @@ std::optional DesktopDupWindowScreencap::screencap() return std::nullopt; } + if (!ensure_foreground_with_cooldown(hwnd_)) { + return std::nullopt; + } + // Ensure the window is fully visible on the monitor before screencap if (!ensure_window_on_screen(hwnd_)) { LogWarn << "Failed to ensure window on screen"; diff --git a/source/MaaWin32ControlUnit/Screencap/ForegroundScreencap.h b/source/MaaWin32ControlUnit/Screencap/ForegroundScreencap.h deleted file mode 100644 index c8dab63071..0000000000 --- a/source/MaaWin32ControlUnit/Screencap/ForegroundScreencap.h +++ /dev/null @@ -1,61 +0,0 @@ -#pragma once - -#include -#include -#include - -#include "Base/ForegroundUtils.h" -#include "Base/UnitBase.h" - -#include "Common/Conf.h" - -MAA_CTRL_UNIT_NS_BEGIN - -class ForegroundScreencap : public ScreencapBase -{ -public: - ForegroundScreencap(HWND hwnd, std::shared_ptr inner) - : hwnd_(hwnd) - , inner_(std::move(inner)) - { - } - - virtual ~ForegroundScreencap() override = default; - -public: // from ScreencapBase - virtual std::optional screencap() override - { - if (!ensure_foreground_with_cooldown()) { - return std::nullopt; - } - return inner_->screencap(); - } - - virtual void inactive() override { inner_->inactive(); } - -private: - bool ensure_foreground_with_cooldown() - { - constexpr auto kForegroundRecoveryInterval = std::chrono::seconds(5); - - if (hwnd_ == GetForegroundWindow()) { - return true; - } - - auto now = std::chrono::steady_clock::now(); - if (last_foreground_attempt_ != std::chrono::steady_clock::time_point::min() - && now - last_foreground_attempt_ < kForegroundRecoveryInterval) { - return false; - } - - last_foreground_attempt_ = now; - ensure_foreground_and_topmost(hwnd_); - return hwnd_ == GetForegroundWindow(); - } - - HWND hwnd_ = nullptr; - std::shared_ptr inner_ = nullptr; - std::chrono::steady_clock::time_point last_foreground_attempt_ = std::chrono::steady_clock::time_point::min(); -}; - -MAA_CTRL_UNIT_NS_END diff --git a/source/MaaWin32ControlUnit/Screencap/ScreenDCScreencap.cpp b/source/MaaWin32ControlUnit/Screencap/ScreenDCScreencap.cpp index 47a11270ce..a6fc7ac0de 100644 --- a/source/MaaWin32ControlUnit/Screencap/ScreenDCScreencap.cpp +++ b/source/MaaWin32ControlUnit/Screencap/ScreenDCScreencap.cpp @@ -1,5 +1,6 @@ #include "ScreenDCScreencap.h" +#include "Base/ForegroundUtils.h" #include "HwndUtils.hpp" #include "MaaUtils/Logger.h" @@ -12,6 +13,10 @@ std::optional ScreenDCScreencap::screencap() return std::nullopt; } + if (!ensure_foreground_with_cooldown(hwnd_)) { + return std::nullopt; + } + // Ensure the window is fully visible on the monitor before screencap if (!ensure_window_on_screen(hwnd_)) { LogWarn << "Failed to ensure window on screen"; From 2675b89ea242910c3f738cf6dcabd81925379081 Mon Sep 17 00:00:00 2001 From: zmdyy0318 Date: Tue, 19 May 2026 23:14:40 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E7=AA=97=E5=8F=A3=E7=BD=AE=E9=A1=B6?= =?UTF-8?q?=E8=AD=A6=E5=91=8A=E4=BB=A3=E6=9B=BF=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MaaWin32ControlUnit/Screencap/DesktopDupWindowScreencap.cpp | 2 +- source/MaaWin32ControlUnit/Screencap/ScreenDCScreencap.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/source/MaaWin32ControlUnit/Screencap/DesktopDupWindowScreencap.cpp b/source/MaaWin32ControlUnit/Screencap/DesktopDupWindowScreencap.cpp index bd36fc6aaf..dd6ff1b749 100644 --- a/source/MaaWin32ControlUnit/Screencap/DesktopDupWindowScreencap.cpp +++ b/source/MaaWin32ControlUnit/Screencap/DesktopDupWindowScreencap.cpp @@ -16,7 +16,7 @@ std::optional DesktopDupWindowScreencap::screencap() } if (!ensure_foreground_with_cooldown(hwnd_)) { - return std::nullopt; + LogWarn << "Failed to ensure foreground window before screencap"; } // Ensure the window is fully visible on the monitor before screencap diff --git a/source/MaaWin32ControlUnit/Screencap/ScreenDCScreencap.cpp b/source/MaaWin32ControlUnit/Screencap/ScreenDCScreencap.cpp index a6fc7ac0de..6f4da0f85c 100644 --- a/source/MaaWin32ControlUnit/Screencap/ScreenDCScreencap.cpp +++ b/source/MaaWin32ControlUnit/Screencap/ScreenDCScreencap.cpp @@ -14,7 +14,7 @@ std::optional ScreenDCScreencap::screencap() } if (!ensure_foreground_with_cooldown(hwnd_)) { - return std::nullopt; + LogWarn << "Failed to ensure foreground window before screencap"; } // Ensure the window is fully visible on the monitor before screencap From ffaf9e5b95399e7b7a76be924bfc55ebff8f56fd Mon Sep 17 00:00:00 2001 From: zmdyy0318 Date: Tue, 19 May 2026 23:54:27 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dai=E5=BB=BA=E8=AE=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/MaaWin32ControlUnit/Base/ForegroundUtils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/MaaWin32ControlUnit/Base/ForegroundUtils.h b/source/MaaWin32ControlUnit/Base/ForegroundUtils.h index 82e3c32b50..b372973813 100644 --- a/source/MaaWin32ControlUnit/Base/ForegroundUtils.h +++ b/source/MaaWin32ControlUnit/Base/ForegroundUtils.h @@ -48,7 +48,7 @@ inline bool ensure_foreground_with_cooldown(HWND hwnd) DWORD now = GetTickCount(); if (last_foreground_attempt != 0 && now - last_foreground_attempt < kForegroundRecoveryInterval) { - return false; + return true; } last_foreground_attempt = now; From 63dc3805eaae00c3f7b7de1970a0ff5ac7bfc31a Mon Sep 17 00:00:00 2001 From: zmdyy0318 Date: Wed, 20 May 2026 00:02:48 +0800 Subject: [PATCH 7/7] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- source/MaaWin32ControlUnit/Base/ForegroundUtils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/MaaWin32ControlUnit/Base/ForegroundUtils.h b/source/MaaWin32ControlUnit/Base/ForegroundUtils.h index b372973813..14213f25eb 100644 --- a/source/MaaWin32ControlUnit/Base/ForegroundUtils.h +++ b/source/MaaWin32ControlUnit/Base/ForegroundUtils.h @@ -48,7 +48,7 @@ inline bool ensure_foreground_with_cooldown(HWND hwnd) DWORD now = GetTickCount(); if (last_foreground_attempt != 0 && now - last_foreground_attempt < kForegroundRecoveryInterval) { - return true; + return hwnd == GetForegroundWindow(); } last_foreground_attempt = now;