-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleLoader.cpp
More file actions
453 lines (373 loc) · 11.3 KB
/
ConsoleLoader.cpp
File metadata and controls
453 lines (373 loc) · 11.3 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
#define UNICODE
#define _UNICODE
#include <windows.h>
#include <dwmapi.h>
#include <richedit.h>
#include <string>
#include <cwctype>
#pragma comment(lib, "dwmapi.lib")
/* ================= 全局(核心原样) ================= */
HWND g_hWnd = nullptr;
HWND g_hEdit = nullptr;
HANDLE g_hRead = nullptr;
HANDLE g_hProc = nullptr;
HANDLE g_hJob = nullptr;
#define WM_APPEND (WM_APP + 1)
/* ================= 仅为滚轮支持新增:RichEdit 子类化 ================= */
static WNDPROC g_OrigEditProc = nullptr;
static LRESULT CALLBACK EditSubclassProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_MOUSEWHEEL:
{
// 即使隐藏滚动条,也确保滚轮能滚
int delta = GET_WHEEL_DELTA_WPARAM(wParam);
int linesPerNotch = 3; // 你想更快可改 5/8
int lines = (delta / WHEEL_DELTA) * linesPerNotch;
// delta>0 通常表示“滚轮向前”,内容应上移 => EM_LINESCROLL 传负数
SendMessageW(hWnd, EM_LINESCROLL, 0, (LPARAM)(-lines));
return 0;
}
}
return CallWindowProcW(g_OrigEditProc, hWnd, msg, wParam, lParam);
}
/* ================= Dark Mode ================= */
bool IsDarkMode()
{
HKEY hKey;
DWORD v = 1, sz = sizeof(v);
if (RegOpenKeyExW(
HKEY_CURRENT_USER,
L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
0, KEY_READ, &hKey) == ERROR_SUCCESS)
{
RegQueryValueExW(
hKey,
L"AppsUseLightTheme",
nullptr, nullptr,
(LPBYTE)&v, &sz);
RegCloseKey(hKey);
}
return v == 0;
}
void AppendText(const std::wstring& text)
{
// 1. 插入前长度
GETTEXTLENGTHEX gtl{};
gtl.flags = GTL_NUMCHARS;
gtl.codepage = 1200; // UTF-16
LONG base = (LONG)SendMessageW(
g_hEdit,
EM_GETTEXTLENGTHEX,
(WPARAM)>l,
0);
// 2. 插入文本
SendMessageW(g_hEdit, EM_SETSEL, -1, -1);
SendMessageW(g_hEdit, EM_REPLACESEL, FALSE, (LPARAM)text.c_str());
// ====== 开始解析本行 ======
const COLORREF RED = RGB(255, 80, 80);
const COLORREF YELLOW = RGB(255, 200, 80);
const COLORREF GREEN = RGB(120, 220, 120);
const wchar_t* s = text.c_str();
int len = (int)text.size();
int latency = -1;
int latPos = -1;
int latLen = 0;
// 3. 找 延迟数字 + ms
for (int i = 0; i < len; i++)
{
if (iswdigit(s[i]))
{
int j = i;
int v = 0;
while (j < len && iswdigit(s[j]))
{
v = v * 10 + (s[j] - L'0');
j++;
}
if (j + 1 < len && s[j] == L'm' && s[j + 1] == L's')
{
latency = v;
latPos = i;
latLen = (j + 2) - i;
break;
}
}
}
if (latency < 0)
{
SendMessageW(g_hEdit, EM_SCROLLCARET, 0, 0);
return;
}
COLORREF color =
(latency > 500) ? RED :
(latency >= 200) ? YELLOW :
GREEN;
// 4. 给 延迟(ms) 着色
{
CHARRANGE cr{ base + latPos, base + latPos + latLen };
SendMessageW(g_hEdit, EM_EXSETSEL, 0, (LPARAM)&cr);
CHARFORMATW cf{};
cf.cbSize = sizeof(cf);
cf.dwMask = CFM_COLOR;
cf.crTextColor = color;
SendMessageW(g_hEdit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
}
// 5. 给 “域名/IP:端口” 着色
// 规则:行首到第一个空格 / tab / '['
int hostEnd = 0;
while (hostEnd < len &&
s[hostEnd] != L' ' &&
s[hostEnd] != L'\t' &&
s[hostEnd] != L'[')
{
hostEnd++;
}
if (hostEnd > 0)
{
CHARRANGE cr{ base, base + hostEnd };
SendMessageW(g_hEdit, EM_EXSETSEL, 0, (LPARAM)&cr);
CHARFORMATW cf{};
cf.cbSize = sizeof(cf);
cf.dwMask = CFM_COLOR;
cf.crTextColor = color;
SendMessageW(g_hEdit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
}
SendMessageW(g_hEdit, EM_SCROLLCARET, 0, 0);
}
void ApplyDarkTitleBar(HWND hwnd)
{
BOOL dark = IsDarkMode();
DwmSetWindowAttribute(
hwnd,
20 /* DWMWA_USE_IMMERSIVE_DARK_MODE */,
&dark,
sizeof(dark));
}
void ApplyDarkRichEdit(HWND hEdit)
{
bool dark = IsDarkMode();
SendMessageW(
hEdit,
EM_SETBKGNDCOLOR,
0,
dark ? RGB(30,30,30) : RGB(255,255,255));
CHARFORMATW cf{};
cf.cbSize = sizeof(cf);
cf.dwMask = CFM_COLOR | CFM_FACE | CFM_SIZE;
cf.crTextColor = dark ? RGB(220,220,220) : RGB(0,0,0);
lstrcpyW(cf.szFaceName, L"Consolas");
cf.yHeight = 180; // 9pt
SendMessageW(hEdit, EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&cf);
}
/* ================= 工具 ================= */
std::wstring AnsiToWideOEM(const char* s)
{
int len = MultiByteToWideChar(GetOEMCP(), 0, s, -1, nullptr, 0);
std::wstring ws(len, 0);
MultiByteToWideChar(GetOEMCP(), 0, s, -1, &ws[0], len);
return ws;
}
/* ================= 管道线程 ================= */
DWORD WINAPI PipeThread(LPVOID)
{
char buf[4096];
DWORD read;
std::string partial;
while (ReadFile(g_hRead, buf, sizeof(buf)-1, &read, nullptr))
{
if (!read) break;
partial.append(buf, read);
while (true)
{
size_t rn = partial.find("\r\n");
size_t r = partial.find('\r');
size_t n = partial.find('\n');
size_t pos = std::string::npos;
size_t eat = 0;
if (rn != std::string::npos)
{
pos = rn; eat = 2;
}
else if (r != std::string::npos && (n == std::string::npos || r < n))
{
pos = r; eat = 1;
}
else if (n != std::string::npos)
{
pos = n; eat = 1;
}
else
{
break;
}
std::string line = partial.substr(0, pos);
partial.erase(0, pos + eat);
// 统一成 CRLF,给 RichEdit 追加
line += "\r\n";
auto* ws = new std::wstring(AnsiToWideOEM(line.c_str()));
PostMessageW(g_hWnd, WM_APPEND, 0, (LPARAM)ws);
}
}
if (!partial.empty())
{
partial += "\r\n";
auto* ws = new std::wstring(AnsiToWideOEM(partial.c_str()));
PostMessageW(g_hWnd, WM_APPEND, 0, (LPARAM)ws);
}
return 0;
}
/* ================= 窗口过程 ================= */
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM w, LPARAM l)
{
switch (msg)
{
case WM_CREATE:
{
LoadLibraryW(L"Msftedit.dll");
// 设置主窗口图标(来自资源 IDI_APPICON)
HICON hBig = (HICON)LoadImageW(GetModuleHandleW(nullptr), L"IDI_APPICON",
IMAGE_ICON, 32, 32, LR_DEFAULTCOLOR);
HICON hSm = (HICON)LoadImageW(GetModuleHandleW(nullptr), L"IDI_APPICON",
IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
SendMessageW(hWnd, WM_SETICON, ICON_BIG, (LPARAM)hBig);
SendMessageW(hWnd, WM_SETICON, ICON_SMALL, (LPARAM)hSm);
// 关键改动:
// 1) 去掉 WS_VSCROLL 隐藏滚动条
// 2) 加 ES_DISABLENOSCROLL,确保“无滚动条”时仍允许滚动逻辑
g_hEdit = CreateWindowExW(
0, // ★ 不再用 WS_EX_CLIENTEDGE
MSFTEDIT_CLASS,
L"",
WS_CHILD | WS_VISIBLE |
ES_MULTILINE | ES_READONLY |
ES_AUTOVSCROLL | ES_DISABLENOSCROLL, // ★ 替换:去 WS_VSCROLL,加 ES_DISABLENOSCROLL
1, 1, 0, 0,
hWnd, nullptr, nullptr, nullptr);
// RichEdit 子类化:只为滚轮支持,不改你现有输出逻辑
g_OrigEditProc = (WNDPROC)SetWindowLongPtrW(g_hEdit, GWLP_WNDPROC, (LONG_PTR)EditSubclassProc);
ApplyDarkTitleBar(hWnd);
ApplyDarkRichEdit(g_hEdit);
return 0;
}
case WM_SETTINGCHANGE:
ApplyDarkTitleBar(hWnd);
ApplyDarkRichEdit(g_hEdit);
InvalidateRect(hWnd, nullptr, TRUE);
return 0;
case WM_SIZE:
MoveWindow(
g_hEdit,
1, 1,
LOWORD(l) - 2,
HIWORD(l) - 2,
TRUE);
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
if (IsDarkMode())
{
RECT rc;
GetClientRect(hWnd, &rc);
HBRUSH hBrush = CreateSolidBrush(RGB(55,55,55));
FrameRect(hdc, &rc, hBrush);
DeleteObject(hBrush);
}
EndPaint(hWnd, &ps);
return 0;
}
case WM_APPEND:
{
auto* ws = (std::wstring*)l;
AppendText(*ws);
delete ws;
return 0;
}
case WM_DESTROY:
if (g_hJob) CloseHandle(g_hJob);
if (g_hRead) CloseHandle(g_hRead);
PostQuitMessage(0);
return 0;
}
return DefWindowProcW(hWnd, msg, w, l);
}
/* ================= 入口 ================= */
int WINAPI wWinMain(
HINSTANCE hInst,
HINSTANCE,
PWSTR lpCmdLine,
int)
{
// ★ 新增:释放 Explorer 自动分配的控制台
FreeConsole();
if (!lpCmdLine || !*lpCmdLine)
{
MessageBoxW(nullptr, L"请指定要运行的程序", L"ConsoleLoader", MB_ICONERROR);
return 0;
}
WNDCLASSW wc{};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInst;
wc.lpszClassName = L"ConsoleLoader";
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
RegisterClassW(&wc);
std::wstring title = L"ConsoleLoader - ";
title += lpCmdLine;
// 默认尺寸
const int baseW = 1280;
const int baseH = 720;
g_hWnd = CreateWindowW(
wc.lpszClassName,
title.c_str(),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT,
baseW, baseH, // ★ 改这里
nullptr, nullptr, hInst, nullptr);
/* ---------- Job Object ---------- */
g_hJob = CreateJobObjectW(nullptr, nullptr);
JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli{};
jeli.BasicLimitInformation.LimitFlags =
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
SetInformationJobObject(
g_hJob,
JobObjectExtendedLimitInformation,
&jeli,
sizeof(jeli));
/* ---------- 管道 ---------- */
SECURITY_ATTRIBUTES sa{ sizeof(sa), nullptr, TRUE };
HANDLE hWrite = nullptr;
CreatePipe(&g_hRead, &hWrite, &sa, 0);
SetHandleInformation(g_hRead, HANDLE_FLAG_INHERIT, 0);
STARTUPINFOW si{};
si.cb = sizeof(si);
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdOutput = hWrite;
si.hStdError = hWrite;
PROCESS_INFORMATION pi{};
std::wstring cmd = lpCmdLine;
if (CreateProcessW(
nullptr,
&cmd[0],
nullptr, nullptr,
TRUE,
CREATE_NO_WINDOW,
nullptr, nullptr,
&si, &pi))
{
g_hProc = pi.hProcess;
AssignProcessToJobObject(g_hJob, pi.hProcess);
CloseHandle(pi.hThread);
}
CloseHandle(hWrite);
CreateThread(nullptr, 0, PipeThread, nullptr, 0, nullptr);
MSG msg;
while (GetMessageW(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return 0;
}