From 7171ffbf597794a6ec98d1a6016440f6a0739941 Mon Sep 17 00:00:00 2001 From: Yu SuiXian <47711102+GengGode@users.noreply.github.com> Date: Thu, 16 Nov 2023 22:10:32 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dwin32=E6=88=AA?= =?UTF-8?q?=E5=9B=BE=20(#103)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Screencap/HwndScreencap.cpp | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/source/MaaWin32ControlUnit/Screencap/HwndScreencap.cpp b/source/MaaWin32ControlUnit/Screencap/HwndScreencap.cpp index 3461ac974..52d2037ae 100644 --- a/source/MaaWin32ControlUnit/Screencap/HwndScreencap.cpp +++ b/source/MaaWin32ControlUnit/Screencap/HwndScreencap.cpp @@ -6,6 +6,31 @@ MAA_CTRL_UNIT_NS_BEGIN +double get_window_screen_scale() +{ +#ifdef _WIN32_WINNT_WIN10 + //需要win10 1607以上版本 + double screen_scale = GetDpiForWindow(GetDesktopWindow()) / 96.0; +#else + HWND desktop_hwnd = GetDesktopWindow(); + HMONITOR monitor_handle = MonitorFromWindow(desktop_hwnd, MONITOR_DEFAULTTONEAREST); + + MONITORINFOEX miex; + miex.cbSize = sizeof(miex); + GetMonitorInfo(monitor_handle, &miex); + int screen_x_logical = (miex.rcMonitor.right - miex.rcMonitor.left); + + DEVMODE dm; + dm.dmSize = sizeof(dm); + dm.dmDriverExtra = 0; + EnumDisplaySettings(miex.szDevice, ENUM_CURRENT_SETTINGS, &dm); + int screen_x_physical = dm.dmPelsWidth; + + double screen_scale = ((double)screen_x_physical / (double)screen_x_logical); +#endif + return screen_scale; +} + std::optional HwndScreencap::screencap() { if (!hwnd_) { @@ -19,8 +44,10 @@ std::optional HwndScreencap::screencap() return std::nullopt; } - int width = rect.right - rect.left; - int height = rect.bottom - rect.top; + double screen_scale = get_window_screen_scale(); + + int width = static_cast(screen_scale * (rect.right - rect.left)); + int height = static_cast(screen_scale * (rect.bottom - rect.top)); HDC hdc = nullptr; HDC mem_dc = nullptr;