2019-09-05 00:26:26 +08:00
|
|
|
#include "pch.h"
|
|
|
|
#include "dpi_aware.h"
|
|
|
|
#include "monitors.h"
|
|
|
|
#include <ShellScalingApi.h>
|
|
|
|
|
|
|
|
HRESULT DPIAware::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-28 22:29:29 +08:00
|
|
|
HRESULT DPIAware::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-05 00:26:26 +08:00
|
|
|
void DPIAware::Convert(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 * dpi_x / DEFAULT_DPI;
|
|
|
|
height = height * dpi_y / DEFAULT_DPI;
|
|
|
|
}
|
2019-11-08 02:56:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DPIAware::EnableDPIAwarenessForThisProcess() {
|
|
|
|
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
|
|
|
|
}
|