Skip to content

Commit

Permalink
chore: 一些实现整理
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Nov 16, 2023
1 parent 7171ffb commit 97db7e9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 38 deletions.
2 changes: 1 addition & 1 deletion include/MaaFramework/MaaDef.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ enum MaaWin32ControllerTypeEnum
MaaWin32ControllerType_Key_SendMessage = 1 << 8,
MaaWin32ControllerType_Key_Mask = 0xFF00,

MaaWin32ControllerType_Screencap_HWND = 1 << 16,
MaaWin32ControllerType_Screencap_GDI = 1 << 16,
MaaWin32ControllerType_Screencap_Mask = 0xFF0000,
};
typedef void* MaaWin32Hwnd;
Expand Down
2 changes: 1 addition & 1 deletion sample/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ MaaControllerHandle create_adb_controller()
MaaControllerHandle create_win32_controller()
{
auto hwnd = MaaToolKitGetCursorWindow();
auto type = MaaWin32ControllerType_Touch_SendMessage | MaaWin32ControllerType_Screencap_HWND;
auto type = MaaWin32ControllerType_Touch_SendMessage | MaaWin32ControllerType_Screencap_GDI;
return MaaWin32ControllerCreate(hwnd, type, nullptr, nullptr);
}

Expand Down
8 changes: 4 additions & 4 deletions source/MaaWin32ControlUnit/API/Win32ControlUnitAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "Base/UnitBase.h"
#include "Input/SendMessageInput.h"
#include "Manager/ControlUnitMgr.h"
#include "Screencap/HwndScreencap.h"
#include "Screencap/GdiScreencap.h"
#include "Utils/Logger.h"
#include "Utils/SafeWindows.hpp"

Expand Down Expand Up @@ -48,9 +48,9 @@ MaaControlUnitHandle MaaWin32ControlUnitCreate( //
}

switch (screencap_type) {
case MaaWin32ControllerType_Screencap_HWND:
LogInfo << "screencap_type: HWND";
screencap_unit = std::make_shared<HwndScreencap>(h_wnd);
case MaaWin32ControllerType_Screencap_GDI:
LogInfo << "screencap_type: GDI";
screencap_unit = std::make_shared<GdiScreencap>(h_wnd);
break;

default:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,25 @@
#include "HwndScreencap.h"
#include "GdiScreencap.h"

#include <functional>

#include "Utils/Logger.h"

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<cv::Mat> HwndScreencap::screencap()
std::optional<cv::Mat> GdiScreencap::screencap()
{
if (!hwnd_) {
LogError << "hwnd_ is nullptr";
return std::nullopt;
}

RECT rect;
RECT rect { 0 };
if (!GetClientRect(hwnd_, &rect)) {
LogError << "GetClientRect failed, error code: " << GetLastError();
return std::nullopt;
}

double screen_scale = get_window_screen_scale();
double screen_scale = window_screen_scale();

int width = static_cast<int>(screen_scale * (rect.right - rect.left));
int height = static_cast<int>(screen_scale * (rect.bottom - rect.top));
Expand Down Expand Up @@ -107,4 +82,33 @@ std::optional<cv::Mat> HwndScreencap::screencap()
return mat;
}

double GdiScreencap::window_screen_scale()
{
// TODO: 处理多显示器不同 DPI 的情况

#ifndef MAA_WIN32_COMPATIBLE

constexpr double kStandardDPI = 96.0;
// 运行期需要 Win10 1607 以上版本
return GetDpiForWindow(GetDesktopWindow()) / kStandardDPI;

#else

HMONITOR monitor_handle = MonitorFromWindow(GetDesktopWindow(), MONITOR_DEFAULTTONEAREST);

MONITORINFOEX miex { 0 };
miex.cbSize = sizeof(miex);
GetMonitorInfo(monitor_handle, &miex);
LONG screen_x_logical = miex.rcMonitor.right - miex.rcMonitor.left;

DEVMODE dm { 0 };
dm.dmSize = sizeof(dm);
EnumDisplaySettings(miex.szDevice, ENUM_CURRENT_SETTINGS, &dm);
DWORD screen_x_physical = dm.dmPelsWidth;

return static_cast<double>(screen_x_physical) / static_cast<double>(screen_x_logical);

#endif
}

MAA_CTRL_UNIT_NS_END
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@

MAA_CTRL_UNIT_NS_BEGIN

class HwndScreencap : public ScreencapBase
class GdiScreencap : public ScreencapBase
{
public:
HwndScreencap(HWND hwnd) : hwnd_(hwnd) {}
virtual ~HwndScreencap() override = default;
GdiScreencap(HWND hwnd) : hwnd_(hwnd) {}
virtual ~GdiScreencap() override = default;

public: // from TouchInputBase
virtual std::optional<cv::Mat> screencap() override;

private:
double window_screen_scale();

HWND hwnd_;
};

Expand Down

0 comments on commit 97db7e9

Please sign in to comment.