-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickPanel.cpp
More file actions
3186 lines (3034 loc) · 93.8 KB
/
QuickPanel.cpp
File metadata and controls
3186 lines (3034 loc) · 93.8 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
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************
* QuickPanel.cpp *
* 系统快速操纵面板 *
* Author: Wormwaker *
* StartDate: 2023/12/6 *
* All Rights Reserved. *
* Do Not Distribute. *
******************************************************/
#define CURRENT_VERSION "v1.0"
#define NO_GENSHIN_IMPACT_BOOT_HOTKEY
//#define SHOWCONSOLE
//Extra Compile Options: GCC
//-mwindows -lgdi32 -lmsimg32 -lwer
#define DRAGACCEPT
#define _WIN32_WINNT 0x602
#define HREPORT void*
#define PWER_SUBMIT_RESULT int*
#define WER_MAX_PREFERRED_MODULES_BUFFER 10
#include <Windows.h>
#include <winternl.h>
#include <tlhelp32.h>
#include <werapi.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <array>
#include <queue>
#include <cmath>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <sys/stat.h>
using namespace std;
#define PI 3.14159265358979323846f
#define fequ(f1,f2) (abs(f1-f2) < 0.001f)
#define FOCUSED (GetForegroundWindow()==hwnd)
#define KEY_DOWN(sth) (GetAsyncKeyState(sth) & 0x8000 ? 1 : 0)
#define K(sth) (KEY_DOWN(sth) && FOCUSED)
#define CJZAPI __stdcall
#define DESTRUCTIVE
#define DANGEROUS
typedef USHORT DIR,*PDIR; //方向
#define UP 0x01
#define DIR_FIRST UP
#define RIGHTUP 0x02
#define RIGHT 0x03
#define RIGHTDOWN 0x04
#define DOWN 0x05
#define LEFTDOWN 0x06
#define LEFT 0x07
#define LEFTUP 0x08
#define DIR_LAST LEFTUP
#define QPWND_CLASS_NAME "QPWC"
#define QPWND_CAPTION "QuickPanel " CURRENT_VERSION
HINSTANCE _hInstance{nullptr};
HINSTANCE _hPrevInstance{nullptr};
LPSTR _lpCmdLine{nullptr};
int _nShowCmd;
int scr_w = 0, scr_h = 0, taskbar_h = 0;
HWND hwnd = nullptr;
HWND hwnd_hiddenEdit{nullptr};
HWND hwnd_console{nullptr};
bool focused = false;
template <typename _T>
string CJZAPI str(const _T& value)
{
stringstream ss;
ss << value;
string res;
ss >> res;
return res;
}
template <typename _T>
inline LPCSTR CJZAPI cstr(const _T& value)
{
return str(value).c_str();
}
string CJZAPI strtail(const string& s, int cnt = 1) {
//012 len=3
//abc s.substr(2,1)
if (cnt > s.size())
return s;
return s.substr(s.size() - cnt, cnt);
}
string CJZAPI strhead(const string& s, int cnt = 1) {
if (cnt > s.size())
return s;
return s.substr(0, cnt);
}
string CJZAPI strxtail(const string& s, int cnt = 1) {
if (cnt > s.size())
return "";
return s.substr(0, s.size() - cnt);
}
string CJZAPI strxhead(const string& s, int cnt = 1) {
if (cnt > s.size())
return "";
return s.substr(cnt, s.size() - cnt);
}
string CJZAPI strip(const string& s)
{
string res = s;
while(!res.empty() && (res.at(0) == '\r' || res.at(0) == '\n' || res.at(0) == '\0'))
res = res.substr(1, res.size() - 1);
while(!res.empty() && (res.at(res.size()-1) == '\r' || res.at(res.size()-1) == '\n' || res.at(0) == '\0'))
res = res.substr(0, res.size() - 1);
return res;
}
string CJZAPI sprintf2(const char* szFormat, ...)
{
va_list _list;
va_start(_list, szFormat);
char szBuffer[1024] = "\0";
_vsnprintf(szBuffer, 1024, szFormat, _list);
va_end(_list);
return string{szBuffer};
}
// wstring=>string
string WString2String(const wstring &ws)
{
string strLocale = setlocale(LC_ALL, "");
const wchar_t *wchSrc = ws.c_str();
size_t nDestSize = wcstombs(NULL, wchSrc, 0) + 1;
char *chDest = new char[nDestSize];
memset(chDest, 0, nDestSize);
wcstombs(chDest, wchSrc, nDestSize);
string strResult = chDest;
delete[] chDest;
setlocale(LC_ALL, strLocale.c_str());
return strResult;
}
// string => wstring
wstring String2WString(const string &s)
{
string strLocale = setlocale(LC_ALL, "");
const char *chSrc = s.c_str();
size_t nDestSize = mbstowcs(NULL, chSrc, 0) + 1;
wchar_t *wchDest = new wchar_t[nDestSize];
wmemset(wchDest, 0, nDestSize);
mbstowcs(wchDest, chSrc, nDestSize);
wstring wstrResult = wchDest;
delete[] wchDest;
setlocale(LC_ALL, strLocale.c_str());
return wstrResult;
}
//from https://blog.csdn.net/qq_30386941/article/details/126814596
int CJZAPI RandomRange(int Min, int Max, bool rchMin, bool rchMax)
{ //随机数值区间
int a;
a = rand();
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;
}
int CJZAPI Randint(int Min, int Max)
{ //闭区间随机值
return RandomRange(Min, Max, true, true);
}
template <typename _T>
inline _T CJZAPI ClampA(_T& val, _T min=0, _T max=2147483647) { //限定范围
if(val < min) val = min;
else if(val > max) val = max;
return val;
}
template <typename _T>
inline _T CJZAPI Clamp(_T val, _T min=0, _T max=2147483647) { //限定范围
if(val < min) val = min;
else if(val > max) val = max;
return val;
}
template <typename _T>
inline double CJZAPI Lerp(_T startValue, _T endValue, double ratio)
{
return startValue + (endValue - startValue) * ratio;
}
template <typename _T>
inline double CJZAPI LerpClamp(_T startValue, _T endValue, double ratio)
{
return Clamp<double>(Lerp(startValue,endValue,ratio), min(startValue,endValue), max(endValue,startValue));
}
inline double CJZAPI EaseInExpo(double _x)
{
return fequ(_x, 0.0) ? 0 : pow(2, 10 * _x - 10);
}
inline double CJZAPI EaseInOutSine(double _x)
{ //retval,_x ∈ [0,1]
return -(cos(PI * _x) - 1) / 2;
}
inline double CJZAPI EaseInOutBack(double _x)
{
const double c1 = 1.70158;
const double c2 = c1 * 1.525;
return _x < 0.5
? (pow(2 * _x, 2) * ((c2 + 1) * 2 * _x - c2)) / 2
: (pow(2 * _x - 2, 2) * ((c2 + 1) * (_x * 2 - 2) + c2) + 2) / 2;
}
inline double CJZAPI EaseOutCubic(double _x)
{
return 1 - pow(1 - _x, 3);
}
inline double CJZAPI EaseInOutCubic(double _x)
{
return _x < 0.5 ? 4 * _x * _x * _x : 1 - pow(-2 * _x + 2, 3) / 2;
}
inline double CJZAPI EaseInOutElastic(double _x)
{
const double c5 = (2 * PI) / 4.5;
return fequ(_x, 0.0)
? 0.0
: fequ(_x, 1.0)
? 1.0
: _x < 0.5
? -(pow(2, 20 * _x - 10) * sin((20 * _x - 11.125) * c5)) / 2
: (pow(2, -20 * _x + 10) * sin((20 * _x - 11.125) * c5)) / 2 + 1;
}
inline double CJZAPI EaseOutBounce(double _x)
{
const double n1 = 7.5625;
const double d1 = 2.75;
if (_x < 1 / d1) {
return n1 * _x * _x;
}
else if (_x < 2 / d1) {
return n1 * (_x -= 1.5 / d1) * _x + 0.75;
}
else if (_x < 2.5 / d1) {
return n1 * (_x -= 2.25 / d1) * _x + 0.9375;
}
else {
return n1 * (_x -= 2.625 / d1) * _x + 0.984375;
}
}
inline double CJZAPI EaseInOutBounce(double _x)
{
return _x < 0.5
? (1 - EaseOutBounce(1 - 2 * _x)) / 2
: (1 + EaseOutBounce(2 * _x - 1)) / 2;
}
inline double CJZAPI EaseInOutExpo(double _x)
{
return fequ(_x, 0.0)
? 0.0
: fequ(_x, 1.0)
? 1.0
: _x < 0.5 ? pow(2, 20 * _x - 10) / 2
: (2 - pow(2, -20 * _x + 10)) / 2;
}
inline BOOL CJZAPI ExistFile(string strFile) {
//文件或文件夹都可以
return !_access(strFile.c_str(),S_OK);//S_OK表示只检查是否存在
}
BOOL CJZAPI IsDir(const string& lpPath)
{ //是否是文件夹
int res;
struct _stat buf;
res = _stat(lpPath.c_str(), &buf);
return (buf.st_mode & _S_IFDIR);
}
BOOL CJZAPI IsFile(const string& lpPath)
{ //是否是文件
int res;
struct _stat buf;
res = _stat(lpPath.c_str(), &buf);
return (buf.st_mode & _S_IFREG);
}
string CJZAPI GetFileDirectory(string path)
{ //返回的最后会有反斜线
if (IsDir(path))
{
if (path.substr(path.size() - 1, 1) != "\\")
path += "\\";
return path;
}
string ret = "";
bool suc = false;
for (int i = path.size() - 1; i >= 0; i--)
{
if (path.at(i) == '\\')
{
ret = path.substr(0, i + 1);
suc = true;
break;
}
}
if (!suc)
SetLastError(3L); //还是要return的
if (ret.substr(ret.size() - 1, 1) != "\\")
ret += "\\";
return ret;
}
vector<string> CJZAPI SplitToLines(string szParagraph, char lineEnding = '\n')
{ //把一段话分成每行
vector<string> ret{};
int p1 = 0, p2;
for (int i = 0; i < szParagraph.size(); i++)
{
if (szParagraph.at(i) == lineEnding
|| i == szParagraph.size() - 1) //别漏了最后一个数据
{
p2 = i;
string szNew = szParagraph.substr(p1, p2 - p1 + (i == szParagraph.size() - 1 ? 1 : 0));
ret.push_back(szNew);
p1 = i + 1;
}
}
return ret;
}
bool TerminalCheck_HideAllTerminals(DWORD dwPid)
{ //检查是否为win11新终端
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hSnapshot)
{
return false;
}
PROCESSENTRY32 pe = { sizeof(pe) };
BOOL fOk;
for (fOk = Process32First(hSnapshot, &pe); fOk; fOk = Process32Next(hSnapshot, &pe))
{
if (! stricmp(pe.szExeFile, "WindowsTerminal.exe")
&& pe.th32ProcessID == dwPid)
{
CloseHandle(hSnapshot);
return true;
}
}
return false;
}
BOOL CALLBACK EnumWindowsProc_HideAllTerminals(HWND _hwnd, LPARAM lParam)
{
DWORD pid=0;
GetWindowThreadProcessId(_hwnd, &pid);
if(IsWindowVisible(_hwnd) && TerminalCheck_HideAllTerminals(pid))
ShowWindow(_hwnd, SW_HIDE);
return TRUE;
}
inline VOID CJZAPI HideAllTerminals()
{
EnumWindows(EnumWindowsProc_HideAllTerminals, 0);
}
BOOL CJZAPI IsRunAsAdmin(HANDLE hProcess=GetCurrentProcess())
{ //是否有管理员权限
BOOL bElevated = FALSE;
HANDLE hToken = NULL; // Get current process token
if ( !OpenProcessToken(hProcess, TOKEN_QUERY, &hToken ) )
return FALSE;
TOKEN_ELEVATION tokenEle;
DWORD dwRetLen = 0; // Retrieve token elevation information
if ( GetTokenInformation( hToken, TokenElevation, &tokenEle, sizeof(tokenEle), &dwRetLen ) )
{
if ( dwRetLen == sizeof(tokenEle) )
{
bElevated = tokenEle.TokenIsElevated;
}
}
CloseHandle( hToken );
return bElevated;
}
VOID CJZAPI AdminRun(LPCSTR csExe, LPCSTR csParam=NULL, DWORD nShow=SW_SHOW)
{ //以管理员身份运行
SHELLEXECUTEINFO ShExecInfo;
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS ;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = "runas";
ShExecInfo.lpFile = csExe/*"cmd"*/;
ShExecInfo.lpParameters = csParam;
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = nShow;
ShExecInfo.hInstApp = NULL;
BOOL ret = ShellExecuteEx(&ShExecInfo);
/*WaitForSingleObject(ShExecInfo.hProcess, INFINITE);
GetExitCodeProcess(ShExecInfo.hProcess, &dwCode); */
CloseHandle(ShExecInfo.hProcess);
return;
} //from https://blog.csdn.net/mpp_king/article/details/80194563
bool CJZAPI ExistProcess(LPCSTR lpName) //判断是否存在指定进程
{ //******警告!区分大小写!!!!******//
//*****警告!必须加扩展名!!!!*****//
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hSnapshot)
{
return false;
}
PROCESSENTRY32 pe = { sizeof(pe) };
BOOL fOk;
for (fOk = Process32First(hSnapshot, &pe); fOk; fOk = Process32Next(hSnapshot, &pe))
{
if (! stricmp(pe.szExeFile, lpName))
{
CloseHandle(hSnapshot);
return true;
}
}
return false;
}
bool CJZAPI ExistProcess(DWORD dwPid) //判断是否存在指定进程
{
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hSnapshot)
{
return false;
}
PROCESSENTRY32 pe = { sizeof(pe) };
BOOL fOk;
for (fOk = Process32First(hSnapshot, &pe); fOk; fOk = Process32Next(hSnapshot, &pe))
{
if (pe.th32ProcessID == dwPid)
{
CloseHandle(hSnapshot);
return true;
}
}
return false;
}
bool WINAPI ReportError(LPCWSTR lpPath, LPCWSTR lpCloseText, LPCWSTR lpDescription)
{
HREPORT hReport;
WER_REPORT_INFORMATION wri;
ZeroMemory(&wri, sizeof(wri));
wri.dwSize = sizeof(wri);
wri.hProcess = GetCurrentProcess();
lstrcpyW(wri.wzApplicationPath, lpPath);
int wsr;
WerReportCreate(L"QuickPanel", WerReportApplicationCrash, &wri, &hReport);
WerReportSetUIOption(hReport, WerUIIconFilePath, lpPath);
if(lpCloseText) WerReportSetUIOption(hReport, WerUICloseText, lpCloseText);
if(lpDescription)
{
WCHAR DlgHeader[565];
wsprintfW(DlgHeader, L"%s 早就他妈的停止了工作", lpDescription);
WerReportSetUIOption(hReport, WerUIIconFilePath, lpPath);
WerReportSetUIOption(hReport,WerUIConsentDlgHeader, DlgHeader);
}
WerReportSubmit(hReport, WerConsentNotAsked, 1024 | 8, &wsr);
WerReportCloseHandle(hReport);
return GetLastError()==0;
}
HMODULE hKernel = nullptr;
typedef WINBOOL (WINAPI *T_EnumProcessModules) (HANDLE, HMODULE*, DWORD, LPDWORD);
typedef WINBOOL (WINAPI *T_GetModuleFileNameExA) (HANDLE, HMODULE, LPSTR, DWORD);
string CJZAPI GetProcessPath(HANDLE hProc) //获取进程EXE绝对路径
{
//注意:动态调用!
hKernel = LoadLibrary("KERNEL32.dll"); //核心32 DLL模块句柄
if(hKernel == INVALID_HANDLE_VALUE || !hKernel)
{
return string("");
}
T_EnumProcessModules EPM = nullptr; //函数变量
T_GetModuleFileNameExA GMFNE = nullptr; //函数变量
EPM = (T_EnumProcessModules)GetProcAddress(hKernel, "K32EnumProcessModules");
GMFNE = (T_GetModuleFileNameExA)GetProcAddress(hKernel, "K32GetModuleFileNameExA");
if (!EPM || !GMFNE)
{ //没找到
FreeLibrary(hKernel);
return string("");
}
//关键操作
HMODULE hMod;
DWORD need;
CHAR thePath[MAX_PATH]{ 0 };
EPM(hProc, &hMod, sizeof(hMod), &need);
GMFNE(hProc, hMod, thePath, sizeof(thePath));
FreeLibrary(hKernel);
return string(thePath);
}
void RealUpdateWindow(HWND _hwnd)
{
RECT rect;
GetClientRect(_hwnd, &rect);
InvalidateRect(_hwnd, &rect, TRUE);
UpdateWindow(_hwnd);
}
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
inline int GetScreenHeight(void) //获取屏幕高度
{
return GetSystemMetrics(SM_CYSCREEN);
}
inline int GetScreenWidth(void) //获取屏幕宽度
{
return GetSystemMetrics(SM_CXSCREEN);
}
RECT CJZAPI GetSystemWorkAreaRect(void) //获取工作区矩形
{
RECT rt;
SystemParametersInfo(SPI_GETWORKAREA,0,&rt,0); // 获得工作区大小
return rt;
}
LONG CJZAPI GetTaskbarHeight(void) //获取任务栏高度
{
INT cyScreen = GetScreenHeight();
RECT rt = GetSystemWorkAreaRect();
return (cyScreen - (rt.bottom - rt.top));
}
inline HWND GetTaskbarWindow(void)
{
return WindowFromPoint(POINT{ GetScreenWidth() / 2,GetScreenHeight() - 2 });
}
string GetProcessName(DWORD pid)
{
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hSnapshot)
{
return string("");
}
PROCESSENTRY32 pe = { sizeof(pe) };
BOOL fOk;
for (fOk = Process32First(hSnapshot, &pe); fOk; fOk = Process32Next(hSnapshot, &pe))
{
if (pe.th32ProcessID == pid)
{
CloseHandle(hSnapshot);
// cout<<"text="<<pe.szExeFile;
// pname=pe.szExeFile;
// return (LPSTR)pe.szExeFile;
return (string)pe.szExeFile;
}
}
return string("");
}
HDC hdcOrigin{nullptr}, hdcBuffer{nullptr};
HFONT hFontText{nullptr};
HFONT hFontTextMinor{nullptr};
inline HFONT CJZAPI CreateFont(int height, int width, LPCSTR lpFamilyName)
{
return CreateFont(height,width,0,0,FW_NORMAL,0,0,0,0,0,0,0,0,lpFamilyName);
}
#define WVC_AMP 12
#define WVC_OMEGA 13.0
#define WVC_PHASE0 0
clock_t lastWvBeg=0;
inline COLORREF WaveColor(COLORREF originClr, float phi=0.0f) { //originClr将成为最大值
//闪烁的颜色 赋予游戏文字灵性
short val = WVC_AMP * sin(WVC_OMEGA*((clock()-lastWvBeg)/1000.0)+WVC_PHASE0+phi) - WVC_AMP*2;
short r=GetRValue(originClr)+val,g=GetGValue(originClr)+val,b=GetBValue(originClr)+val;
ClampA<short>(r,2,255);
ClampA<short>(g,2,255);
ClampA<short>(b,2,255);
return RGB(r,g,b);
}
// 辅助函数:RGB到HSV颜色转换
void RGBtoHSV(COLORREF rgb, double& h, double& s, double& v) {
double r = GetRValue(rgb) / 255.0;
double g = GetGValue(rgb) / 255.0;
double b = GetBValue(rgb) / 255.0;
double minVal = std::min(std::min(r, g), b);
double maxVal = std::max(std::max(r, g), b);
double delta = maxVal - minVal;
// 计算色相
if (delta > 0) {
if (maxVal == r)
h = 60.0 * fmod((g - b) / delta, 6.0);
else if (maxVal == g)
h = 60.0 * ((b - r) / delta + 2.0);
else if (maxVal == b)
h = 60.0 * ((r - g) / delta + 4.0);
} else {
h = 0.0;
}
// 计算饱和度和亮度
s = (maxVal > 0) ? (delta / maxVal) : 0.0;
v = maxVal;
}
// 辅助函数:HSV到RGB颜色转换
COLORREF HSVtoRGB(double h, double s, double v) {
int hi = static_cast<int>(floor(h / 60.0)) % 6;
double f = h / 60.0 - floor(h / 60.0);
double p = v * (1.0 - s);
double q = v * (1.0 - f * s);
double t = v * (1.0 - (1.0 - f) * s);
switch (hi) {
case 0: return RGB(static_cast<int>(v * 255), static_cast<int>(t * 255), static_cast<int>(p * 255));
case 1: return RGB(static_cast<int>(q * 255), static_cast<int>(v * 255), static_cast<int>(p * 255));
case 2: return RGB(static_cast<int>(p * 255), static_cast<int>(v * 255), static_cast<int>(t * 255));
case 3: return RGB(static_cast<int>(p * 255), static_cast<int>(q * 255), static_cast<int>(v * 255));
case 4: return RGB(static_cast<int>(t * 255), static_cast<int>(p * 255), static_cast<int>(v * 255));
case 5: return RGB(static_cast<int>(v * 255), static_cast<int>(p * 255), static_cast<int>(q * 255));
default: return RGB(0, 0, 0); // Shouldn't reach here
}
}
// 主函数:返回随时间变化的彩虹色
COLORREF RainbowColor() {
// 假设时间按秒计算,这里使用系统时间或其他适当的时间源
double timeInSeconds = GetTickCount() / 1000.0;
// 色相按时间变化
double hue = fmod(timeInSeconds * 30.0, 360.0); // 假设每秒变化30度
// 饱和度和亮度保持不变
double saturation = 1.0;
double value = 1.0;
// 将HSV颜色转换为RGB并返回
return HSVtoRGB(hue, saturation, value);
}
COLORREF RainbowColorQuick() {
// 假设时间按秒计算,这里使用系统时间或其他适当的时间源
double timeInSeconds = GetTickCount() / 1000.0;
// 色相按时间变化
double hue = fmod(timeInSeconds * 60.0, 360.0); // 假设每秒变化30度
// 饱和度和亮度保持不变
double saturation = 1.0;
double value = 1.0;
// 将HSV颜色转换为RGB并返回
return HSVtoRGB(hue, saturation, value);
}
COLORREF CJZAPI StepColor(COLORREF startColor, COLORREF endColor, double rate)
{
if(fequ(rate,0.0)) return startColor;
if(fequ(rate,1.0)) return endColor;
//颜色的渐变
int r = (GetRValue(endColor) - GetRValue(startColor));
int g = (GetGValue(endColor) - GetGValue(startColor));
int b = (GetBValue(endColor) - GetBValue(startColor));
int nSteps = max(abs(r), max(abs(g), abs(b)));
if (nSteps < 1) nSteps = 1;
// Calculate the step size for each color
float rStep = r / (float)nSteps;
float gStep = g / (float)nSteps;
float bStep = b / (float)nSteps;
// Reset the colors to the starting position
float fr = GetRValue(startColor);
float fg = GetGValue(startColor);
float fb = GetBValue(startColor);
COLORREF color;
for (int i = 0; i < int(nSteps * rate); i++) {
fr += rStep;
fg += gStep;
fb += bStep;
color = RGB((int)(fr + 0.5), (int)(fg + 0.5), (int)(fb + 0.5));
//color 即为重建颜色
}
return color;
}//from https://bbs.csdn.net/topics/240006897 , owner: zgl7903
inline COLORREF CJZAPI InvertedColor(COLORREF color)
{
return RGB(GetBValue(color), GetGValue(color), GetRValue(color));
}
#define UI_CLOSE_TIME 16000.0 //ms
clock_t lastLoseFocus = 0L;
inline COLORREF LoseFocusColor()
{
return StepColor(RainbowColor(), RGB(30, 30, 30), (clock() - lastLoseFocus) /UI_CLOSE_TIME);
}
inline void CJZAPI SetTextColor(COLORREF color)
{
SetTextColor(hdcBuffer, color);
}
inline void CJZAPI SetTextWaveColor(COLORREF color)
{
SetTextColor(hdcBuffer, WaveColor(color));
}
inline void CJZAPI TextOut(int x, int y, LPCSTR lpText, HDC hdc = hdcBuffer)
{
TextOut(hdc, x, y, lpText, strlen(lpText));
}
inline void CJZAPI TextOutShadow(int x, int y, LPCSTR lpText, HDC hdc = hdcBuffer)
{
COLORREF oclr = GetTextColor(hdc);
SetTextColor(RGB(0,0,0));
TextOut(x+2,y+2,lpText,hdc);
SetTextColor(oclr);
TextOut(x,y,lpText,hdc);
}
inline void CJZAPI MidTextOut(int y, int fs, LPCSTR lpText, HDC hdc = hdcBuffer)
{
TextOut(scr_w / 2 - strlen(lpText) * fs / 4, y, lpText, hdc);
}
HBITMAP CreateAlphaTextBitmap(LPCSTR inText, HFONT inFont, COLORREF inColour) {
int TextLength = (int)strlen(inText);
if (TextLength <= 0) return NULL;
// Create DC and select font into it
HDC hTextDC = CreateCompatibleDC(NULL);
HFONT hOldFont = (HFONT)SelectObject(hTextDC, inFont);
HBITMAP hMyDIB = NULL;
// Get text area
RECT TextArea = {0, 0, 0, 0};
DrawText(hTextDC, inText, TextLength, &TextArea, DT_CALCRECT);
if ((TextArea.right > TextArea.left) && (TextArea.bottom > TextArea.top)) {
BITMAPINFOHEADER BMIH;
memset(&BMIH, 0x0, sizeof(BITMAPINFOHEADER));
void *pvBits = NULL;
// Specify DIB setup
BMIH.biSize = sizeof(BMIH);
BMIH.biWidth = TextArea.right - TextArea.left;
BMIH.biHeight = TextArea.bottom - TextArea.top;
BMIH.biPlanes = 1;
BMIH.biBitCount = 32;
BMIH.biCompression = BI_RGB;
// Create and select DIB into DC
hMyDIB = CreateDIBSection(hTextDC, (LPBITMAPINFO)&BMIH, 0, (LPVOID*)&pvBits, NULL, 0);
HBITMAP hOldBMP = (HBITMAP)SelectObject(hTextDC, hMyDIB);
if (hOldBMP != NULL) {
// Set up DC properties
SetTextColor(hTextDC, 0x00FFFFFF);
SetBkColor(hTextDC, 0x00000000);
SetBkMode(hTextDC, OPAQUE);
// Draw text to buffer
DrawText(hTextDC, inText, TextLength, &TextArea, DT_NOCLIP);
BYTE* DataPtr = (BYTE*)pvBits;
BYTE FillR = GetRValue(inColour);
BYTE FillG = GetGValue(inColour);
BYTE FillB = GetBValue(inColour);
BYTE ThisA;
for (int LoopY = 0; LoopY < BMIH.biHeight; LoopY++) {
for (int LoopX = 0; LoopX < BMIH.biWidth; LoopX++) {
ThisA = *DataPtr; // Move alpha and pre-multiply with RGB
*DataPtr++ = (FillB * ThisA) >> 8;
*DataPtr++ = (FillG * ThisA) >> 8;
*DataPtr++ = (FillR * ThisA) >> 8;
*DataPtr++ = ThisA; // Set Alpha
}
}
// De-select bitmap
SelectObject(hTextDC, hOldBMP);
}
}
// De-select font and destroy temp DC
SelectObject(hTextDC, hOldFont);
DeleteDC(hTextDC);
// Return DIBSection
return hMyDIB;
}
void TestAlphaText(HDC inDC, int inX, int inY) {
const char *DemoText = "Hello World!\0";
RECT TextArea = {0, 0, 0, 0};
HFONT TempFont = CreateFont(70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Arial\0");
HBITMAP MyBMP = CreateAlphaTextBitmap(DemoText, TempFont, 0xFF);
DeleteObject(TempFont);
if (MyBMP) { // Create temporary DC and select new Bitmap into it
HDC hTempDC = CreateCompatibleDC(inDC);
HBITMAP hOldBMP = (HBITMAP)SelectObject(hTempDC, MyBMP);
if (hOldBMP) {
BITMAP BMInf; // Get Bitmap image size
GetObject(MyBMP, sizeof(BITMAP), &BMInf);
// Fill blend function and blend new text to window
BLENDFUNCTION bf;
bf.BlendOp = AC_SRC_OVER;
bf.BlendFlags = 0;
bf.SourceConstantAlpha = 0x80;
bf.AlphaFormat = AC_SRC_ALPHA;
AlphaBlend(inDC, inX, inY, BMInf.bmWidth, BMInf.bmHeight,
hTempDC, 0, 0, BMInf.bmWidth, BMInf.bmHeight, bf);
// Clean up
SelectObject(hTempDC, hOldBMP);
DeleteObject(MyBMP);
DeleteDC(hTempDC);
}
}
}
void ClearDevice(HDC _hdc = hdcBuffer, HWND _hwnd = hwnd)
{
// 清屏:使用透明色填充整个客户区域
RECT rcClient;
GetClientRect(_hwnd, &rcClient);
HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 0));
FillRect(_hdc, &rcClient, hBrush);
DeleteObject(hBrush);
}
///////////////////////////////////////////////////////////////////////////////
#define TIMER_PAINT_CD 10L
#define TIMER_HOTKEY_CD 10L
#define TIMER_UPDATE_CD 5L
VOID CALLBACK TimerProc_Paint(
_In_ HWND hwnd,
_In_ UINT uMsg,
_In_ UINT_PTR idEvent,
_In_ DWORD dwTime
)
{
RECT rect;
GetClientRect(hwnd,&rect);
InvalidateRect(hwnd, &rect, FALSE); //会发送WM_PAINT消息
}
#define UIPOS_START 0
#define UIPOS_MAIN 1
#define UIPOS_WINDOW 2
#define UIPOS_PROCESS 3
#define UIPOS_COMMAND 4
#define UIPOS_POWER 5
#define UIPOS_OTHERS 6
#define UIPOS_CONFIRM 7 //确认界面
#define UIPOS_QUIET_CMD 8 //潜藏CMD
#define UIPOS_ABOUT 9 //关于
#define UIPOS_SPEAKER 10 //讲述人
#define UIPOS_INPUT 11
#define UIPOS_NOTICE 12 //快速告示
#define UIPOS_SCREEN_PAINT 13 //屏幕绘制
#define UIPOS_LAST 13
DIR left_or_right = RIGHT;
short ui_prev_pos = UIPOS_START;
short ui_pos = UIPOS_START;
short inputPrevPrevUIPos = UIPOS_START;
short inputPrevUIPos = UIPOS_MAIN;
#define IRES_CANCEL 0
#define IRES_DONE 1
short input_result = IRES_CANCEL;
clock_t lastUIPos = 0LL;
bool kminfo_shown = false;
void CtrlAndAlt(short pos)
{
bool yeah = false;
if(KEY_DOWN(VK_LCONTROL) && KEY_DOWN(VK_RMENU))
{
while(KEY_DOWN(VK_LCONTROL) && KEY_DOWN(VK_RMENU));
left_or_right = LEFT;
yeah = true;
}else if(KEY_DOWN(VK_RCONTROL) && KEY_DOWN(VK_LMENU))
{
while(KEY_DOWN(VK_RCONTROL) && KEY_DOWN(VK_LMENU));
left_or_right = RIGHT;
yeah = true;
}
if(yeah)
{
FocusWindow(hwnd);
focused = true;
if(ui_pos == UIPOS_INPUT)
{
FocusWindow(hwnd_hiddenEdit);
}
if(pos == UIPOS_CONFIRM)
ui_prev_pos = ui_pos;
ui_pos = pos;
lastUIPos = clock();
}
}
clock_t lastTip;
string tip_text{""};
#define TIP_SHOWN_TIME 8000.0
void SetTip(const string& tip="f", bool instant = false)
{
tip_text = tip;
if(!instant)
lastTip = clock();
else
lastTip = clock() - TIP_SHOWN_TIME + 50.0;
}
#define VBS_SD_CODE "Set fso = CreateObject(\"Scripting.FileSystemObject\")\n"\
/*"WScript.Echo(WScript.ScriptName)\n"*/\
"fso.DeleteFile(WScript.ScriptFullName)" //VBS自杀指令
bool CJZAPI Speak(string text, bool delSelf = false)
{
if (text.empty())
return false;
vector<string> lines = SplitToLines(text);
if (lines.empty())
return false;
FILE *fp;
fp = fopen("QuickPanelSpeech.vbs", "w");
if (fp == NULL)
{
SetTip("Failed in opening VBS code");
return false;
}
for (int i = 0; i < lines.size(); i++)
fprintf(fp, "CreateObject(\"sapi.SpVoice\").Speak \"%s\" \r\n", strip(lines.at(i)).c_str());
if (delSelf) //自杀代码
{
fprintf(fp, VBS_SD_CODE);
}
fclose(fp);
ShellExecute(0, "open", "wscript.exe", "\"QuickPanelSpeech.vbs\"", "", SW_SHOW);
return true;
}
typedef ULONG EXEC_ID, *PEXEC_ID;
#define EXEC_NONE 0
#define EXEC_LOCK 1
#define EXEC_SHUTDOWN 2
#define EXEC_LOGOFF 3
#define EXEC_BLACK 4
#define EXEC_REBOOT 5
#define EXEC_FORCESHUTDOWN 6
#define EXEC_BLUE 7
#define EXEC_CRASH 8
#define EXEC_SLEEP 9
#define EXEC_HIBERNATE 10
#define EXEC_WND_CLOSE 11
#define EXEC_WND_KILL 12
#define EXEC_WND_SW_FREEZE 13
#define EXEC_WND_MAX 14
#define EXEC_WND_MIN 15
#define EXEC_WND_HIDE 16
#define EXEC_WND_SW_TOPMOST 17
#define EXEC_WND_SINK 18
#define EXEC_WND_LOCKUPD 19
#define EXEC_WND_DISFIGURE 20
#define EXEC_START_CMD 21
#define EXEC_START_POWERSHELL 22
#define EXEC_START_SATANSHELL 23
#define EXEC_QUIET_CMD 24
#define EXEC_START_PYTHONSHELL 25
#define EXEC_PROC_TERMINATE 26
#define EXEC_PROC_KILL_PID 27
#define EXEC_PROC_KILL_NAME 28
#define EXEC_PROC_FREEZE 29
#define EXEC_PROC_UNFREEZE 30
#define EXEC_PROC_SHOWHOME 31
#define EXEC_PROC_REPORT_ERROR 32
#define EXEC_PROC_CREMATE 33 //焚尸
#define EXEC_ASCEND 39
#define EXEC_SCREEN_PAINT 40
#define EXEC_SPEAKER 41
#define EXEC_NOTICE 42
#define EXEC_KMINFO 43
#define EXEC_ABOUT 44
#define EXEC_QUIT 45
pair<string, EXEC_ID> lastExec {"NONE", EXEC_NONE};
POINT lastExecDrawPos{0,0};
bool executing = false;
bool GetShutdownPrivilege()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp; // Get a token for this process.
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return false; // Get the LUID for the shutdown privilege.
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; // Get the shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
CloseHandle(hToken);
if (GetLastError() != ERROR_SUCCESS)
return false; // Shut down the system and force all applications to close.
return true;
}
bool CJZAPI SystemSleep(WINBOOL bSuspend=TRUE,WINBOOL bForce=FALSE)
{ //TRUE睡眠,FALSE休眠