fix #include tray and thread create

This commit is contained in:
2026-10-08 09:59:28 +08:00
parent 7881c9a31e
commit 048b6e1513
+56 -113
View File
@@ -2,12 +2,20 @@
#include "server_pipe.h" #include "server_pipe.h"
#include "Gui.h" #include "Gui.h"
#include "XorStr.h" #include "XorStr.h"
#include "resource.h"
#include "utils.h" #include "utils.h"
#include "Tray.h"
BOOL WINAPI CtrlHandler(DWORD type) auto WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) -> int
{
//AllocConsole();
//freopen("CONOUT$", "w+", stdout);
if (!utils::check_mutex())
return 0;
auto WINAPI CtrlHandler = [](DWORD type)->BOOL
{ {
switch (type) switch (type)
{ {
@@ -21,117 +29,53 @@ BOOL WINAPI CtrlHandler(DWORD type)
default: default:
return FALSE; return FALSE;
} }
} };
HWND g_hWnd = NULL;
HICON g_hTrayIcon = NULL;
BOOL g_bIconAdded = FALSE;
// 自定义消息,托盘通知消息 SetConsoleCtrlHandler(CtrlHandler, TRUE);
#define WM_TRAYMESSAGE (WM_USER + 1001)
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) // 先启动服务端,立即创建命名管道
HANDLE server_thread = CreateThread(
nullptr,
0,
[](LPVOID) -> DWORD
{ {
NOTIFYICONDATA nid = { 0 }; StartServer();
switch (msg)
{
case WM_TRAYMESSAGE:
{
// 托盘消息
switch (LOWORD(lParam))
{
case WM_RBUTTONUP:
{
// 右键弹出菜单
POINT pt;
GetCursorPos(&pt);
HMENU hMenu = CreatePopupMenu();
AppendMenuA(hMenu, MF_STRING, 1001, "关于");
AppendMenuA(hMenu, MF_STRING, 1002, "退出程序");
// 弹出菜单
SetForegroundWindow(hWnd);
TrackPopupMenu(hMenu, TPM_BOTTOMALIGN | TPM_LEFTALIGN, pt.x, pt.y, 0, hWnd, NULL);
DestroyMenu(hMenu);
break;
}
}
break;
}
case WM_COMMAND:
{
UINT cmdId = LOWORD(wParam);
if (cmdId == 1001)
{
MessageBoxA(hWnd, "ESP反挂插件\nwww.espgom.com", "", MB_OK);
}
else if (cmdId == 1002)
{
// 退出,先移除托盘图标
nid.cbSize = sizeof(nid);
nid.hWnd = g_hWnd;
Shell_NotifyIcon(NIM_DELETE, &nid);
ExitProcess(0);
}
break;
}
case WM_DESTROY:
nid.cbSize = sizeof(nid);
nid.hWnd = g_hWnd;
Shell_NotifyIcon(NIM_DELETE, &nid);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0; return 0;
} },
nullptr,
0,
nullptr);
BOOL CreateHiddenWindow() if (!server_thread)
return 0;
CloseHandle(server_thread);
HANDLE esp_gui_thread = CreateThread(
nullptr,
0,
[](LPVOID) -> DWORD
{ {
WNDCLASSEXA wc = { 0 }; Gui::CreateGuiWindow();
wc.cbSize = sizeof(WNDCLASSEXA); return 0;
wc.lpfnWndProc = WndProc; },
wc.hInstance = GetModuleHandle(NULL); nullptr,
wc.lpszClassName = "TrayWndClass"; 0,
RegisterClassExA(&wc); nullptr);
if (!esp_gui_thread)
return 0;
g_hWnd = CreateWindowExA(0, wc.lpszClassName, "Tray", 0,
CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, NULL, NULL, wc.hInstance, NULL);
// 不显示窗口(隐藏) CloseHandle(esp_gui_thread);
ShowWindow(g_hWnd, SW_HIDE);
return g_hWnd != NULL;
}
BOOL AddTrayIcon()
{
NOTIFYICONDATAA nid = { 0 };
nid.cbSize = sizeof(NOTIFYICONDATAA);
nid.hWnd = g_hWnd;
nid.uID = 3276147; // 图标唯一ID
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nid.uCallbackMessage = WM_TRAYMESSAGE;
// 图标:可以 LoadIcon/LoadImage,也可以用自带默认图标
// ========== 加载资源图标(托盘建议16x16) ==========
g_hTrayIcon = (HICON)LoadImageA(
GetModuleHandle(NULL),
MAKEINTRESOURCEA(IDI_ICON1),
IMAGE_ICON,
64, 64, // 托盘标准尺寸 16*16
LR_DEFAULTCOLOR
);
nid.hIcon = g_hTrayIcon; // GUI 单独线程
strcpy_s(nid.szTip, "Esp Tray System"); auto tray_icon = CreateThread(
nullptr,
BOOL ok = Shell_NotifyIconA(NIM_ADD, &nid); 0,
if (ok) [](LPVOID) -> DWORD
g_bIconAdded = TRUE;
return ok;
}
auto GuiWindowThread(LPVOID)->DWORD
{ {
CreateHiddenWindow(); CreateHiddenWindow();
AddTrayIcon(); AddTrayIcon();
@@ -143,23 +87,22 @@ auto GuiWindowThread(LPVOID)->DWORD
} }
return 0; return 0;
} },
nullptr,
0,
nullptr);
auto WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) -> int if (!tray_icon)
{
//AllocConsole();
//freopen("CONOUT$", "w+", stdout);
if (!utils::check_mutex())
return 0; return 0;
SetConsoleCtrlHandler(CtrlHandler, TRUE); CloseHandle(tray_icon);
CreateThread(NULL, 0, GuiWindowThread, NULL, 0, NULL); //获取系统版本和子版本信息
Gui::CreateGuiWindow(); //WINDOWS_VERSION win_version{ 0 };
//
StartServer(); //utils::GetWindowsVersion(&win_version);
MSG msg; MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) { while (GetMessage(&msg, NULL, 0, 0)) {