mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 04:07:40 +02:00
format dpi_aware
This commit is contained in:
@@ -5,60 +5,71 @@
|
|||||||
|
|
||||||
namespace DPIAware
|
namespace DPIAware
|
||||||
{
|
{
|
||||||
HRESULT GetScreenDPIForWindow(HWND hwnd, UINT &dpi_x, UINT &dpi_y) {
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void EnableDPIAwarenessForThisProcess() {
|
|
||||||
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
|
|
||||||
}
|
|
||||||
|
|
||||||
AWARENESS_LEVEL GetAwarenessLevel(DPI_AWARENESS_CONTEXT system_returned_value)
|
|
||||||
{
|
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
if(AreDpiAwarenessContextsEqual(levels[i], system_returned_value))
|
auto monitor_handle = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
|
||||||
|
dpi_x = 0;
|
||||||
|
dpi_y = 0;
|
||||||
|
if (monitor_handle != nullptr)
|
||||||
{
|
{
|
||||||
return static_cast<AWARENESS_LEVEL>(i);
|
return GetDpiForMonitor(monitor_handle, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return E_FAIL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return AWARENESS_LEVEL::UNAWARE;
|
|
||||||
}
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnableDPIAwarenessForThisProcess()
|
||||||
|
{
|
||||||
|
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
|
||||||
|
}
|
||||||
|
|
||||||
|
AWARENESS_LEVEL GetAwarenessLevel(DPI_AWARENESS_CONTEXT system_returned_value)
|
||||||
|
{
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
if (AreDpiAwarenessContextsEqual(levels[i], system_returned_value))
|
||||||
|
{
|
||||||
|
return static_cast<AWARENESS_LEVEL>(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return AWARENESS_LEVEL::UNAWARE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,22 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "windef.h"
|
#include "windef.h"
|
||||||
|
|
||||||
namespace DPIAware {
|
namespace DPIAware
|
||||||
constexpr inline int DEFAULT_DPI = 96;
|
{
|
||||||
|
constexpr inline int DEFAULT_DPI = 96;
|
||||||
|
|
||||||
HRESULT GetScreenDPIForWindow(HWND hwnd, UINT & dpi_x, UINT & dpi_y);
|
HRESULT GetScreenDPIForWindow(HWND hwnd, UINT& dpi_x, UINT& dpi_y);
|
||||||
HRESULT GetScreenDPIForPoint(POINT p, UINT& dpi_x, UINT& dpi_y);
|
HRESULT GetScreenDPIForPoint(POINT p, UINT& dpi_x, UINT& dpi_y);
|
||||||
void Convert(HMONITOR monitor_handle, int &width, int &height);
|
void Convert(HMONITOR monitor_handle, int& width, int& height);
|
||||||
void EnableDPIAwarenessForThisProcess();
|
void EnableDPIAwarenessForThisProcess();
|
||||||
|
|
||||||
enum class AWARENESS_LEVEL
|
enum AWARENESS_LEVEL
|
||||||
{
|
{
|
||||||
UNAWARE,
|
UNAWARE,
|
||||||
SYSTEM_AWARE,
|
SYSTEM_AWARE,
|
||||||
PER_MONITOR_AWARE,
|
PER_MONITOR_AWARE,
|
||||||
PER_MONITOR_AWARE_V2,
|
PER_MONITOR_AWARE_V2,
|
||||||
UNAWARE_GDISCALED
|
UNAWARE_GDISCALED
|
||||||
};
|
};
|
||||||
AWARENESS_LEVEL GetAwarenessLevel(DPI_AWARENESS_CONTEXT system_returned_value);
|
AWARENESS_LEVEL GetAwarenessLevel(DPI_AWARENESS_CONTEXT system_returned_value);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user