From c8a2bcb6b0c32c93fd0501378ebaeaa25fd6e984 Mon Sep 17 00:00:00 2001 From: ozone10 Date: Wed, 13 Mar 2024 08:47:10 +0100 Subject: [PATCH] Use undocumented dpi API to support win7 Fix https://github.com/notepad-plus-plus/notepad-plus-plus/pull/14818#issuecomment-1993472569 Close #14855 --- PowerEditor/gcc/makefile | 2 +- PowerEditor/src/CMakeLists.txt | 4 +- PowerEditor/src/NppDarkMode.cpp | 18 ++----- PowerEditor/src/dpiManagerV2.cpp | 55 +++++++++++++++++--- PowerEditor/src/dpiManagerV2.h | 6 ++- PowerEditor/src/winmain.cpp | 2 + PowerEditor/visual.net/notepadPlus.Cpp.props | 2 +- 7 files changed, 64 insertions(+), 25 deletions(-) diff --git a/PowerEditor/gcc/makefile b/PowerEditor/gcc/makefile index 8c8854e7b..31c7104f5 100644 --- a/PowerEditor/gcc/makefile +++ b/PowerEditor/gcc/makefile @@ -41,7 +41,7 @@ CXXFLAGS := -include $(GCC_DIRECTORY)/gcc-fixes.h -std=c++20 RC := $(CROSS_COMPILE)windres RCFLAGS := CPP_PATH := $(SCINTILLA_DIRECTORY)/include $(LEXILLA_DIRECTORY)/include -CPP_DEFINE := UNICODE _UNICODE OEMRESOURCE NOMINMAX _WIN32_WINNT=_WIN32_WINNT_WIN10 NTDDI_VERSION=NTDDI_WIN10_RS5 TIXML_USE_STL TIXMLA_USE_STL +CPP_DEFINE := UNICODE _UNICODE OEMRESOURCE NOMINMAX _WIN32_WINNT=_WIN32_WINNT_WIN7 NTDDI_VERSION=NTDDI_WIN7 TIXML_USE_STL TIXMLA_USE_STL LD := $(CXX) LDFLAGS := -municode -mwindows LD_PATH := diff --git a/PowerEditor/src/CMakeLists.txt b/PowerEditor/src/CMakeLists.txt index 3297d4549..e1f69e43d 100644 --- a/PowerEditor/src/CMakeLists.txt +++ b/PowerEditor/src/CMakeLists.txt @@ -395,13 +395,13 @@ IF (WIN32) if ( MSVC ) #do not use for mingw builds SET(CMAKE_CXX_FLAGS "/EHa /MP /W4") - SET(defs -DUNICODE -D_UNICODE -D_WIN32_WINNT=_WIN32_WINNT_WIN10 -DNTDDI_VERSION=NTDDI_WIN10_RS5 -D_USE_64BIT_TIME_T -DTIXML_USE_STL -DTIXMLA_USE_STL -DNOMINMAX -DOEMRESOURCE -D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS -D_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING ) + SET(defs -DUNICODE -D_UNICODE -D_WIN32_WINNT=_WIN32_WINNT_WIN7 -DNTDDI_VERSION=NTDDI_WIN7 -D_USE_64BIT_TIME_T -DTIXML_USE_STL -DTIXMLA_USE_STL -DNOMINMAX -DOEMRESOURCE -D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS -D_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING ) set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd") else ( MSVC ) # For possible MinGW compilation SET(CMAKE_CXX_FLAGS "-include../gcc/gcc-fixes.h -std=c++20 -fpermissive -municode") - SET(defs -DUNICODE -D_UNICODE -D_WIN32_WINNT=_WIN32_WINNT_WIN10 -DNTDDI_VERSION=NTDDI_WIN10_RS5 -D_USE_64BIT_TIME_T -DTIXML_USE_STL -DTIXMLA_USE_STL -DNOMINMAX -DOEMRESOURCE) + SET(defs -DUNICODE -D_UNICODE -D_WIN32_WINNT=_WIN32_WINNT_WIN7 -DNTDDI_VERSION=NTDDI_WIN7 -D_USE_64BIT_TIME_T -DTIXML_USE_STL -DTIXMLA_USE_STL -DNOMINMAX -DOEMRESOURCE) endif ( MSVC ) ENDIF (WIN32) diff --git a/PowerEditor/src/NppDarkMode.cpp b/PowerEditor/src/NppDarkMode.cpp index 56b8c085b..75f5693ed 100644 --- a/PowerEditor/src/NppDarkMode.cpp +++ b/PowerEditor/src/NppDarkMode.cpp @@ -1600,20 +1600,10 @@ namespace NppDarkMode void setMetricsForDpi(UINT dpi) { _dpi = dpi; - if (NppDarkMode::isWindows10()) - { - _xEdge = ::GetSystemMetricsForDpi(SM_CXEDGE, _dpi); - _yEdge = ::GetSystemMetricsForDpi(SM_CYEDGE, _dpi); - _xScroll = ::GetSystemMetricsForDpi(SM_CXVSCROLL, _dpi); - _yScroll = ::GetSystemMetricsForDpi(SM_CYVSCROLL, _dpi); - } - else - { - _xEdge = DPIManagerV2::scale(::GetSystemMetrics(SM_CXEDGE), _dpi); - _yEdge = DPIManagerV2::scale(::GetSystemMetrics(SM_CYEDGE), _dpi); - _xScroll = DPIManagerV2::scale(::GetSystemMetrics(SM_CXVSCROLL), _dpi); - _yScroll = DPIManagerV2::scale(::GetSystemMetrics(SM_CYVSCROLL), _dpi); - } + _xEdge = DPIManagerV2::getSystemMetricsForDpi(SM_CXEDGE, _dpi); + _yEdge = DPIManagerV2::getSystemMetricsForDpi(SM_CYEDGE, _dpi); + _xScroll = DPIManagerV2::getSystemMetricsForDpi(SM_CXVSCROLL, _dpi); + _yScroll = DPIManagerV2::getSystemMetricsForDpi(SM_CYVSCROLL, _dpi); } }; diff --git a/PowerEditor/src/dpiManagerV2.cpp b/PowerEditor/src/dpiManagerV2.cpp index 86d25be5a..c92bb65c0 100644 --- a/PowerEditor/src/dpiManagerV2.cpp +++ b/PowerEditor/src/dpiManagerV2.cpp @@ -17,11 +17,54 @@ #include "dpiManagerV2.h" +template +bool ptrFn(HMODULE handle, P& pointer, const char* name) +{ + auto p = reinterpret_cast

