-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWHAT.cpp
448 lines (396 loc) · 11.9 KB
/
WHAT.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//#define FREEZE_PROCESS
#include <Windows.h>
#include <graphics.h>
#include <tlhelp32.h>
#include <iostream>
#include <ctime>
#include <vector>
#include <string>
#define CJZAPI __stdcall
#define MEME_DURATION 9000
using namespace std;
int scr_w = 0, scr_h = 0;
float g_scale = 1.0f;
#define NOSIGNAL_FREQ 1000
#define K(sth) ((GetAsyncKeyState(sth) & 0x8000))
// && GetForegroundWindow() == getHWnd()
inline int GetScreenHeight(void) //获取屏幕高度
{
return GetSystemMetrics(SM_CYSCREEN);
}
inline int GetScreenWidth(void) //获取屏幕宽度
{
return GetSystemMetrics(SM_CXSCREEN);
}
float GetScreenScaleFactor()
{
// 获取窗口当前显示的监视器
// 使用桌面的句柄.
HWND hWnd = GetForegroundWindow();
HMONITOR hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
// 获取监视器逻辑宽度与高度
MONITORINFOEX miex;
miex.cbSize = sizeof(miex);
GetMonitorInfo(hMonitor, &miex);
int cxLogical = (miex.rcMonitor.right - miex.rcMonitor.left);
//int cyLogical = (miex.rcMonitor.bottom - miex.rcMonitor.top);
// 获取监视器物理宽度与高度
DEVMODE dm;
dm.dmSize = sizeof(dm);
dm.dmDriverExtra = 0;
EnumDisplaySettings(miex.szDevice, ENUM_CURRENT_SETTINGS, &dm);
int cxPhysical = dm.dmPelsWidth;
//int cyPhysical = dm.dmPelsHeight;
// 缩放比例计算 实际上使用任何一个即可
double horzScale = ((double)cxPhysical / (double)cxLogical);
//double vertScale = ((double)cyPhysical / (double)cyLogical);
return horzScale;
}
void FocusWindow(HWND _hwnd)
{
DWORD dwCurrentThread = GetCurrentThreadId();
DWORD dwFGThread = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
AttachThreadInput(dwCurrentThread, dwFGThread, TRUE);
// Possible actions you may wan to bring the window into focus.
SetForegroundWindow(_hwnd);
SetCapture(_hwnd);
SetFocus(_hwnd);
SetActiveWindow(_hwnd);
EnableWindow(_hwnd, TRUE);
AttachThreadInput(dwCurrentThread, dwFGThread, FALSE);
} //from https://www.coder.work/article/555749
HANDLE CJZAPI GetProcessHandle(DWORD pid) //通过进程ID获取进程句柄
{
return OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
}HANDLE CJZAPI GetProcessHandle(LPCSTR lpName) //通过进程名获取进程句柄
{ //******警告!区分大小写!!!!******//
//*****警告!必须加扩展名!!!!*****//
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hSnapshot)
{
return NULL;
}
PROCESSENTRY32 pe = { sizeof(pe) };
BOOL fOk;
for (fOk = Process32First(hSnapshot, &pe); fOk; fOk = Process32Next(hSnapshot, &pe))
{
//cout << "\n进程:" << pe.szExeFile;
if (!_stricmp(pe.szExeFile, lpName))
{
CloseHandle(hSnapshot);
return GetProcessHandle(pe.th32ProcessID);
}
}
CloseHandle(hSnapshot);
return NULL;
}
//from https://blog.csdn.net/fzuim/article/details/60954959
bool FreezeProcess(HANDLE hProc) //冻结进程
{
int pid = GetProcessId(hProc);
THREADENTRY32 th32;
th32.dwSize = sizeof(th32);
HANDLE hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (hThreadSnap == INVALID_HANDLE_VALUE)
{
return false;
}
bool b;
b = Thread32First(hThreadSnap, &th32);
while (b)
{
if (th32.th32OwnerProcessID == pid)
{
HANDLE oth = OpenThread(THREAD_ALL_ACCESS, FALSE, th32.th32ThreadID);
if (!(SuspendThread(oth)))
{
// SetColor(11, 0);
// printf("\n已冻结线程ID: %d", th32.th32ThreadID);
// printf("\n已冻结进程ID: %d", th32.th32OwnerProcessID);
}
CloseHandle(oth);
}
b = Thread32Next(hThreadSnap, &th32);
}
CloseHandle(hThreadSnap);
return true;
}
bool UnfreezeProcess(HANDLE hProc) //解冻进程
{
int pid = GetProcessId(hProc);
THREADENTRY32 th32;
th32.dwSize = sizeof(th32);
HANDLE hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (hThreadSnap == INVALID_HANDLE_VALUE)
{
return false;
}
bool b;
b = Thread32First(hThreadSnap, &th32);
while (b)
{
if (th32.th32OwnerProcessID == pid)
{
HANDLE oth = OpenThread(THREAD_ALL_ACCESS, FALSE, th32.th32ThreadID);
if (!(ResumeThread(oth)))
{
// printf("\n已解冻线程ID: %d", th32.th32ThreadID);
// printf("\n已解冻进程ID: %d", th32.th32OwnerProcessID);
}
CloseHandle(oth);
}
b = Thread32Next(hThreadSnap, &th32);
}
CloseHandle(hThreadSnap);
return true;
}
bool FreezeWindow(HWND hWnd)
{
return //EnableWindow(hWnd, FALSE);// &&
InvalidateRect(hWnd, nullptr, FALSE);
}
bool UnfreezeWindow(HWND hWnd)
{
return //EnableWindow(hWnd, TRUE);// &&
ValidateRect(hWnd, nullptr);
}
HWND hwnd;
inline color_t EgeColor(COLORREF clr) {
//RGB -> EGERGB
return EGERGB(GetRValue(clr),GetGValue(clr),GetBValue(clr));
}
template <typename _T>
inline _T Clamp(_T val, _T min=0, _T max=2147483647) { //限定范围
if(val < min) val = min;
else if(val > max) val = max;
return val;
}
int CJZAPI RandomRange(int Min=0,int Max=RAND_MAX,bool rchMin=true,bool rchMax=true) {
//随机数???区???
int a;
//a = random(Max);
a = rand() * rand() % Max;
if(rchMin && rchMax) //[a,b]
return (a%(Max - Min + 1)) + Min;
else if(rchMin && !rchMax) //[a,b)
return (a%(Max - Min)) + Min;
else if(!rchMin && rchMax) //(a,b]
return (a%(Max - Min)) + Min + 1;
else //(a,b)
return (a%(Max - Min - 1)) + Min + 1;
}
template<typename _T>
inline _T CJZAPI Choice(initializer_list<_T> choices) {
vector<_T> vec(choices);
return vec[RandomRange(0, vec.size())];
}
template<typename _T>
inline _T CJZAPI Choice(const vector<_T>& choices_vector) {
return choices_vector[RandomRange(0, choices_vector.size())];
}
template<typename _T>
inline _T& CJZAPI ChoiceRef(vector<_T>& choices_vector) {
return choices_vector[RandomRange(0, choices_vector.size())];
}
void Start()
{
cout << "Start\n";
POINT pt;
GetCursorPos(&pt);
#ifdef FREEZE_PROCESS
HWND hwnd_there = WindowFromPoint(pt);
DWORD self_pid = GetCurrentProcessId();
DWORD pid = 0L;
if (hwnd_there != nullptr && hwnd_there != INVALID_HANDLE_VALUE)
{
if (hwnd_there == hwnd)
pid = self_pid;
GetWindowThreadProcessId(hwnd_there, &pid);
if (pid && pid != self_pid)
{
cout << "Process ID=" << pid << '\n';
HANDLE hProc = GetProcessHandle(pid);
if (!FreezeProcess(hProc))
{
cout << "Process Freezing Failed\n";
CloseHandle(hProc);
return;
}
cout << "Process Freezing Success\n";
CloseHandle(hProc);
}
}
#endif
cout << "Floating Self Window...\n";
SetLayeredWindowAttributes(hwnd, RGB(4,4,4), 0, LWA_COLORKEY);
FocusWindow(getHWnd());
SetCursorPos(scr_w, scr_h*0.5);
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_DRAWFRAME);
static const vector<string> texts
{
"WHAT",
"WHAT",
"WHAT",
"WHAT",
"WHAT",
"WHAT",
"What",
"WTF",
"???",
"HOW",
"Bruh",
"LOL",
"LMAO",
};
int box_w = scr_w * 0.72;
int box_left = scr_w / 2 - box_w / 2;
int box_right = scr_w / 2 + box_w / 2;
int box_top = scr_h * 0.15;
int box_bottom = scr_h * 0.6;
int box_h = (box_bottom - box_top);
constexpr int what_fs = 120;
static const char* what_fontname = "Tahoma";
int text_top = scr_h * 0.67;
cout << "Taking a screenshot...\n";
// 创建内存位图
HDC hdc = GetDC(0);
HDC hMemDC = CreateCompatibleDC(hdc);
HBITMAP hBitmap = CreateCompatibleBitmap(hdc, scr_w* g_scale, scr_h* g_scale);
SelectObject(hMemDC, hBitmap);
// 将窗口内容复制到内存位图
BitBlt(hMemDC, 0, 0, scr_w* g_scale, scr_h* g_scale, hdc, 0, 0, SRCCOPY);
// 获取像素数据
BYTE* pixels = new BYTE[int(scr_w * scr_h * g_scale * g_scale * 4)]; // RGBA格式,每个像素占4字节
BITMAPINFO bmi = { 0 };
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = scr_w * g_scale;
bmi.bmiHeader.biHeight = -scr_h * g_scale; // 负表示顶部到底部扫描
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
GetDIBits(hMemDC, hBitmap, 0, scr_h * g_scale, pixels, &bmi, DIB_RGB_COLORS);
int src_x = Clamp<long>(pt.x - box_w / 2, 0, scr_w - box_w - 1);
int src_y = Clamp<long>(pt.y - box_h / 2, 0, scr_h - box_h - 1);
//
// src_x *= g_scale;
// src_y *= g_scale;
//
cout << "Copying Pixels...\n";
PIMAGE pimg = newimage(scr_w* g_scale, scr_h* g_scale);
color_t* pbuffer = getbuffer(pimg);
for (int y = 0; y < scr_h* g_scale; ++y)
for (int x = 0; x < scr_w* g_scale; ++x)
pbuffer[int(y * scr_w* g_scale + x)] =
EGERGB(
pixels[4 * int(y * scr_w* g_scale + x) + 2],
pixels[4 * int(y * scr_w* g_scale + x) + 1],
pixels[4 * int(y * scr_w* g_scale + x)]);
// if (hwnd_there && INVALID_HANDLE_VALUE != hwnd_there)
// {
// if (hwnd_there != hwnd)
// {
// cout << "Freezing Window...\n";
// if (!FreezeWindow(hwnd_there))
// cout << "Window Freezing Failed\n";
// else
// cout << "Window Freezing Success\n";
// }
// }
PlaySound("WHAT.wav", NULL, SND_FILENAME | SND_ASYNC | SND_NODEFAULT);
clock_t start = clock();
string text = Choice(texts);
bool clicked{false};
for(;clock() - start < MEME_DURATION;)
{
if(clock() - start > 100 && !clicked)
{
clicked = true;
SetCursorPos(scr_w, scr_h*0.5);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
Sleep(50);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
//PIMAGE imgScr = newimage(scr_w, scr_h);
PIMAGE imgScr = nullptr;
cleardevice();
setcolor(BLACK, imgScr);
setfillcolor(BLACK, imgScr);
fillrect(0, 0, box_left, scr_h, imgScr);
fillrect(box_left, 0, box_right, box_top, imgScr);
fillrect(box_right, 0, scr_w, scr_h, imgScr);
fillrect(box_left, box_bottom, box_right, scr_h, imgScr);
// putimage(imgScr, box_left, box_top, pimg);
putimage(imgScr, box_left, box_top, box_w, box_h, pimg, src_x, src_y, box_w* g_scale, box_h* g_scale);
// setcolor(RED, imgScr);
// rectangle(box_left, box_top, box_right, box_bottom, imgScr);
setfont(what_fs, 0, what_fontname, imgScr);
setcolor(EGERGB(255, 255, 255), imgScr);
int text_w = textwidth(text.c_str(), imgScr);
outtextxy(scr_w / 2 - text_w / 2, text_top, text.c_str(), imgScr);
//putimage(nullptr, 0, 0, imgScr);
//delimage(imgScr);
delay_fps(60);
if(K(VK_ESCAPE))
break;
}
delete[] pixels;
DeleteObject(hBitmap);
DeleteDC(hMemDC);
delimage(pimg);
#ifdef FREEZE_PROCESS
if (pid && pid != self_pid)
{
HANDLE hProc = GetProcessHandle(pid);
if (!UnfreezeProcess(hProc))
{
cout << "Process Unfreezing Failed\n";
}else
cout << "Process Unfreezing Success\n";
CloseHandle(hProc);
}
// if (hwnd_there)
// {
// cout << "Unfreezing Window...\n";
// if (!UnfreezeWindow(hwnd_there))
// cout << "Window Unfreezing Failed\n";
// else
// cout << "Window Unfreezing Success\n";
// }
#endif
SetCursorPos(pt.x, pt.y);
SetLayeredWindowAttributes(hwnd, RGB(4,4,4), 0, LWA_COLORKEY | LWA_ALPHA);
cleardevice();
delay_fps(60);
cout << "End\n";
}
int main()
{
SetProcessDPIAware();
g_scale = GetScreenScaleFactor();
scr_w = GetScreenWidth();
scr_h = GetScreenHeight();
initgraph(scr_w, scr_h);
setbkmode(TRANSPARENT);
setbkcolor(EGERGB(4,4,4));
setrendermode(RENDER_MANUAL);
hwnd = getHWnd();
::SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & (~(WS_CAPTION | WS_SYSMENU | WS_SIZEBOX)));
::SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) & (~(WS_EX_WINDOWEDGE | WS_EX_DLGMODALFRAME)) | WS_EX_LAYERED | WS_EX_TOOLWINDOW);
SetWindowPos(hwnd, HWND_TOPMOST, 0,0, getwidth(), getheight(), SWP_SHOWWINDOW
| SWP_FRAMECHANGED | SWP_DRAWFRAME);
SetLayeredWindowAttributes(hwnd, RGB(4,4,4), 0, LWA_COLORKEY | LWA_ALPHA);
FocusWindow(hwnd);
ShowWindow(GetConsoleWindow(), SW_HIDE);
while(1)
{
if(K(VK_RMENU) && K(VK_OEM_5))
{
Start();
while(K(VK_RMENU) && K(VK_OEM_5));
}
if(K(VK_LMENU) && K(VK_RMENU) && K(VK_LCONTROL) && K(VK_RCONTROL))
break;
}
closegraph();
return 0;
}