Workaround Microsoft dark mode API bug of Windows 11

Notepad++ dark mode works fine under Microsft Windows 7, 8, 8.1 & 10.
Under Windows 11 while Notepad++ being switched into dark mode,
the hightlighting (background) of toolbar's hot-track icons remains in light mode.
Hence this workaround to remedy Notepad++ dark mode visual glitchy due to the bug of Windows 11.

Fix #10510, close #10685
This commit is contained in:
Don Ho 2021-10-17 14:03:59 +02:00
parent d80b0b254e
commit 5d086f93a8
6 changed files with 32 additions and 31 deletions

View File

@ -258,6 +258,11 @@ constexpr bool CheckBuildNumber(DWORD buildNumber)
buildNumber >= 22000); // Windows 11 insider builds buildNumber >= 22000); // Windows 11 insider builds
} }
bool IsWindows11() // or later OS version
{
return (g_buildNumber >= 22000);
}
void InitDarkMode() void InitDarkMode()
{ {
fnRtlGetNtVersionNumbers RtlGetNtVersionNumbers = nullptr; fnRtlGetNtVersionNumbers RtlGetNtVersionNumbers = nullptr;

View File

@ -15,3 +15,4 @@ void AllowDarkModeForApp(bool allow);
void EnableDarkScrollBarForWindowAndChildren(HWND hwnd); void EnableDarkScrollBarForWindowAndChildren(HWND hwnd);
void InitDarkMode(); void InitDarkMode();
void SetDarkMode(bool useDarkMode, bool fixDarkScrollbar); void SetDarkMode(bool useDarkMode, bool fixDarkScrollbar);
bool IsWindows11();

View File

@ -364,6 +364,11 @@ namespace NppDarkMode
return g_darkModeSupported; return g_darkModeSupported;
} }
bool isWindows11()
{
return IsWindows11();
}
COLORREF invertLightness(COLORREF c) COLORREF invertLightness(COLORREF c)
{ {
WORD h = 0; WORD h = 0;
@ -1146,34 +1151,6 @@ namespace NppDarkMode
SetWindowSubclass(hwnd, GroupboxSubclass, g_groupboxSubclassID, pButtonData); SetWindowSubclass(hwnd, GroupboxSubclass, g_groupboxSubclassID, pButtonData);
} }
constexpr UINT_PTR g_toolbarSubclassID = 42;
LRESULT CALLBACK ToolbarSubclass(
HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam,
UINT_PTR uIdSubclass,
DWORD_PTR dwRefData
)
{
UNREFERENCED_PARAMETER(uIdSubclass);
UNREFERENCED_PARAMETER(dwRefData);
switch (uMsg)
{
case WM_NCDESTROY:
RemoveWindowSubclass(hWnd, ToolbarSubclass, g_toolbarSubclassID);
break;
}
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
void subclassToolbarControl(HWND hwnd)
{
SetWindowSubclass(hwnd, ToolbarSubclass, g_toolbarSubclassID, 0);
}
constexpr UINT_PTR g_tabSubclassID = 42; constexpr UINT_PTR g_tabSubclassID = 42;
LRESULT CALLBACK TabSubclass( LRESULT CALLBACK TabSubclass(

View File

@ -83,6 +83,8 @@ namespace NppDarkMode
bool isDarkMenuEnabled(); bool isDarkMenuEnabled();
bool isExperimentalSupported(); bool isExperimentalSupported();
bool isWindows11();
COLORREF invertLightness(COLORREF c); COLORREF invertLightness(COLORREF c);
COLORREF invertLightnessSofter(COLORREF c); COLORREF invertLightnessSofter(COLORREF c);
double calculatePerceivedLighness(COLORREF c); double calculatePerceivedLighness(COLORREF c);
@ -145,7 +147,6 @@ namespace NppDarkMode
void subclassButtonControl(HWND hwnd); void subclassButtonControl(HWND hwnd);
void subclassGroupboxControl(HWND hwnd); void subclassGroupboxControl(HWND hwnd);
void subclassToolbarControl(HWND hwnd);
void subclassTabControl(HWND hwnd); void subclassTabControl(HWND hwnd);
void subclassComboBoxControl(HWND hwnd); void subclassComboBoxControl(HWND hwnd);

View File

@ -306,6 +306,11 @@ void ToolBar::reset(bool create)
{ {
setDefaultImageListDM(); setDefaultImageListDM();
setDisableImageListDM(); setDisableImageListDM();
if (NppDarkMode::isWindows11())
{
setHoveredImageListDM();
}
} }
else else
{ {
@ -319,6 +324,11 @@ void ToolBar::reset(bool create)
{ {
setDefaultImageListDM2(); setDefaultImageListDM2();
setDisableImageListDM2(); setDisableImageListDM2();
if (NppDarkMode::isWindows11())
{
setHoveredImageListDM2();
}
} }
else else
{ {
@ -534,8 +544,7 @@ void ReBar::init(HINSTANCE hInst, HWND hPere)
_hSelf = CreateWindowEx(WS_EX_TOOLWINDOW, _hSelf = CreateWindowEx(WS_EX_TOOLWINDOW,
REBARCLASSNAME, REBARCLASSNAME,
NULL, NULL,
WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT| WS_CHILD|WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | RBS_VARHEIGHT | CCS_NODIVIDER | CCS_NOPARENTALIGN,
CCS_NODIVIDER | CCS_NOPARENTALIGN,
0,0,0,0, _hParent, NULL, _hInst, NULL); 0,0,0,0, _hParent, NULL, _hInst, NULL);
SetWindowSubclass(_hSelf, RebarSubclass, g_rebarSubclassID, 0); SetWindowSubclass(_hSelf, RebarSubclass, g_rebarSubclassID, 0);

View File

@ -147,6 +147,14 @@ private :
::SendMessage(_hSelf, TB_SETDISABLEDIMAGELIST, 0, reinterpret_cast<LPARAM>(_toolBarIcons.getDisableLstSetDM2())); ::SendMessage(_hSelf, TB_SETDISABLEDIMAGELIST, 0, reinterpret_cast<LPARAM>(_toolBarIcons.getDisableLstSetDM2()));
}; };
void setHoveredImageListDM() {
::SendMessage(_hSelf, TB_SETHOTIMAGELIST, 0, reinterpret_cast<LPARAM>(_toolBarIcons.getDefaultLst()));
};
void setHoveredImageListDM2() {
::SendMessage(_hSelf, TB_SETHOTIMAGELIST, 0, reinterpret_cast<LPARAM>(_toolBarIcons.getDefaultLstSet2()));
};
void reset(bool create = false); void reset(bool create = false);
void setState(toolBarStatusType state) { void setState(toolBarStatusType state) {
_state = state; _state = state;