diff --git a/tools/dpitester/CMakeLists.txt b/tools/dpitester/CMakeLists.txt index fafff7f..4d00dc4 100644 --- a/tools/dpitester/CMakeLists.txt +++ b/tools/dpitester/CMakeLists.txt @@ -20,7 +20,7 @@ string(REGEX REPLACE "[-|/]GR" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) #string(REGEX REPLACE "[-|/]EHsc" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) string(REGEX REPLACE "[-|/]W[0|1|2|3|4]" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) string(REGEX REPLACE "[-|/]Ob[0|1|2|3]" "" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE}) -string(APPEND CMAKE_RC_FLAGS " /c65001 /nologo ") +string(APPEND CMAKE_RC_FLAGS " /c65001 /DWIN32 /nologo ") add_executable(${PROJECT_NAME}) @@ -32,14 +32,15 @@ set(_WIN32_WINNT_WIN10 0x0A00) set(NTDDI_WIN10_CO 0x0A00000B) target_compile_definitions(${PROJECT_NAME} PRIVATE _CRT_NON_CONFORMING_SWPRINTFS _CRT_SECURE_NO_WARNINGS - _ENABLE_EXTENDED_ALIGNED_STORAGE NOMINMAX UNICODE - _UNICODE WIN32_LEAN_AND_MEAN WINRT_LEAN_AND_MEAN + _CRT_SECURE_NO_DEPRECATE _CRT_NONSTDC_NO_WARNINGS + _CRT_NONSTDC_NO_DEPRECATE _ENABLE_EXTENDED_ALIGNED_STORAGE + NOMINMAX UNICODE _UNICODE WIN32_LEAN_AND_MEAN WINRT_LEAN_AND_MEAN WINVER=${_WIN32_WINNT_WIN10} _WIN32_WINNT=${_WIN32_WINNT_WIN10} _WIN32_IE=${_WIN32_WINNT_WIN10} NTDDI_VERSION=${NTDDI_WIN10_CO} ) target_compile_options(${PROJECT_NAME} PRIVATE - /await:strict /bigobj /d2FH4 /FS /GR- /MP /permissive- /utf-8 /W4 /WX /ZH:SHA_256 + /await:strict /bigobj /d2FH4 /EHsc /FS /GR- /MP /permissive- /utf-8 /W4 /WX /ZH:SHA_256 /Zc:char8_t,__cplusplus,externC,externConstexpr,hiddenFriend,lambda,preprocessor,referenceBinding,rvalueCast,strictStrings,ternary,throwingNew,trigraphs $<$:/JMC> $<$>:/guard:cf /guard:ehcont /GA /GT /Gw /Gy /Ob3 /Oi /Oy /QIntel-jcc-erratum /Qspectre-load /Zc:inline> diff --git a/tools/dpitester/app.manifest b/tools/dpitester/app.manifest index 745ceae..0356f4e 100644 --- a/tools/dpitester/app.manifest +++ b/tools/dpitester/app.manifest @@ -71,7 +71,7 @@ True/PM - PerMonitorV2, PerMonitor, System + PerMonitorV2, PerMonitor True True True diff --git a/tools/dpitester/main.cpp b/tools/dpitester/main.cpp index f5cb874..f2b9aab 100644 --- a/tools/dpitester/main.cpp +++ b/tools/dpitester/main.cpp @@ -217,8 +217,7 @@ void HiDPI::Initialize() std::wcerr << L"Failed to retrieve the handle of the SHCORE.DLL." << std::endl; } - const HINSTANCE instance = GetModuleHandleW(nullptr); - if (instance) { + if (const HINSTANCE instance = GetModuleHandleW(nullptr)) { WNDCLASSEXW wcex; SecureZeroMemory(&wcex, sizeof(wcex)); wcex.cbSize = sizeof(wcex); @@ -270,9 +269,8 @@ void HiDPI::Cleanup() [[nodiscard]] static inline UINT GetCurrentDPIForPrimaryScreen() { HiDPI::Initialize(); - if (HiDPI::GetDpiForMonitorPtr) { - const HMONITOR hMonitor = MonitorFromWindow(HiDPI::FakeWindow, MONITOR_DEFAULTTOPRIMARY); - if (hMonitor) { + if (const HMONITOR hMonitor = MonitorFromWindow(GetDesktopWindow(), MONITOR_DEFAULTTOPRIMARY)) { + if (HiDPI::GetDpiForMonitorPtr) { UINT dpiX = 0, dpiY = 0; const HRESULT hr = HiDPI::GetDpiForMonitorPtr(hMonitor, MDT_EFFECTIVE_DPI, &dpiX, &dpiY); if (SUCCEEDED(hr) && (dpiX > 0) && (dpiY > 0)) { @@ -280,12 +278,27 @@ void HiDPI::Cleanup() } else { std::wcerr << L"GetDpiForMonitor() failed." << std::endl; } - } else { - std::wcerr << L"Failed to retrieve the current monitor." << std::endl; } + MONITORINFOEXW monitorInfo; + SecureZeroMemory(&monitorInfo, sizeof(monitorInfo)); + monitorInfo.cbSize = sizeof(monitorInfo); + GetMonitorInfoW(hMonitor, &monitorInfo); + if (const HDC hdc = CreateDCW(monitorInfo.szDevice, monitorInfo.szDevice, nullptr, nullptr)) { + const int dpiX = GetDeviceCaps(hdc, LOGPIXELSX); + const int dpiY = GetDeviceCaps(hdc, LOGPIXELSY); + DeleteDC(hdc); + if ((dpiX > 0) && (dpiY > 0)) { + return dpiX; + } else { + std::wcerr << L"Failed to retrieve the primary screen's DPI." << std::endl; + } + } else { + std::wcerr << L"Failed to create DC for the primary monitor." << std::endl; + } + } else { + std::wcerr << L"Failed to retrieve the primary monitor." << std::endl; } - const HDC hdc = GetDC(nullptr); - if (hdc) { + if (const HDC hdc = GetDC(nullptr)) { const int dpiX = GetDeviceCaps(hdc, LOGPIXELSX); const int dpiY = GetDeviceCaps(hdc, LOGPIXELSY); ReleaseDC(nullptr, hdc);