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
| bool isDisplay = false; HMENU hmenu; NOTIFYICONDATA nid;
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { POINT pt; int opt;
switch (message) { case WM_CREATE: nid.cbSize = sizeof(NOTIFYICONDATA); nid.hWnd = hwnd; nid.uID = 0; nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; nid.uCallbackMessage = WM_USER; nid.hIcon = LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(IDI_ICON2)); wcscpy_s(nid.szTip, make_text(0)); Shell_NotifyIcon(NIM_ADD, &nid); hmenu = CreatePopupMenu(); AppendMenu(hmenu, MF_STRING, TRAY_MENU_EXIT, _T("退出")); break;
case WM_USER: if (lParam == WM_LBUTTONDOWN) { if (isDisplay) { ShowWindow(GetConsoleWindow(), SW_HIDE); isDisplay = false; } else { ShowWindow(GetConsoleWindow(), SW_SHOW); isDisplay = true; } } if (lParam == WM_RBUTTONDOWN) { GetCursorPos(&pt); SetForegroundWindow(hwnd); opt = TrackPopupMenu(hmenu, TPM_RETURNCMD, pt.x, pt.y, NULL, hwnd, NULL); if (opt == TRAY_MENU_EXIT) { auto ret = MessageBox(hwnd, _T("确认退出?"), FORM_NAME, MB_OKCANCEL | MB_ICONQUESTION); if (IDOK == ret) { SendMessage(hwnd, WM_CLOSE, wParam, lParam); } }
if (opt == 0) PostMessage(hwnd, WM_LBUTTONDOWN, NULL, NULL); } break;
case WM_DESTROY: Shell_NotifyIcon(NIM_DELETE, &nid); PostQuitMessage(0); break; } return DefWindowProc(hwnd, message, wParam, lParam); }
BOOL WINAPI ConsoleHandler(DWORD CEvent) { switch (CEvent) { case CTRL_BREAK_EVENT: Shell_NotifyIcon(NIM_DELETE, &nid); break; case CTRL_CLOSE_EVENT: Shell_NotifyIcon(NIM_DELETE, &nid); break; case CTRL_SHUTDOWN_EVENT: Shell_NotifyIcon(NIM_DELETE, &nid); break; } return TRUE; }
void CreateTray() { MSG msg; WNDCLASS wndclass; HWND handle = FindWindow(NULL, CONSOLE_NAME); if (handle != NULL) { MessageBox(NULL, TEXT("服务正在运行中!"), FORM_NAME, MB_ICONERROR); return; } wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = 0; wndclass.hIcon = LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(IDI_ICON1)); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = FORM_NAME;
if (!RegisterClass(&wndclass)) { MessageBox(NULL, TEXT("This program requires Windows NT!"), FORM_NAME, MB_ICONERROR); return; }
CreateWindowEx(WS_EX_TOOLWINDOW, FORM_NAME, FORM_NAME, WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, 0, NULL);
HWND console = GetConsoleWindow(); SetConsoleTitle(CONSOLE_NAME); SetConsoleCtrlHandler(ConsoleHandler, TRUE); ShowWindow(console, SW_HIDE); UpdateWindow(console);
while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } }
|