| 1 | #include "stdafx.h" |
|---|
| 2 | #include ".\trayicon.h" |
|---|
| 3 | #include "resource.h" |
|---|
| 4 | #include <strEx.h> |
|---|
| 5 | #include <ShellAPI.h> |
|---|
| 6 | |
|---|
| 7 | namespace TrayIcon |
|---|
| 8 | { |
|---|
| 9 | HWND ghDlgWnd = NULL; |
|---|
| 10 | HANDLE ghMutex; |
|---|
| 11 | std::string defaultCommand; |
|---|
| 12 | } |
|---|
| 13 | |
|---|
| 14 | BOOL CALLBACK TrayIcon::InjectDialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam) { |
|---|
| 15 | switch (uMsg) |
|---|
| 16 | { |
|---|
| 17 | case WM_INITDIALOG: |
|---|
| 18 | SetDlgItemText(hwndDlg, IDC_COMMAND, TrayIcon::defaultCommand.c_str()); |
|---|
| 19 | case WM_COMMAND: |
|---|
| 20 | switch (LOWORD(wParam)) |
|---|
| 21 | { |
|---|
| 22 | case IDOK: |
|---|
| 23 | { |
|---|
| 24 | char *c=new char[1024]; |
|---|
| 25 | if (GetDlgItemText(hwndDlg, IDC_COMMAND, c, 1023)) |
|---|
| 26 | TrayIcon::defaultCommand = c; |
|---|
| 27 | delete [] c; |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | case IDCANCEL: |
|---|
| 31 | EndDialog(hwndDlg, wParam); |
|---|
| 32 | return TRUE; |
|---|
| 33 | } |
|---|
| 34 | } |
|---|
| 35 | return FALSE; |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | INT_PTR CALLBACK TrayIcon::DialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam) |
|---|
| 39 | { |
|---|
| 40 | DWORD tmp = 0; |
|---|
| 41 | switch (uMsg) |
|---|
| 42 | { |
|---|
| 43 | case WM_INITDIALOG: |
|---|
| 44 | addIcon(hwndDlg); |
|---|
| 45 | break; |
|---|
| 46 | |
|---|
| 47 | case WM_ICON_NOTIFY: |
|---|
| 48 | if (lParam==WM_RBUTTONDOWN) { |
|---|
| 49 | HMENU hMenu = LoadMenu(NSCModuleWrapper::getModule(),MAKEINTRESOURCE(IDR_POPUP)); |
|---|
| 50 | HMENU hSubMenu = GetSubMenu(hMenu, 0); |
|---|
| 51 | const RECT r = {0, 0, 0, 0}; |
|---|
| 52 | POINT pt; |
|---|
| 53 | GetCursorPos(&pt); |
|---|
| 54 | SetForegroundWindow(ghDlgWnd); |
|---|
| 55 | int cmd = TrackPopupMenu(hSubMenu, TPM_LEFTALIGN|TPM_RETURNCMD, pt.x, pt.y, 0, ghDlgWnd, &r); |
|---|
| 56 | DestroyMenu(hMenu); |
|---|
| 57 | switch (cmd) { |
|---|
| 58 | case ID_POPUP_STOPSERVICE: |
|---|
| 59 | NSCModuleHelper::StopService(); |
|---|
| 60 | break; |
|---|
| 61 | case ID_POPUP_INJECTCOMMAND: |
|---|
| 62 | if (TrayIcon::defaultCommand.empty()) |
|---|
| 63 | TrayIcon::defaultCommand = NSCModuleHelper::getSettingsString("systray", "defaultCommand", ""); |
|---|
| 64 | if (DialogBox(NSCModuleWrapper::getModule(),MAKEINTRESOURCE(IDD_INJECTDIALOG),NULL,InjectDialogProc) == IDOK) { |
|---|
| 65 | NSCModuleHelper::InjectCommand(TrayIcon::defaultCommand); |
|---|
| 66 | } |
|---|
| 67 | break; |
|---|
| 68 | case ID_POPUP_SHOWLOG: |
|---|
| 69 | if ((tmp = (INT)ShellExecute(ghDlgWnd, "open", (NSCModuleHelper::getBasePath() + NSCModuleHelper::getSettingsString("log", "file", "")).c_str(), NULL, NULL, SW_SHOWNORMAL))<=32) { |
|---|
| 70 | NSC_LOG_ERROR("ShellExecute failed : " + strEx::itos((INT)tmp)); |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | return TRUE; |
|---|
| 74 | } |
|---|
| 75 | break; |
|---|
| 76 | } |
|---|
| 77 | return FALSE; |
|---|
| 78 | } |
|---|
| 79 | void TrayIcon::addIcon(HWND hWnd) { |
|---|
| 80 | assert(NSCModuleWrapper::getModule() != NULL); |
|---|
| 81 | assert(hWnd != NULL); |
|---|
| 82 | |
|---|
| 83 | NOTIFYICONDATA ndata; |
|---|
| 84 | ndata.cbSize=sizeof(NOTIFYICONDATA); |
|---|
| 85 | ndata.hWnd=hWnd; |
|---|
| 86 | ndata.uID=2000; |
|---|
| 87 | ndata.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP; |
|---|
| 88 | ndata.uCallbackMessage=WM_ICON_NOTIFY; |
|---|
| 89 | ndata.hIcon=::LoadIcon(NSCModuleWrapper::getModule(),MAKEINTRESOURCE(IDI_STANDBY)); |
|---|
| 90 | strncpy(ndata.szTip,(NSCModuleHelper::getApplicationName() + " - " + NSCModuleHelper::getApplicationVersionString()).c_str(), 63); |
|---|
| 91 | Shell_NotifyIcon(NIM_ADD,&ndata); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | void TrayIcon::removeIcon(void) { |
|---|
| 95 | NOTIFYICONDATA ndata; |
|---|
| 96 | ndata.hWnd=ghDlgWnd; |
|---|
| 97 | ndata.uID=2000; |
|---|
| 98 | Shell_NotifyIcon(NIM_DELETE,&ndata); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | void TrayIcon::createDialog(void) { |
|---|
| 102 | ghMutex = ::CreateMutex(NULL, TRUE, NULL); |
|---|
| 103 | if (!ghMutex) |
|---|
| 104 | throw std::string("Could not create mutex."); |
|---|
| 105 | |
|---|
| 106 | ghDlgWnd = ::CreateDialog(NSCModuleWrapper::getModule(),MAKEINTRESOURCE(IDD_NSTRAYDLG),NULL,DialogProc); |
|---|
| 107 | |
|---|
| 108 | MSG Msg; |
|---|
| 109 | while(::GetMessage(&Msg, ghDlgWnd, 0, 0)) |
|---|
| 110 | { |
|---|
| 111 | if (Msg.message == WM_MY_CLOSE) |
|---|
| 112 | break; |
|---|
| 113 | if (!::IsWindow(ghDlgWnd) || !::IsDialogMessage(ghDlgWnd, &Msg)) { |
|---|
| 114 | ::TranslateMessage(&Msg); |
|---|
| 115 | ::DispatchMessage(&Msg); |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | removeIcon(); |
|---|
| 119 | |
|---|
| 120 | ::DestroyWindow(ghDlgWnd); |
|---|
| 121 | ::ReleaseMutex(ghMutex); |
|---|
| 122 | } |
|---|
| 123 | void TrayIcon::destroyDialog(void) { |
|---|
| 124 | ::PostMessage(ghDlgWnd, WM_MY_CLOSE, NULL, NULL); |
|---|
| 125 | } |
|---|
| 126 | bool TrayIcon::waitForTermination(DWORD timeout /* = 5000L */) { |
|---|
| 127 | DWORD dwWaitResult = WaitForSingleObject(ghMutex, timeout); |
|---|
| 128 | switch (dwWaitResult) { |
|---|
| 129 | // The thread got mutex ownership. |
|---|
| 130 | case WAIT_OBJECT_0: |
|---|
| 131 | ReleaseMutex(ghMutex); |
|---|
| 132 | CloseHandle(ghMutex); |
|---|
| 133 | return true; |
|---|
| 134 | // Cannot get mutex ownership due to time-out. |
|---|
| 135 | case WAIT_TIMEOUT: |
|---|
| 136 | return false; |
|---|
| 137 | |
|---|
| 138 | // Got ownership of the abandoned mutex object. |
|---|
| 139 | case WAIT_ABANDONED: |
|---|
| 140 | return false; |
|---|
| 141 | } |
|---|
| 142 | return false; |
|---|
| 143 | } |
|---|