This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
win10.cpp
124 lines (112 loc) · 4.47 KB
/
win10.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
#include <napi.h>
#include <dwmapi.h>
enum AccentState {
ACCENT_DISABLED = 0,
ACCENT_ENABLE_GRADIENT = 1,
ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
ACCENT_ENABLE_BLURBEHIND = 3,
ACCENT_ENABLE_ACRYLICBLURBEHIND = 4,
ACCENT_ENABLE_HOSTBACKDROP = 5, // RS5 1809
ACCENT_INVALID_STATE = 6
};
enum WindowCompositionAttribute {
WCA_ACCENT_POLICY = 19
};
struct AccentPolicy {
AccentState accentState;
int accentFlags;
int gradientColor;
int animationId;
};
struct WindowCompositionAttributeData {
WindowCompositionAttribute attribute;
PVOID pData;
ULONG dataSize;
};
typedef BOOL(WINAPI
*pSetWindowCompositionAttribute)(HWND, WindowCompositionAttributeData*);
const HINSTANCE hModule = LoadLibrary(TEXT("user32.dll"));
void setVibrancy(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
try {
if (info.Length() == 0) {
Napi::TypeError::New(env, "WINDOW_NOT_GIVEN").ThrowAsJavaScriptException();
return;
}
if (info.Length() != 6) {
Napi::TypeError::New(env, "PARAMETER_ERROR").ThrowAsJavaScriptException();
return;
}
if (!info[0].IsNumber()) {
Napi::TypeError::New(env, "UNKNOWN").ThrowAsJavaScriptException();
return;
}
HWND hWnd = (HWND) info[0].As<Napi::Number>().Int64Value();
int isRS4OrGreater = info[1].As<Napi::Number>().Int32Value();
int redValue = info[2].As<Napi::Number>().Int32Value();
int greenValue = info[3].As<Napi::Number>().Int32Value();
int blueValue = info[4].As<Napi::Number>().Int32Value();
int alphaValue = info[5].As<Napi::Number>().Int32Value();
if (hModule) {
const pSetWindowCompositionAttribute SetWindowCompositionAttribute = (pSetWindowCompositionAttribute) GetProcAddress(
hModule, "SetWindowCompositionAttribute");
if (SetWindowCompositionAttribute) {
int gradientColor = (alphaValue<<24) + (blueValue<<16) + (greenValue<<8) + (redValue);
AccentState blurType = isRS4OrGreater == 1 ? ACCENT_ENABLE_ACRYLICBLURBEHIND : ACCENT_ENABLE_BLURBEHIND;
AccentPolicy policy = {blurType, 2, gradientColor, 0};
WindowCompositionAttributeData data = {WCA_ACCENT_POLICY, &policy, sizeof(AccentPolicy)};
SetWindowCompositionAttribute(hWnd, &data);
} else {
Napi::Error::New(env, "FAIL_LOAD_DLL").ThrowAsJavaScriptException();
return;
}
FreeLibrary(hModule);
} else {
Napi::Error::New(env, "FAIL_LOAD_DLL").ThrowAsJavaScriptException();
return;
}
} catch (const char *ex) {
Napi::Error::New(env, "UNKNOWN").ThrowAsJavaScriptException();
}
}
void disableVibrancy(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
try {
if (info.Length() != 1) {
Napi::TypeError::New(env, "WINDOW_NOT_GIVEN").ThrowAsJavaScriptException();
return;
}
if (!info[0].IsNumber()) {
Napi::TypeError::New(env, "UNKNOWN").ThrowAsJavaScriptException();
return;
}
HWND hWnd = (HWND) info[0].As<Napi::Number>().Int64Value();
if (hModule) {
const pSetWindowCompositionAttribute SetWindowCompositionAttribute = (pSetWindowCompositionAttribute) GetProcAddress(
hModule, "SetWindowCompositionAttribute");
if (SetWindowCompositionAttribute) {
AccentPolicy policy = {ACCENT_DISABLED, 0, 0, 0};
WindowCompositionAttributeData data = {WCA_ACCENT_POLICY, &policy, sizeof(AccentPolicy)};
SetWindowCompositionAttribute(hWnd, &data);
} else {
Napi::Error::New(env, "FAIL_LOAD_DLL").ThrowAsJavaScriptException();
return;
}
FreeLibrary(hModule);
} else {
Napi::Error::New(env, "FAIL_LOAD_DLL").ThrowAsJavaScriptException();
return;
}
} catch (const char *ex) {
Napi::Error::New(env, "UNKNOWN").ThrowAsJavaScriptException();
}
}
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "setVibrancy"),
Napi::Function::New(env, setVibrancy));
exports.Set(Napi::String::New(env, "disableVibrancy"),
Napi::Function::New(env, disableVibrancy));
return exports;
}
NODE_API_MODULE(vibrancy, Init
)