fix #include tray and thread create
This commit is contained in:
+86
-143
@@ -2,164 +2,107 @@
|
|||||||
#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)
|
|
||||||
{
|
|
||||||
switch (type)
|
|
||||||
{
|
|
||||||
case CTRL_C_EVENT:
|
|
||||||
case CTRL_BREAK_EVENT:
|
|
||||||
case CTRL_CLOSE_EVENT:
|
|
||||||
case CTRL_LOGOFF_EVENT:
|
|
||||||
case CTRL_SHUTDOWN_EVENT:
|
|
||||||
RequestShutdown(XorStr("console ctrl"));
|
|
||||||
return TRUE;
|
|
||||||
default:
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
HWND g_hWnd = NULL;
|
|
||||||
HICON g_hTrayIcon = NULL;
|
|
||||||
BOOL g_bIconAdded = FALSE;
|
|
||||||
|
|
||||||
// 自定义消息,托盘通知消息
|
|
||||||
#define WM_TRAYMESSAGE (WM_USER + 1001)
|
|
||||||
|
|
||||||
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
|
|
||||||
{
|
|
||||||
NOTIFYICONDATA nid = { 0 };
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
BOOL CreateHiddenWindow()
|
|
||||||
{
|
|
||||||
WNDCLASSEXA wc = { 0 };
|
|
||||||
wc.cbSize = sizeof(WNDCLASSEXA);
|
|
||||||
wc.lpfnWndProc = WndProc;
|
|
||||||
wc.hInstance = GetModuleHandle(NULL);
|
|
||||||
wc.lpszClassName = "TrayWndClass";
|
|
||||||
RegisterClassExA(&wc);
|
|
||||||
|
|
||||||
g_hWnd = CreateWindowExA(0, wc.lpszClassName, "Tray", 0,
|
|
||||||
CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, NULL, NULL, wc.hInstance, NULL);
|
|
||||||
|
|
||||||
// 不显示窗口(隐藏)
|
|
||||||
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;
|
|
||||||
strcpy_s(nid.szTip, "Esp Tray System");
|
|
||||||
|
|
||||||
BOOL ok = Shell_NotifyIconA(NIM_ADD, &nid);
|
|
||||||
if (ok)
|
|
||||||
g_bIconAdded = TRUE;
|
|
||||||
return ok;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto GuiWindowThread(LPVOID)->DWORD
|
|
||||||
{
|
|
||||||
CreateHiddenWindow();
|
|
||||||
AddTrayIcon();
|
|
||||||
|
|
||||||
MSG msg;
|
|
||||||
while (GetMessage(&msg, NULL, 0, 0)) {
|
|
||||||
TranslateMessage(&msg);
|
|
||||||
DispatchMessage(&msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
auto WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) -> int
|
auto WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) -> int
|
||||||
{
|
{
|
||||||
//AllocConsole();
|
//AllocConsole();
|
||||||
//freopen("CONOUT$", "w+", stdout);
|
//freopen("CONOUT$", "w+", stdout);
|
||||||
|
|
||||||
if (!utils::check_mutex())
|
if (!utils::check_mutex())
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
auto WINAPI CtrlHandler = [](DWORD type)->BOOL
|
||||||
|
{
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case CTRL_C_EVENT:
|
||||||
|
case CTRL_BREAK_EVENT:
|
||||||
|
case CTRL_CLOSE_EVENT:
|
||||||
|
case CTRL_LOGOFF_EVENT:
|
||||||
|
case CTRL_SHUTDOWN_EVENT:
|
||||||
|
RequestShutdown(XorStr("console ctrl"));
|
||||||
|
return TRUE;
|
||||||
|
default:
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
SetConsoleCtrlHandler(CtrlHandler, TRUE);
|
SetConsoleCtrlHandler(CtrlHandler, TRUE);
|
||||||
|
|
||||||
CreateThread(NULL, 0, GuiWindowThread, NULL, 0, NULL);
|
// 先启动服务端,立即创建命名管道
|
||||||
|
HANDLE server_thread = CreateThread(
|
||||||
|
nullptr,
|
||||||
|
0,
|
||||||
|
[](LPVOID) -> DWORD
|
||||||
|
{
|
||||||
|
StartServer();
|
||||||
|
return 0;
|
||||||
|
},
|
||||||
|
nullptr,
|
||||||
|
0,
|
||||||
|
nullptr);
|
||||||
|
|
||||||
Gui::CreateGuiWindow();
|
if (!server_thread)
|
||||||
|
return 0;
|
||||||
|
|
||||||
StartServer();
|
CloseHandle(server_thread);
|
||||||
|
|
||||||
|
|
||||||
|
HANDLE esp_gui_thread = CreateThread(
|
||||||
|
nullptr,
|
||||||
|
0,
|
||||||
|
[](LPVOID) -> DWORD
|
||||||
|
{
|
||||||
|
Gui::CreateGuiWindow();
|
||||||
|
return 0;
|
||||||
|
},
|
||||||
|
nullptr,
|
||||||
|
0,
|
||||||
|
nullptr);
|
||||||
|
if (!esp_gui_thread)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
|
||||||
|
CloseHandle(esp_gui_thread);
|
||||||
|
|
||||||
|
|
||||||
|
// GUI 单独线程
|
||||||
|
auto tray_icon = CreateThread(
|
||||||
|
nullptr,
|
||||||
|
0,
|
||||||
|
[](LPVOID) -> DWORD
|
||||||
|
{
|
||||||
|
CreateHiddenWindow();
|
||||||
|
AddTrayIcon();
|
||||||
|
|
||||||
|
MSG msg;
|
||||||
|
while (GetMessage(&msg, NULL, 0, 0)) {
|
||||||
|
TranslateMessage(&msg);
|
||||||
|
DispatchMessage(&msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
},
|
||||||
|
nullptr,
|
||||||
|
0,
|
||||||
|
nullptr);
|
||||||
|
|
||||||
|
|
||||||
|
if (!tray_icon)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
CloseHandle(tray_icon);
|
||||||
|
|
||||||
|
//获取系统版本和子版本信息
|
||||||
|
|
||||||
|
//WINDOWS_VERSION win_version{ 0 };
|
||||||
|
//
|
||||||
|
//utils::GetWindowsVersion(&win_version);
|
||||||
|
|
||||||
MSG msg;
|
MSG msg;
|
||||||
while (GetMessage(&msg, NULL, 0, 0)) {
|
while (GetMessage(&msg, NULL, 0, 0)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user