-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreen_off.cpp
More file actions
252 lines (228 loc) · 7.6 KB
/
Copy pathscreen_off.cpp
File metadata and controls
252 lines (228 loc) · 7.6 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// 息屏静音工具 by.LU
// GitHub: FireStar0507/screen-off
#include <windows.h>
#include <shellapi.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
// ---------- 全局变量 ----------
HWND g_hWndBlack = NULL;
bool g_bIsOff = false;
float g_originalVolume = 1.0f;
UINT g_hotkeyId = 1;
HANDLE g_hMutex = NULL;
IAudioEndpointVolume* g_pVolume = NULL;
// ---------- 音量控制 (COM) ----------
bool InitVolumeControl() {
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr)) return false;
IMMDeviceEnumerator* pEnumerator = NULL;
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL,
__uuidof(IMMDeviceEnumerator), (void**)&pEnumerator);
if (FAILED(hr)) {
CoUninitialize();
return false;
}
IMMDevice* pDevice = NULL;
hr = pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDevice);
pEnumerator->Release();
if (FAILED(hr)) {
CoUninitialize();
return false;
}
hr = pDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_ALL, NULL,
(void**)&g_pVolume);
pDevice->Release();
if (FAILED(hr)) {
CoUninitialize();
return false;
}
return true;
}
void SetSystemVolume(float volume) {
if (g_pVolume) g_pVolume->SetMasterVolumeLevelScalar(volume, NULL);
}
float GetSystemVolume() {
if (g_pVolume) {
float vol;
g_pVolume->GetMasterVolumeLevelScalar(&vol);
return vol;
}
return 1.0f;
}
// ---------- 单实例检测 ----------
bool CheckSingleInstance() {
g_hMutex = CreateMutexW(NULL, TRUE, L"Global\\ScreenOffTool_Mutex");
if (GetLastError() == ERROR_ALREADY_EXISTS) {
// 已有实例,激活其隐藏窗口(可有可无,此处仅作演示)
HWND hWnd = FindWindowW(L"ScreenOffMainClass", NULL);
if (hWnd) {
if (IsIconic(hWnd)) ShowWindow(hWnd, SW_RESTORE);
SetForegroundWindow(hWnd);
}
CloseHandle(g_hMutex);
return false;
}
return true;
}
// ---------- 黑色窗口过程 ----------
LRESULT CALLBACK BlackWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_KEYDOWN:
if (wParam == VK_ESCAPE) {
HWND hMain = FindWindowW(L"ScreenOffMainClass", NULL);
if (hMain) PostMessage(hMain, WM_HOTKEY, g_hotkeyId, 0);
return 0;
}
break;
case WM_DESTROY:
// 不调用 PostQuitMessage,只销毁自身
break;
}
return DefWindowProcW(hwnd, msg, wParam, lParam);
}
// ---------- 创建/销毁黑色窗口 ----------
void CreateBlackWindow() {
WNDCLASSEXW wc = {};
wc.cbSize = sizeof(WNDCLASSEXW);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = BlackWndProc;
wc.hInstance = GetModuleHandleW(NULL);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = CreateSolidBrush(RGB(0, 0, 0));
wc.lpszClassName = L"BlackScreenClass";
RegisterClassExW(&wc);
g_hWndBlack = CreateWindowExW(
WS_EX_TOPMOST | WS_EX_NOACTIVATE,
L"BlackScreenClass",
NULL,
WS_POPUP | WS_VISIBLE,
0, 0,
GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN),
NULL, NULL, GetModuleHandleW(NULL), NULL
);
}
void DestroyBlackWindow() {
if (g_hWndBlack) {
DestroyWindow(g_hWndBlack);
g_hWndBlack = NULL;
}
}
void ToggleBlackScreen(bool show) {
if (show && !g_hWndBlack) CreateBlackWindow();
else if (!show && g_hWndBlack) DestroyBlackWindow();
}
// ---------- 主窗口过程 ----------
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE: {
if (!RegisterHotKey(hwnd, g_hotkeyId, MOD_CONTROL | MOD_ALT, 'O'))
MessageBoxW(hwnd, L"注册热键失败!", L"错误", MB_ICONERROR);
NOTIFYICONDATAW nid = {};
nid.cbSize = sizeof(NOTIFYICONDATAW);
nid.hWnd = hwnd;
nid.uID = 100;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nid.uCallbackMessage = WM_APP + 1;
nid.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcscpy_s(nid.szTip, L"息屏静音工具 by.LU");
Shell_NotifyIconW(NIM_ADD, &nid);
break;
}
case WM_DESTROY: {
NOTIFYICONDATAW nid = {};
nid.cbSize = sizeof(NOTIFYICONDATAW);
nid.hWnd = hwnd;
nid.uID = 100;
Shell_NotifyIconW(NIM_DELETE, &nid);
if (g_bIsOff) {
ToggleBlackScreen(false);
SetSystemVolume(g_originalVolume);
g_bIsOff = false;
}
if (g_pVolume) { g_pVolume->Release(); g_pVolume = NULL; }
CoUninitialize();
if (g_hMutex) { CloseHandle(g_hMutex); g_hMutex = NULL; }
PostQuitMessage(0);
break;
}
case WM_HOTKEY: {
if (wParam == g_hotkeyId) {
if (!g_bIsOff) {
g_originalVolume = GetSystemVolume();
SetSystemVolume(0.0f);
ToggleBlackScreen(true);
g_bIsOff = true;
} else {
ToggleBlackScreen(false);
SetSystemVolume(g_originalVolume);
g_bIsOff = false;
}
}
break;
}
case WM_APP + 1: {
if (lParam == WM_LBUTTONDBLCLK) {
SendMessage(hwnd, WM_HOTKEY, g_hotkeyId, 0);
} else if (lParam == WM_RBUTTONUP) {
HMENU hMenu = CreatePopupMenu();
AppendMenuW(hMenu, MF_STRING, 1, L"切换息屏");
AppendMenuW(hMenu, MF_STRING, 2, L"退出");
POINT pt;
GetCursorPos(&pt);
SetForegroundWindow(hwnd);
int cmd = TrackPopupMenu(hMenu, TPM_RETURNCMD | TPM_NONOTIFY,
pt.x, pt.y, 0, hwnd, NULL);
DestroyMenu(hMenu);
if (cmd == 1) SendMessage(hwnd, WM_HOTKEY, g_hotkeyId, 0);
else if (cmd == 2) DestroyWindow(hwnd);
}
break;
}
default:
return DefWindowProcW(hwnd, msg, wParam, lParam);
}
return 0;
}
// ---------- WinMain ----------
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow) {
// 防多开
if (!CheckSingleInstance()) return 0;
// 初始化音量控制
if (!InitVolumeControl()) {
MessageBoxW(NULL, L"音量控制初始化失败!", L"错误", MB_ICONERROR);
return 1;
}
// 注册主窗口类
WNDCLASSEXW wc = {};
wc.cbSize = sizeof(WNDCLASSEXW);
wc.lpfnWndProc = MainWndProc;
wc.hInstance = hInstance;
wc.lpszClassName = L"ScreenOffMainClass";
if (!RegisterClassExW(&wc)) {
MessageBoxW(NULL, L"注册主窗口类失败", L"错误", MB_ICONERROR);
return 1;
}
// 创建隐藏窗口(用于接收消息)
HWND hWnd = CreateWindowExW(
0,
L"ScreenOffMainClass",
L"ScreenOff",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 200, 200,
NULL, NULL, hInstance, NULL
);
if (!hWnd) {
MessageBoxW(NULL, L"创建主窗口失败", L"错误", MB_ICONERROR);
return 1;
}
ShowWindow(hWnd, SW_HIDE);
// 消息循环
MSG msg;
while (GetMessageW(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return 0;
}