#include "WinNativeDpiAwareness.h"
#pragma comment(lib,
"Shcore.lib"
)
namespace
{
namespace
{
namespace
{
#define USE_API(NAME, API) static auto proc_##API = (decltype(&API))(GetProcAddress(GetModuleHandle(L#NAME), #API))
void
(
bool
)
{
USE_API(user32, SetProcessDpiAwarenessContext);
static
auto
proc_SetProcessDpiAwarenessContext
= (
decltype
(&
SetProcessDpiAwarenessContext
))(
(
(
L"user32"
),
"SetProcessDpiAwarenessContext"
));
USE_API(shcore, SetProcessDpiAwareness);
static
auto
proc_SetProcessDpiAwareness
= (
decltype
(&
))(
(
(
L"shcore"
),
"SetProcessDpiAwareness"
));
if
(
proc_SetProcessDpiAwarenessContext
)
{
proc_SetProcessDpiAwarenessContext(dpiAware ? DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2: DPI_AWARENESS_CONTEXT_UNAWARE);
proc_SetProcessDpiAwarenessContext
(
? ((
)-
4
): ((
)-
1
));
return
;
}
if
(
proc_SetProcessDpiAwareness
)
{
proc_SetProcessDpiAwareness
(
?
PROCESS_PER_MONITOR_DPI_AWARE
:
);
return
;
}
}
void
(
,
*
,
*
)
{
USE_API(shcore, GetDpiForMonitor);
static
auto
= (
decltype
(&
))(
(
(
L"shcore"
),
"GetDpiForMonitor"
));
if
(
proc_GetDpiForMonitor
)
{
if (proc_GetDpiForMonitor(monitor, MDT_DEFAULT, x, y) == S_OK)
if
(
proc_GetDpiForMonitor
(
,
,
,
) == ((
)
0L
))
{
return
;
}
}
*
=
96
;
*
=
96
;
}
void
DpiAwared_GetDpiForWindow
(
,
*
,
*
)
{
USE_API(user32, GetDpiForWindow);
static
auto
= (
decltype
(&
))(
(
(
L"user32"
),
"GetDpiForWindow"
));
if
(
proc_GetDpiForWindow
)
{
*
= *
=
proc_GetDpiForWindow
(
);
}
else
{
HMONITOR monitor = MonitorFromWindow(handle, MONITOR_DEFAULTTONULL);
{
*
= *
=
96
;
}
else
{
DpiAwared_GetDpiForMonitor
(
monitor
,
,
);
}
}
}
void
DpiAwared_AdjustWindowRect
(
,
,
)
{
USE_API(user32, AdjustWindowRectExForDpi);
static
auto
proc_AdjustWindowRectExForDpi
= (
decltype
(&
))(
(
(
L"user32"
),
"AdjustWindowRectExForDpi"
));
if
(
proc_AdjustWindowRectExForDpi
)
{
proc_AdjustWindowRectExForDpi(rect, (DWORD)GetWindowLongPtr(handle, GWL_STYLE), FALSE, (DWORD)GetWindowLongPtr(handle, GWL_EXSTYLE), dpi);
proc_AdjustWindowRectExForDpi
(
, (
)
(
, (-
16
)),
0
, (
)
(
, (-
20
)),
);
}
else
{
AdjustWindowRect(rect, (DWORD)GetWindowLongPtr(handle, GWL_STYLE), FALSE);
}
}
int
DpiAwared_GetSystemMetrics
(
int
,
)
{
USE_API(user32, GetSystemMetricsForDpi);
static
auto
proc_GetSystemMetricsForDpi
= (
decltype
(&
))(
(
(
L"user32"
),
"GetSystemMetricsForDpi"
));
if
(
proc_GetSystemMetricsForDpi
)
{
return
proc_GetSystemMetricsForDpi
(
,
);
}
else
{
return
(
);
}
}
#undef USE_API
}
}
}