2019-09-05 00:26:26 +08:00
|
|
|
#include "pch.h"
|
2019-12-17 16:21:46 +08:00
|
|
|
|
|
|
|
#include "common.h"
|
2019-09-05 00:26:26 +08:00
|
|
|
#include "monitors.h"
|
|
|
|
|
2020-03-05 18:07:06 +08:00
|
|
|
bool operator==(const ScreenSize& lhs, const ScreenSize& rhs)
|
|
|
|
{
|
|
|
|
auto lhs_tuple = std::make_tuple(lhs.rect.left, lhs.rect.right, lhs.rect.top, lhs.rect.bottom);
|
|
|
|
auto rhs_tuple = std::make_tuple(rhs.rect.left, rhs.rect.right, rhs.rect.top, rhs.rect.bottom);
|
|
|
|
return lhs_tuple == rhs_tuple;
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
2020-03-05 18:07:06 +08:00
|
|
|
static BOOL CALLBACK get_displays_enum_cb(HMONITOR monitor, HDC hdc, LPRECT rect, LPARAM data)
|
|
|
|
{
|
|
|
|
MONITORINFOEX monitor_info;
|
|
|
|
monitor_info.cbSize = sizeof(MONITORINFOEX);
|
|
|
|
GetMonitorInfo(monitor, &monitor_info);
|
|
|
|
reinterpret_cast<std::vector<MonitorInfo>*>(data)->emplace_back(monitor, monitor_info.rcWork);
|
|
|
|
return true;
|
2019-09-05 00:26:26 +08:00
|
|
|
};
|
|
|
|
|
2020-03-05 18:07:06 +08:00
|
|
|
static BOOL CALLBACK get_displays_enum_cb_with_toolbar(HMONITOR monitor, HDC hdc, LPRECT rect, LPARAM data)
|
|
|
|
{
|
|
|
|
MONITORINFOEX monitor_info;
|
|
|
|
monitor_info.cbSize = sizeof(MONITORINFOEX);
|
|
|
|
GetMonitorInfo(monitor, &monitor_info);
|
|
|
|
reinterpret_cast<std::vector<MonitorInfo>*>(data)->emplace_back(monitor, monitor_info.rcMonitor);
|
|
|
|
return true;
|
2019-09-05 00:26:26 +08:00
|
|
|
};
|
|
|
|
|
2020-03-05 18:07:06 +08:00
|
|
|
std::vector<MonitorInfo> MonitorInfo::GetMonitors(bool include_toolbar)
|
|
|
|
{
|
|
|
|
std::vector<MonitorInfo> monitors;
|
|
|
|
EnumDisplayMonitors(NULL, NULL, include_toolbar ? get_displays_enum_cb_with_toolbar : get_displays_enum_cb, reinterpret_cast<LPARAM>(&monitors));
|
|
|
|
std::sort(begin(monitors), end(monitors), [](const MonitorInfo& lhs, const MonitorInfo& rhs) {
|
|
|
|
return lhs.rect < rhs.rect;
|
2019-09-05 00:26:26 +08:00
|
|
|
});
|
2020-03-05 18:07:06 +08:00
|
|
|
return monitors;
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
2020-04-15 18:34:11 +08:00
|
|
|
int MonitorInfo::GetMonitorsCount()
|
|
|
|
{
|
|
|
|
return GetMonitors(true).size();
|
|
|
|
}
|
|
|
|
|
2020-03-05 18:07:06 +08:00
|
|
|
static BOOL CALLBACK get_primary_display_enum_cb(HMONITOR monitor, HDC hdc, LPRECT rect, LPARAM data)
|
|
|
|
{
|
|
|
|
MONITORINFOEX monitor_info;
|
|
|
|
monitor_info.cbSize = sizeof(MONITORINFOEX);
|
|
|
|
GetMonitorInfo(monitor, &monitor_info);
|
|
|
|
if (monitor_info.dwFlags & MONITORINFOF_PRIMARY)
|
|
|
|
{
|
|
|
|
reinterpret_cast<MonitorInfo*>(data)->handle = monitor;
|
|
|
|
reinterpret_cast<MonitorInfo*>(data)->rect = monitor_info.rcWork;
|
|
|
|
}
|
|
|
|
return true;
|
2019-09-05 00:26:26 +08:00
|
|
|
};
|
|
|
|
|
2020-03-05 18:07:06 +08:00
|
|
|
MonitorInfo MonitorInfo::GetPrimaryMonitor()
|
|
|
|
{
|
|
|
|
MonitorInfo primary({}, {});
|
|
|
|
EnumDisplayMonitors(NULL, NULL, get_primary_display_enum_cb, reinterpret_cast<LPARAM>(&primary));
|
|
|
|
return primary;
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
2020-03-05 18:07:06 +08:00
|
|
|
MonitorInfo MonitorInfo::GetFromWindow(HWND hwnd)
|
|
|
|
{
|
|
|
|
auto monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
|
|
|
|
return GetFromHandle(monitor);
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
2020-03-05 18:07:06 +08:00
|
|
|
MonitorInfo MonitorInfo::GetFromPoint(POINT p)
|
|
|
|
{
|
|
|
|
auto monitor = MonitorFromPoint(p, MONITOR_DEFAULTTONEAREST);
|
|
|
|
return GetFromHandle(monitor);
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
2020-03-05 18:07:06 +08:00
|
|
|
MonitorInfo MonitorInfo::GetFromHandle(HMONITOR monitor)
|
|
|
|
{
|
|
|
|
MONITORINFOEX monitor_info;
|
|
|
|
monitor_info.cbSize = sizeof(MONITORINFOEX);
|
|
|
|
GetMonitorInfo(monitor, &monitor_info);
|
|
|
|
return MonitorInfo(monitor, monitor_info.rcWork);
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|