-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathAeroColors.cpp
127 lines (112 loc) · 4.83 KB
/
AeroColors.cpp
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
120
121
122
123
124
125
126
127
// sktoolslib - common files for SK tools
// Copyright (C) 2012, 2020 - Stefan Kueng
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include "stdafx.h"
#include "AeroColors.h"
#pragma warning(push)
#pragma warning(disable : 4458) // declaration of 'xx' hides class member
#include <GdiPlus.h>
#pragma warning(pop)
#include <dwmapi.h>
#include <memory>
#pragma comment(lib, "dwmapi.lib")
typedef struct tagCOLORIZATIONPARAMS
{
COLORREF clr1;
COLORREF clr2;
UINT nIntensity;
UINT nReserved2;
UINT nReserved3;
UINT nReserved4;
BOOL fOpaque;
} COLORIZATIONPARAMS;
typedef void(WINAPI* FN_DwmGetColorizationParameters)(COLORIZATIONPARAMS* parameters);
typedef void(WINAPI* FN_DwmSetColorizationParameters)(COLORIZATIONPARAMS* parameters, BOOL unknown);
CAeroColors::CAeroColors(void)
{
oldWallPaperDate.dwLowDateTime = 0;
oldWallPaperDate.dwHighDateTime = 0;
}
CAeroColors::~CAeroColors(void)
{
}
std::wstring CAeroColors::AdjustColorsFromWallpaper()
{
WCHAR wallPaperPath[MAX_PATH] = {0};
SystemParametersInfo(SPI_GETDESKWALLPAPER, _countof(wallPaperPath), wallPaperPath, 0);
if (wallPaperPath[0] == 0)
{
// no wallpaper - no nothing
return L"";
}
if (oldWallpaperPath.compare(wallPaperPath) == 0)
{
HANDLE hFile = CreateFile(wallPaperPath, GENERIC_READ, FILE_SHARE_DELETE | FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
FILETIME create, access, write;
GetFileTime(hFile, &create, &access, &write);
CloseHandle(hFile);
if (CompareFileTime(&oldWallPaperDate, &write) == 0)
return oldWallpaperPath;
oldWallPaperDate = write;
}
else
return oldWallpaperPath;
}
oldWallpaperPath = wallPaperPath;
BOOL bDwmEnabled = FALSE;
if (SUCCEEDED(DwmIsCompositionEnabled(&bDwmEnabled)) && bDwmEnabled)
{
auto bmp = std::make_unique<Gdiplus::Bitmap>(wallPaperPath);
if (bmp == nullptr)
return oldWallpaperPath;
auto bitmap = std::make_unique<Gdiplus::Bitmap>(1, 1, PixelFormat32bppRGB);
Gdiplus::Graphics* graphics = Gdiplus::Graphics::FromImage(bitmap.get());
graphics->SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic);
graphics->DrawImage(bmp.get(), Gdiplus::RectF(0, 0, 1, 1));
Gdiplus::Color clr;
bitmap->GetPixel(0, 0, &clr);
FN_DwmGetColorizationParameters pDwmGetColorizationParameters = (FN_DwmGetColorizationParameters)GetProcAddress(GetModuleHandle(L"dwmapi.dll"), LPCSTR(127));
FN_DwmSetColorizationParameters pDwmSetColorizationParameters = (FN_DwmSetColorizationParameters)GetProcAddress(GetModuleHandle(L"dwmapi.dll"), LPCSTR(131));
if (pDwmGetColorizationParameters && pDwmSetColorizationParameters)
{
COLORIZATIONPARAMS params = {0};
pDwmGetColorizationParameters(¶ms);
params.clr1 = clr.ToCOLORREF();
params.clr2 = clr.ToCOLORREF();
pDwmSetColorizationParameters(¶ms, 0);
}
delete graphics;
}
return oldWallpaperPath;
}
void CAeroColors::SetRandomColor()
{
BOOL bDwmEnabled = FALSE;
if (SUCCEEDED(DwmIsCompositionEnabled(&bDwmEnabled)) && bDwmEnabled)
{
FN_DwmGetColorizationParameters pDwmGetColorizationParameters = (FN_DwmGetColorizationParameters)GetProcAddress(GetModuleHandle(L"dwmapi.dll"), LPCSTR(127));
FN_DwmSetColorizationParameters pDwmSetColorizationParameters = (FN_DwmSetColorizationParameters)GetProcAddress(GetModuleHandle(L"dwmapi.dll"), LPCSTR(131));
if (pDwmGetColorizationParameters && pDwmSetColorizationParameters)
{
COLORREF clr = RGB(rand() & 0xFF, rand() & 0xFF, rand() & 0xFF);
COLORIZATIONPARAMS params = {0};
pDwmGetColorizationParameters(¶ms);
params.clr1 = clr;
params.clr2 = clr;
pDwmSetColorizationParameters(¶ms, 0);
}
}
}