-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThemeManagement.h
119 lines (102 loc) · 3.61 KB
/
ThemeManagement.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#pragma once
// Óïðàâëåíèå òåìàìè äëÿ êîíòðîëîâ/îêíà
// ñì https://docs.microsoft.com/ru-ru/windows/win32/controls/parts-and-states?redirectedfrom=MSDN
#include <afx.h>
#include <libloaderapi.h>
#include <shlwapi.h>
#include <Uxtheme.h>
////////////////////////////////////////////////////////////////////////////////
/* Óñòàíîâèòü òåìó äëÿ îêíà / êîíòðîëà
Ïðèìåðû
EnableWindowTheme(GetSafeHwnd(), L"ListView", L"Explorer", NULL);
EnableWindowTheme(m_hWnd, L"WINDOW", L"Explorer", NULL);
SetWindowTheme(m_hWnd, L"Explorer", NULL);
*/
inline LRESULT EnableWindowTheme(HWND hwnd, LPCWSTR classList, LPCWSTR subApp, LPCWSTR idlist)
{
LRESULT lResult = S_FALSE;
HRESULT(__stdcall *pSetWindowTheme)(HWND hwnd, LPCWSTR pszSubAppName, LPCWSTR pszSubIdList);
HANDLE(__stdcall *pOpenThemeData)(HWND hwnd, LPCWSTR pszClassList);
HRESULT(__stdcall *pCloseThemeData)(HANDLE hTheme);
HMODULE hinstDll = ::LoadLibrary(_T("UxTheme.dll"));
if (hinstDll)
{
(FARPROC&)pOpenThemeData = ::GetProcAddress(hinstDll, "OpenThemeData");
(FARPROC&)pCloseThemeData = ::GetProcAddress(hinstDll, "CloseThemeData");
(FARPROC&)pSetWindowTheme = ::GetProcAddress(hinstDll, "SetWindowTheme");
if (pSetWindowTheme && pOpenThemeData && pCloseThemeData)
{
HANDLE theme = pOpenThemeData(hwnd, classList);
if (theme != NULL)
{
VERIFY(pCloseThemeData(theme) == S_OK);
lResult = pSetWindowTheme(hwnd, subApp, idlist);
}
}
::FreeLibrary(hinstDll);
}
return lResult;
}
//----------------------------------------------------------------------------//
inline bool IsCommonControlsEnabled()
{
bool commoncontrols = false;
// Test if application has access to common controls
HMODULE hinstDll = ::LoadLibrary(_T("comctl32.dll"));
if (hinstDll)
{
DLLGETVERSIONPROC pDllGetVersion = (DLLGETVERSIONPROC)::GetProcAddress(hinstDll, "DllGetVersion");
if (pDllGetVersion != NULL)
{
DLLVERSIONINFO dvi = { 0 };
dvi.cbSize = sizeof(dvi);
HRESULT hRes = pDllGetVersion((DLLVERSIONINFO *)&dvi);
if (SUCCEEDED(hRes))
commoncontrols = dvi.dwMajorVersion >= 6;
}
::FreeLibrary(hinstDll);
}
return commoncontrols;
}
//----------------------------------------------------------------------------//
inline bool IsThemeEnabled()
{
bool XPStyle = false;
bool(__stdcall *pIsAppThemed)();
bool(__stdcall *pIsThemeActive)();
// Test if operating system has themes enabled
HMODULE hinstDll = ::LoadLibrary(_T("UxTheme.dll"));
if (hinstDll)
{
(FARPROC&)pIsAppThemed = ::GetProcAddress(hinstDll, "IsAppThemed");
(FARPROC&)pIsThemeActive = ::GetProcAddress(hinstDll, "IsThemeActive");
if (pIsAppThemed != NULL && pIsThemeActive != NULL)
{
if (pIsAppThemed() && pIsThemeActive())
{
// Test if application has themes enabled by loading the proper DLL
XPStyle = IsCommonControlsEnabled();
}
}
::FreeLibrary(hinstDll);
}
return XPStyle;
}
// see https://docs.microsoft.com/ru-ru/windows/win32/controls/parts-and-states?redirectedfrom=MSDN
struct ThemeHolder
{
explicit ThemeHolder(HWND hWnd, const wchar_t* themeName)
: m_hTheme(::OpenThemeData(hWnd, themeName))
{
VERIFY(!!m_hTheme);
}
~ThemeHolder()
{
VERIFY(SUCCEEDED(::CloseThemeData(m_hTheme)));
}
operator HTHEME() const
{
return m_hTheme;
}
HTHEME m_hTheme;
};