(::GetProcAddress(handle, name)); + if (p != nullptr) + { + pointer = p; + return true; + } + return false; +} + +using fnGetDpiForSystem = UINT (WINAPI*)(); +using fnGetDpiForWindow = UINT (WINAPI*)(HWND); +using fnGetSystemMetricsForDpi = int (WINAPI*)(int, UINT); +using fnSystemParametersInfoForDpi = BOOL(WINAPI*)(UINT, UINT, PVOID, UINT, UINT); + +fnGetDpiForSystem _fnGetDpiForSystem = nullptr; +fnGetDpiForWindow _fnGetDpiForWindow = nullptr; +fnGetSystemMetricsForDpi _fnGetSystemMetricsForDpi = nullptr; +fnSystemParametersInfoForDpi _fnSystemParametersInfoForDpi = nullptr; + +void DPIManagerV2::initDpiAPI() +{ + HMODULE hUser32 = ::GetModuleHandleW(L"user32.dll"); + if (hUser32) + { + ptrFn(hUser32, _fnGetDpiForSystem, "GetDpiForSystem"); + ptrFn(hUser32, _fnGetDpiForWindow, "GetDpiForWindow"); + ptrFn(hUser32, _fnGetSystemMetricsForDpi, "GetSystemMetricsForDpi"); + ptrFn(hUser32, _fnSystemParametersInfoForDpi, "SystemParametersInfoForDpi"); + } +} + +int DPIManagerV2::getSystemMetricsForDpi(int nIndex, UINT dpi) +{ + if (_fnGetSystemMetricsForDpi != nullptr) + { + return _fnGetSystemMetricsForDpi(nIndex, dpi); + } + return DPIManagerV2::scale(::GetSystemMetrics(nIndex), dpi); +} + UINT DPIManagerV2::getDpiForSystem() { - if (NppDarkMode::isWindows10()) + if (_fnGetDpiForSystem != nullptr) { - return ::GetDpiForSystem(); + return _fnGetDpiForSystem(); } UINT dpi = USER_DEFAULT_SCREEN_DPI; @@ -36,9 +79,9 @@ UINT DPIManagerV2::getDpiForSystem() UINT DPIManagerV2::getDpiForWindow(HWND hWnd) { - if (NppDarkMode::isWindows10()) + if (_fnGetDpiForWindow != nullptr) { - const auto dpi = ::GetDpiForWindow(hWnd); + const auto dpi = _fnGetDpiForWindow(hWnd); if (dpi > 0) { return dpi; @@ -66,8 +109,8 @@ LOGFONT DPIManagerV2::getDefaultGUIFontForDpi(UINT dpi, FontType type) LOGFONT lf{}; NONCLIENTMETRICS ncm{}; ncm.cbSize = sizeof(NONCLIENTMETRICS); - if (NppDarkMode::isWindows10() - && (::SystemParametersInfoForDpi(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0, dpi) != FALSE)) + if (_fnSystemParametersInfoForDpi != nullptr + && (_fnSystemParametersInfoForDpi(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0, dpi) != FALSE)) { result = 2; } diff --git a/PowerEditor/src/dpiManagerV2.h b/PowerEditor/src/dpiManagerV2.h index c24968272..b1f6e1e81 100644 --- a/PowerEditor/src/dpiManagerV2.h +++ b/PowerEditor/src/dpiManagerV2.h @@ -16,7 +16,7 @@ #pragma once -#include "NppDarkMode.h" +#include class DPIManagerV2 { @@ -28,6 +28,10 @@ public: enum class FontType { menu, status, message, caption, smcaption }; + static void initDpiAPI(); + + static int getSystemMetricsForDpi(int nIndex, UINT dpi); + static UINT getDpiForSystem(); static UINT getDpiForWindow(HWND hWnd); static UINT getDpiForParent(HWND hWnd) { diff --git a/PowerEditor/src/winmain.cpp b/PowerEditor/src/winmain.cpp index 66401d6f2..f1db98ebf 100644 --- a/PowerEditor/src/winmain.cpp +++ b/PowerEditor/src/winmain.cpp @@ -20,6 +20,7 @@ #include "MiniDumper.h" //Write dump files #include "verifySignedfile.h" #include "NppDarkMode.h" +#include "dpiManagerV2.h" #include typedef std::vector ParamVector; @@ -502,6 +503,7 @@ int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE /*hPrevInstance NppGUI & nppGui = nppParameters.getNppGUI(); NppDarkMode::initDarkMode(); + DPIManagerV2::initDpiAPI(); bool doUpdateNpp = nppGui._autoUpdateOpt._doAutoUpdate; bool doUpdatePluginList = nppGui._autoUpdateOpt._doAutoUpdate; diff --git a/PowerEditor/visual.net/notepadPlus.Cpp.props b/PowerEditor/visual.net/notepadPlus.Cpp.props index f3fbd9e8b..526604586 100644 --- a/PowerEditor/visual.net/notepadPlus.Cpp.props +++ b/PowerEditor/visual.net/notepadPlus.Cpp.props @@ -25,7 +25,7 @@ ..\src;..\src\MISC;..\src\MISC\Common;..\src\MISC\Exception;..\src\MISC\PluginsManager;..\src\MISC\Process;..\src\MISC\RegExt;..\src\MISC\md5;..\src\MISC\sha1;..\src\MISC\sha2;..\src\MISC\sha512;..\src\MISC\SysMsg;..\src\ScintillaComponent;..\src\Win32Explr;..\src\WinControls;..\src\WinControls\AboutDlg;..\src\WinControls\AnsiCharPanel;..\src\WinControls\ClipboardHistory;..\src\WinControls\ColourPicker;..\src\WinControls\ContextMenu;..\src\WinControls\DockingWnd;..\src\WinControls\DocumentMap;..\src\WinControls\FileBrowser;..\src\WinControls\FindCharsInRange;..\src\WinControls\FunctionList;..\src\WinControls\Grid;..\src\WinControls\ImageListSet;..\src\WinControls\OpenSaveFileDialog;..\src\WinControls\PluginsAdmin;..\src\WinControls\Preference;..\src\WinControls\ProjectPanel;..\src\WinControls\ReadDirectoryChanges;..\src\WinControls\shortcut;..\src\WinControls\SplitterContainer;..\src\WinControls\StaticDialog;..\src\WinControls\StaticDialog\RunDlg;..\src\WinControls\StatusBar;..\src\WinControls\TabBar;..\src\WinControls\TaskList;..\src\WinControls\ToolBar;..\src\WinControls\ToolTip;..\src\WinControls\TrayIcon;..\src\WinControls\TreeView;..\src\WinControls\VerticalFileSwitcher;..\src\WinControls\WindowsDlg;%(AdditionalIncludeDirectories) - WIN32;_WIN32_WINNT=_WIN32_WINNT_WIN10;NTDDI_VERSION=NTDDI_WIN10_RS5;_WINDOWS;OEMRESOURCE;NOMINMAX;_USE_64BIT_TIME_T;TIXML_USE_STL;TIXMLA_USE_STL;_CRT_NONSTDC_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES;_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT;_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING;%(PreprocessorDefinitions) + WIN32;_WIN32_WINNT=_WIN32_WINNT_WIN7;NTDDI_VERSION=NTDDI_WIN7;_WINDOWS;OEMRESOURCE;NOMINMAX;_USE_64BIT_TIME_T;TIXML_USE_STL;TIXMLA_USE_STL;_CRT_NONSTDC_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES;_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT;_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING;%(PreprocessorDefinitions) Async Level4 true