2019-09-04 18:26:26 +02:00
|
|
|
#include "pch.h"
|
|
|
|
|
#include "dpi_aware.h"
|
|
|
|
|
#include "monitors.h"
|
|
|
|
|
#include <ShellScalingApi.h>
|
|
|
|
|
|
2019-12-16 14:28:14 +03:00
|
|
|
namespace DPIAware
|
|
|
|
|
{
|
2019-12-17 12:32:56 +03:00
|
|
|
HRESULT GetScreenDPIForWindow(HWND hwnd, UINT& dpi_x, UINT& dpi_y)
|
|
|
|
|
{
|
|
|
|
|
auto monitor_handle = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
|
|
|
|
|
dpi_x = 0;
|
|
|
|
|
dpi_y = 0;
|
|
|
|
|
if (monitor_handle != nullptr)
|
|
|
|
|
{
|
|
|
|
|
return GetDpiForMonitor(monitor_handle, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return E_FAIL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-04 18:26:26 +02:00
|
|
|
|
2019-12-17 12:32:56 +03:00
|
|
|
HRESULT GetScreenDPIForPoint(POINT p, UINT& dpi_x, UINT& dpi_y)
|
|
|
|
|
{
|
|
|
|
|
auto monitor_handle = MonitorFromPoint(p, MONITOR_DEFAULTTONEAREST);
|
|
|
|
|
dpi_x = 0;
|
|
|
|
|
dpi_y = 0;
|
|
|
|
|
if (monitor_handle != nullptr)
|
|
|
|
|
{
|
|
|
|
|
return GetDpiForMonitor(monitor_handle, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return E_FAIL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-28 15:29:29 +01:00
|
|
|
|
2019-12-17 12:32:56 +03:00
|
|
|
void Convert(HMONITOR monitor_handle, int& width, int& height)
|
|
|
|
|
{
|
|
|
|
|
if (monitor_handle == NULL)
|
|
|
|
|
{
|
|
|
|
|
const POINT ptZero = { 0, 0 };
|
|
|
|
|
monitor_handle = MonitorFromPoint(ptZero, MONITOR_DEFAULTTOPRIMARY);
|
|
|
|
|
}
|
2019-09-04 18:26:26 +02:00
|
|
|
|
2019-12-17 12:32:56 +03:00
|
|
|
UINT dpi_x, dpi_y;
|
|
|
|
|
if (GetDpiForMonitor(monitor_handle, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y) == S_OK)
|
|
|
|
|
{
|
2020-07-01 15:36:05 +02:00
|
|
|
width = width * static_cast<int>(dpi_x) / DEFAULT_DPI;
|
|
|
|
|
height = height * static_cast<int>(dpi_y) / DEFAULT_DPI;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void InverseConvert(HMONITOR monitor_handle, int& width, int& height)
|
|
|
|
|
{
|
|
|
|
|
if (monitor_handle == NULL)
|
|
|
|
|
{
|
|
|
|
|
const POINT ptZero = { 0, 0 };
|
|
|
|
|
monitor_handle = MonitorFromPoint(ptZero, MONITOR_DEFAULTTOPRIMARY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UINT dpi_x, dpi_y;
|
|
|
|
|
if (GetDpiForMonitor(monitor_handle, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y) == S_OK)
|
|
|
|
|
{
|
|
|
|
|
width = width * DEFAULT_DPI / static_cast<int>(dpi_x);
|
|
|
|
|
height = height * DEFAULT_DPI / static_cast<int>(dpi_y);
|
2019-12-17 12:32:56 +03:00
|
|
|
}
|
|
|
|
|
}
|
2019-11-07 21:56:32 +03:00
|
|
|
|
2019-12-17 12:32:56 +03:00
|
|
|
void EnableDPIAwarenessForThisProcess()
|
|
|
|
|
{
|
|
|
|
|
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
|
|
|
|
|
}
|
2019-12-16 14:28:14 +03:00
|
|
|
|
2020-05-27 10:58:47 -04:00
|
|
|
AwarenessLevel GetAwarenessLevel(DPI_AWARENESS_CONTEXT system_returned_value)
|
2019-12-16 14:28:14 +03:00
|
|
|
{
|
2019-12-17 12:32:56 +03:00
|
|
|
const std::array levels{ DPI_AWARENESS_CONTEXT_UNAWARE,
|
|
|
|
|
DPI_AWARENESS_CONTEXT_SYSTEM_AWARE,
|
|
|
|
|
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE,
|
|
|
|
|
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2,
|
|
|
|
|
DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED };
|
|
|
|
|
for (size_t i = 0; i < size(levels); ++i)
|
2019-12-16 14:28:14 +03:00
|
|
|
{
|
2019-12-17 12:32:56 +03:00
|
|
|
if (AreDpiAwarenessContextsEqual(levels[i], system_returned_value))
|
|
|
|
|
{
|
2020-05-27 10:58:47 -04:00
|
|
|
return static_cast<AwarenessLevel>(i);
|
2019-12-17 12:32:56 +03:00
|
|
|
}
|
2019-12-16 14:28:14 +03:00
|
|
|
}
|
2020-05-27 10:58:47 -04:00
|
|
|
return AwarenessLevel::UNAWARE;
|
2019-12-16 14:28:14 +03:00
|
|
|
}
|
|
|
|
|
}
|