From 6bc94625e0bca9129fb019afdc5ab9d8800a0da1 Mon Sep 17 00:00:00 2001 From: MouriNaruto Date: Tue, 14 May 2024 09:32:02 +0800 Subject: [PATCH] Start to implement Dark Mode Handlers. --- NanaZip.Frieren/NanaZip.Frieren.cpp | 192 ++++++++++++++++++++++++++++ 1 file changed, 192 insertions(+) diff --git a/NanaZip.Frieren/NanaZip.Frieren.cpp b/NanaZip.Frieren/NanaZip.Frieren.cpp index 99fb391c3..bc017be25 100644 --- a/NanaZip.Frieren/NanaZip.Frieren.cpp +++ b/NanaZip.Frieren/NanaZip.Frieren.cpp @@ -10,6 +10,150 @@ #include "NanaZip.Frieren.h" +#include +#include + +#include + +#include + +namespace +{ + namespace FunctionType + { + enum + { + GetSysColor, + GetSysColorBrush, + DrawThemeText, + DrawThemeBackground, + + MaximumFunction + }; + } + + struct FunctionItem + { + PVOID Original; + PVOID Detoured; + }; + + thread_local bool volatile g_ThreadInitialized = false; + thread_local FunctionItem g_FunctionTable[FunctionType::MaximumFunction]; + + static DWORD WINAPI OriginalGetSysColor( + _In_ int nIndex) + { + return reinterpret_cast( + g_FunctionTable[FunctionType::GetSysColor].Original)( + nIndex); + } + + static DWORD WINAPI DetouredGetSysColor( + _In_ int nIndex) + { + return ::OriginalGetSysColor(nIndex); + } + + static HBRUSH WINAPI OriginalGetSysColorBrush( + _In_ int nIndex) + { + return reinterpret_cast( + g_FunctionTable[FunctionType::GetSysColorBrush].Original)( + nIndex); + } + + static HBRUSH WINAPI DetouredGetSysColorBrush( + _In_ int nIndex) + { + return ::OriginalGetSysColorBrush(nIndex); + } + + static HRESULT WINAPI OriginalDrawThemeText( + _In_ HTHEME hTheme, + _In_ HDC hdc, + _In_ int iPartId, + _In_ int iStateId, + _In_ LPCWSTR pszText, + _In_ int cchText, + _In_ DWORD dwTextFlags, + _In_ DWORD dwTextFlags2, + _In_ LPCRECT pRect) + { + return reinterpret_cast( + g_FunctionTable[FunctionType::DrawThemeText].Original)( + hTheme, + hdc, + iPartId, + iStateId, + pszText, + cchText, + dwTextFlags, + dwTextFlags2, + pRect); + } + + static HRESULT WINAPI DetouredDrawThemeText( + _In_ HTHEME hTheme, + _In_ HDC hdc, + _In_ int iPartId, + _In_ int iStateId, + _In_ LPCWSTR pszText, + _In_ int cchText, + _In_ DWORD dwTextFlags, + _In_ DWORD dwTextFlags2, + _In_ LPCRECT pRect) + { + return OriginalDrawThemeText( + return ::OriginalDrawThemeText( + hTheme, + hdc, + iPartId, + iStateId, + pszText, + cchText, + dwTextFlags, + dwTextFlags2, + pRect); + } + + static HRESULT WINAPI OriginalDrawThemeBackground( + _In_ HTHEME hTheme, + _In_ HDC hdc, + _In_ int iPartId, + _In_ int iStateId, + _In_ LPCRECT pRect, + _In_opt_ LPCRECT pClipRect) + { + return reinterpret_cast( + g_FunctionTable[FunctionType::DrawThemeBackground].Original)( + hTheme, + hdc, + iPartId, + iStateId, + pRect, + pClipRect); + } + + static HRESULT WINAPI DetouredDrawThemeBackground( + _In_ HTHEME hTheme, + _In_ HDC hdc, + _In_ int iPartId, + _In_ int iStateId, + _In_ LPCRECT pRect, + _In_opt_ LPCRECT pClipRect) + { + return OriginalDrawThemeBackground( + return ::OriginalDrawThemeBackground( + hTheme, + hdc, + iPartId, + iStateId, + pRect, + pClipRect); + } +} + EXTERN_C HRESULT WINAPI NanaZipFrierenThreadInitialize() { if (g_ThreadInitialized) @@ -17,6 +161,39 @@ EXTERN_C HRESULT WINAPI NanaZipFrierenThreadInitialize() return S_OK; } + g_FunctionTable[FunctionType::GetSysColor].Original = + ::GetSysColor; + g_FunctionTable[FunctionType::GetSysColor].Detoured = + ::DetouredGetSysColor; + + g_FunctionTable[FunctionType::GetSysColorBrush].Original = + ::GetSysColorBrush; + g_FunctionTable[FunctionType::GetSysColorBrush].Detoured = + ::DetouredGetSysColorBrush; + + g_FunctionTable[FunctionType::DrawThemeText].Original = + ::DrawThemeText; + g_FunctionTable[FunctionType::DrawThemeText].Detoured = + ::DetouredDrawThemeText; + + g_FunctionTable[FunctionType::DrawThemeBackground].Original = + ::DrawThemeBackground; + g_FunctionTable[FunctionType::DrawThemeBackground].Detoured = + ::DetouredDrawThemeBackground; + + ::DetourTransactionBegin(); + ::DetourUpdateThread(::GetCurrentThread()); + for (size_t i = 0; i < FunctionType::MaximumFunction; ++i) + { + if (g_FunctionTable[i].Original) + { + ::DetourAttach( + &g_FunctionTable[i].Original, + g_FunctionTable[i].Detoured); + } + } + ::DetourTransactionCommit(); + g_ThreadInitialized = true; return S_OK; @@ -31,5 +208,20 @@ EXTERN_C HRESULT WINAPI NanaZipFrierenThreadUninitialize() g_ThreadInitialized = false; + ::DetourTransactionBegin(); + ::DetourUpdateThread(::GetCurrentThread()); + for (size_t i = 0; i < FunctionType::MaximumFunction; ++i) + { + if (g_FunctionTable[i].Original) + { + ::DetourDetach( + &g_FunctionTable[i].Original, + g_FunctionTable[i].Detoured); + g_FunctionTable[i].Original = nullptr; + g_FunctionTable[i].Detoured = nullptr; + } + } + ::DetourTransactionCommit(); + return S_OK; }