-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGhostWindow.cpp
320 lines (298 loc) · 6.91 KB
/
GhostWindow.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
#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include <iostream>
#include <vector>
#include <map>
#include <conio.h>
using namespace std;
HWND hwnd{nullptr};
int alpha{80};
map<HWND, LONG> wnd_longs;
vector<pair<UINT, string>> outputs;
void Output(UINT uForeColor, const char* s)
{
if (outputs.size() > 8)
outputs.erase(outputs.begin());
outputs.emplace_back(uForeColor, string{s});
}
void SetColor(UINT uFore, UINT uBack)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, uFore + uBack * 0x10);
}
void SetPos(short x, short y)
{
COORD pos = {x, y};
HANDLE out=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(out, pos);
}
void LimitAlpha()
{
if (alpha < 10)
alpha = 10;
else if(alpha > 255)
alpha = 255;
}
bool IsWindowHandleValid(HWND _hwnd)
{
if (!_hwnd || _hwnd == INVALID_HANDLE_VALUE)
return false;
SetLastError(0L);
GetWindowLong(_hwnd, GWL_STYLE);
return GetLastError() == 0L;
}
void DisplayWindowInfo()
{
if (!IsWindowHandleValid(hwnd))
{
return;
}
char title[200]{0};
GetWindowTextA(hwnd, title, 200);
Output(14, "**********************");
Output(14, ("窗口句柄:" + to_string(intptr_t(hwnd))).c_str());
Output(14, ("窗口标题:"s + title).c_str());
Output(14, "**********************");
}
void DrawUI()
{
system("cls");
SetPos(0, 0);
SetColor(2, 0);
cout << "---------------------------------\n";
cout << "\n";
SetColor(14, 0);
cout << "请按下:\n\n";
SetColor(15, 0);
cout << " [Space] 捕获前端窗口\n\n";
cout << " [Enter] 捕获鼠标位置窗口(仅未应用窗口)\n\n";
SetColor(IsWindowHandleValid(hwnd)?7:12, 0);
cout << " [1] 应用状态\n\n";
cout << " [2] 恢复原状态\n\n";
SetColor(wnd_longs.empty()?12:7, 0);
cout << " [3] 恢复所有登记过的窗口\n\n";
SetColor(3, 0);
cout << " [S] 设置不透明度\n\n";
SetColor(8, 0);
cout << " [Esc] 退出\n";
SetColor(1, 0);
cout << " 保存的窗口信息数:" << wnd_longs.size() << '\n';
SetColor(2, 0);
cout << "---------------------------------\n";
constexpr int rui_left = 0;
int y = 20;
for (auto& o : outputs)
{
SetPos(rui_left, y);
SetColor(o.first, 0);
cout << o.second;
++y;
}
}
void UpdateWindowLongs()
{
if (wnd_longs.empty())
return;
bool removed{false};
do{
removed = false;
for (auto iter = wnd_longs.begin(); iter != wnd_longs.end(); ++iter)
if (!IsWindowHandleValid(iter->first)
//|| GetWindowLongA(iter->first, GWL_EXSTYLE) == iter->second
)
{
removed = true;
wnd_longs.erase(iter);
break;
}
}while(removed && !wnd_longs.empty());
}
void Read()
{
FILE *fp = fopen("GhostWindow.dat", "r");
if (!fp || feof(fp))
return;
printf("reading...\n");
fscanf(fp, "%d", &alpha);
LimitAlpha();
int i{0};
intptr_t val1;
LONG val2;
wnd_longs.clear();
while (!feof(fp))
{
if (i == 0)
{
if (!fscanf(fp, "%Ld", &val1))
break;
}
else
{
if (!fscanf(fp, "%ld", &val2))
break;
wnd_longs.insert(make_pair((HWND)val1, val2));
}
i = (i + 1) % 2;
}
fclose(fp);
printf("reading completed\n");
UpdateWindowLongs();
}
bool Save()
{
FILE *fp = fopen("GhostWindow.dat", "w");
if (!fp)
{
Output(12, "无法打开文件保存不透明度\n");
return false;
}
LimitAlpha();
fprintf(fp, "%d\n", alpha);
for (auto& pr : wnd_longs)
{
if (IsWindowHandleValid(pr.first))
fprintf(fp, "%Ld %ld\n", pr.first, pr.second);
}
fclose(fp);
return true;
}
void CaptureHere()
{
Output(11, "请将鼠标放在目标窗口上,五秒后将进行窗口捕获\n");
DrawUI();
Sleep(5000);
POINT pt;
GetCursorPos(&pt);
hwnd = WindowFromPoint(pt);
if (!IsWindowHandleValid(hwnd))
{
MessageBeep(MB_ICONERROR);
Output(12, "窗口捕获失败!\n");
}else{
MessageBeep(MB_ICONEXCLAMATION);
Output(10, "捕获成功!\n");
DisplayWindowInfo();
}
}
void CaptureFore()
{
Output(14, "请切换到目标窗口,五秒后进行捕获\n");
DrawUI();
Sleep(5000);
if (!IsWindowHandleValid(hwnd = GetForegroundWindow()))
{
MessageBeep(MB_ICONERROR);
Output(12, "窗口捕获失败!\n");
}else{
MessageBeep(MB_ICONEXCLAMATION);
Output(10, "窗口捕获成功!\n");
DisplayWindowInfo();
}
}
void GhostifyWindow(HWND _hwnd = hwnd)
{
if (!IsWindowHandleValid(_hwnd))
{
Output(12, "你还没有成功捕获窗口,请先捕获窗口!\n");
return;
}
Read();
Output(7, ("当前不透明度:" + to_string(alpha) + '\n').c_str());
LONG t = GetWindowLong(_hwnd, GWL_EXSTYLE);
LONG t0 = t;
t |= WS_EX_LAYERED;
t |= WS_EX_TRANSPARENT;
SetWindowLong(_hwnd, GWL_EXSTYLE, t);
MessageBeep(MB_ICONINFORMATION);
SetLayeredWindowAttributes(_hwnd, 0, alpha, LWA_ALPHA);
SetWindowPos(_hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_DRAWFRAME);
SetForegroundWindow(_hwnd);
wnd_longs[_hwnd] = t0;
Save();
Output(10, ("已对目标窗口 " + to_string(intptr_t(_hwnd)) + " 应用操作\n").c_str());
}
void DeghostifyWindow(HWND _hwnd = hwnd)
{
Read();
if (wnd_longs.find(_hwnd) == wnd_longs.end())
{
Output(14, "错误:该窗口没有被本程序登记过,无法恢复原状态,请考虑重新启动目标窗口\n");
return;
}
MessageBeep(MB_ICONINFORMATION);
LONG t0 = wnd_longs[_hwnd];
SetLayeredWindowAttributes(_hwnd, 0, 255, LWA_ALPHA);
SetWindowPos(hwnd, (HWND_NOTOPMOST), 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_DRAWFRAME);
SetWindowLong(_hwnd, GWL_EXSTYLE, t0);
UpdateWindow(_hwnd);
Output(10, ("已对目标窗口 " + to_string(intptr_t(_hwnd)) + " 应用恢复操作\n").c_str());
}
void DeghostifyRegisteredWindows()
{
if (wnd_longs.empty())
{
Output(13, "没有窗口需要恢复");
return;
}
for (auto& pr : wnd_longs)
DeghostifyWindow(pr.first);
Output(15, ("成功对 " + to_string(wnd_longs.size()) + " 个窗口应用恢复操作").c_str());
}
void SetOpacity()
{
system("cls");
SetColor(15, 0);
cout << "输入不透明度(10~255):";
Output(15, "输入不透明度(10~255):");
cin >> alpha;
if (Save())
{
MessageBeep(MB_ICONEXCLAMATION);
SetColor(10, 0);
cout << "设置成功,将在下一次应用时生效\n";
Output(10, "设置成功,将在下一次应用时生效");
}
Sleep(1500);
}
int main()
{
SetConsoleTitleA("GhostWindow - by Wormwaker");
while(1)
{
Read();
DrawUI();
int ch = getch();
switch(ch)
{
case ' ':{
CaptureFore();
break;
}
case 13:{
CaptureHere();
break;
}
case '1':{
GhostifyWindow();
break;
}
case '2':{
DeghostifyWindow();
break;
}
case '3':{
DeghostifyRegisteredWindows();
break;
}
case 's': case 'S':{
SetOpacity();
break;
}
case 27:{
exit(0);
break;
}
}
}
return 0;
}