source: nscp/trunk/modules/SysTray/TrayIcon.cpp @ edbbfbb

Last change on this file since edbbfbb was 237da21, checked in by Michael Medin <michael@…>, 8 years ago

Alot of fixes and some changes (se changelog for details)

  • Property mode set to 100644
File size: 3.4 KB
Line 
1#include "stdafx.h"
2#include ".\trayicon.h"
3#include "resource.h"
4#include <strEx.h>
5#include <ShellAPI.h>
6
7unsigned IconWidget_::threadProc(LPVOID lpParameter)
8{
9        createDialog();
10        return 0;
11}
12
13
14void IconWidget_::createDialog(void) {
15        hDlgWnd = ::CreateDialog(NSCModuleWrapper::getModule(),MAKEINTRESOURCE(IDD_NSTRAYDLG),NULL,TrayIcon::DialogProc);
16
17        MSG Msg;
18        while(::GetMessage(&Msg, hDlgWnd, 0, 0))
19        {
20                if (Msg.message == WM_MY_CLOSE)
21                        break;
22                if (!::IsWindow(hDlgWnd) || !::IsDialogMessage(hDlgWnd, &Msg)) {
23                        ::TranslateMessage(&Msg);
24                        ::DispatchMessage(&Msg);
25                }
26        }
27        TrayIcon::removeIcon(hDlgWnd);
28
29        ::DestroyWindow(hDlgWnd);
30}
31void IconWidget_::exitThread(void) {
32        ::PostMessage(hDlgWnd, WM_MY_CLOSE, NULL, NULL);
33
34}
35
36namespace TrayIcon
37{
38        std::string defaultCommand;
39}
40
41BOOL CALLBACK TrayIcon::InjectDialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam) {
42        switch (uMsg)
43        {
44        case WM_INITDIALOG:
45                SetDlgItemText(hwndDlg, IDC_COMMAND, TrayIcon::defaultCommand.c_str());
46        case WM_COMMAND:
47                switch (LOWORD(wParam))
48                {
49                case IDOK:
50                        {
51                                char *c=new char[1024];
52                                if (GetDlgItemText(hwndDlg, IDC_COMMAND, c, 1023))
53                                        TrayIcon::defaultCommand = c;
54                                delete [] c;
55                        }
56
57                case IDCANCEL:
58                        EndDialog(hwndDlg, wParam);
59                        return TRUE;
60                }
61        }
62        return FALSE;
63}
64
65INT_PTR CALLBACK TrayIcon::DialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
66{
67        switch (uMsg)
68        {
69        case WM_INITDIALOG:
70                addIcon(hwndDlg);
71                break;
72
73        case WM_ICON_NOTIFY:
74                if (lParam==WM_RBUTTONDOWN) {
75                        HMENU hMenu = LoadMenu(NSCModuleWrapper::getModule(),MAKEINTRESOURCE(IDR_POPUP));
76                        HMENU hSubMenu = GetSubMenu(hMenu, 0);
77                        const RECT r = {0, 0, 0, 0};
78                        POINT pt;
79                        GetCursorPos(&pt);
80                        SetForegroundWindow(hwndDlg);
81                        int cmd = TrackPopupMenu(hSubMenu, TPM_LEFTALIGN|TPM_RETURNCMD, pt.x, pt.y, 0, hwndDlg, &r);
82                        DestroyMenu(hMenu);
83                        switch (cmd) {
84                        case ID_POPUP_STOPSERVICE:
85                                NSCModuleHelper::StopService();
86                                break;
87                        case ID_POPUP_INJECTCOMMAND:
88                                if (TrayIcon::defaultCommand.empty())
89                                        TrayIcon::defaultCommand = NSCModuleHelper::getSettingsString("systray", "defaultCommand", "");
90                                if (DialogBox(NSCModuleWrapper::getModule(),MAKEINTRESOURCE(IDD_INJECTDIALOG),NULL,InjectDialogProc) == IDOK) {
91                                        // @todo NSCModuleHelper::InjectCommand(TrayIcon::defaultCommand);
92                                }
93                                break;
94                        case ID_POPUP_SHOWLOG:
95                                {
96                                        long long err = reinterpret_cast<long long>(ShellExecute(hwndDlg, "open",
97                                                (NSCModuleHelper::getBasePath() + NSCModuleHelper::getSettingsString("log", "file", "")).c_str(),
98                                                NULL, NULL, SW_SHOWNORMAL));
99                                        if (err <=32) {
100                                                        NSC_LOG_ERROR("ShellExecute failed : " + strEx::itos(err));
101                                                }
102                                }
103                        }
104                        return TRUE;
105                }
106                break;
107        }
108        return FALSE;
109}
110void TrayIcon::addIcon(HWND hWnd) {
111        assert(NSCModuleWrapper::getModule() != NULL);
112        assert(hWnd != NULL);
113
114        NOTIFYICONDATA ndata;
115        ndata.cbSize=sizeof(NOTIFYICONDATA);
116        ndata.hWnd=hWnd;
117        ndata.uID=2000;
118        ndata.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP;
119        ndata.uCallbackMessage=WM_ICON_NOTIFY;
120        ndata.hIcon=::LoadIcon(NSCModuleWrapper::getModule(),MAKEINTRESOURCE(IDI_STANDBY));
121        strncpy(ndata.szTip,(NSCModuleHelper::getApplicationName() + " - " + NSCModuleHelper::getApplicationVersionString()).c_str(), 63);
122        Shell_NotifyIcon(NIM_ADD,&ndata);
123}
124
125void TrayIcon::removeIcon(HWND hWnd) {
126        NOTIFYICONDATA ndata;
127        ndata.cbSize=sizeof(NOTIFYICONDATA);
128        ndata.hWnd=hWnd;
129        ndata.uID=2000;
130        Shell_NotifyIcon(NIM_DELETE,&ndata);
131}
Note: See TracBrowser for help on using the repository browser.