source: trunk/testapp/gui/systray/StealthDialog.cpp@ 21588

Last change on this file since 21588 was 21588, checked in by dmik, 15 years ago

testapp: Added systray testcase (taken from http://www.codeproject.com/KB/shell/StealthDialog.aspx and slightly modified to build with Odin).

  • Property svn:eol-style set to native
File size: 7.2 KB
Line 
1// Slightly modified to build out of the original package available at
2// http://www.codeproject.com/KB/shell/StealthDialog.aspx
3
4// Win32 Dialog.cpp : Defines the entry point for the application.
5//
6
7#include "stdafx.h"
8#include "resource.h"
9
10#define TRAYICONID 1// ID number for the Notify Icon
11#define SWM_TRAYMSG WM_APP// the message ID sent to our window
12
13#define SWM_SHOW WM_APP + 1// show the window
14#define SWM_HIDE WM_APP + 2// hide the window
15#define SWM_EXIT WM_APP + 3// close the window
16
17#ifndef _MSC_VER
18
19#include <odinlx.h>
20
21int APIENTRY _tWinMain(HINSTANCE hInstance,
22 HINSTANCE hPrevInstance,
23 LPTSTR lpCmdLine,
24 int nCmdShow);
25
26//Win32 resource table (produced by wrc)
27extern DWORD Resource_PEResTab;
28
29int main(int argc, char **argv)
30{
31 EnableSEH();
32 RegisterLxExe((WINMAIN)_tWinMain, (PVOID)&Resource_PEResTab);
33}
34
35#endif
36
37// Global Variables:
38HINSTANCE hInst; // current instance
39NOTIFYICONDATA niData; // notify icon data
40
41// Forward declarations of functions included in this code module:
42BOOL InitInstance(HINSTANCE, int);
43BOOL OnInitDialog(HWND hWnd);
44void ShowContextMenu(HWND hWnd);
45ULONGLONG GetDllVersion(LPCTSTR lpszDllName);
46
47INT_PTR CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM);
48LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
49
50int APIENTRY _tWinMain(HINSTANCE hInstance,
51 HINSTANCE hPrevInstance,
52 LPTSTR lpCmdLine,
53 int nCmdShow)
54{
55 MSG msg;
56 HACCEL hAccelTable;
57
58 // Perform application initialization:
59 if (!InitInstance (hInstance, nCmdShow)) return FALSE;
60 hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_STEALTHDIALOG);
61
62 // Main message loop:
63 while (GetMessage(&msg, NULL, 0, 0))
64 {
65 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)||
66 !IsDialogMessage(msg.hwnd,&msg) )
67 {
68 TranslateMessage(&msg);
69 DispatchMessage(&msg);
70 }
71 }
72 return (int) msg.wParam;
73}
74
75// Initialize the window and tray icon
76BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
77{
78 // prepare for XP style controls
79 InitCommonControls();
80
81 // store instance handle and create dialog
82 hInst = hInstance;
83 HWND hWnd = CreateDialog( hInstance, MAKEINTRESOURCE(IDD_DLG_DIALOG),
84 NULL, (DLGPROC)DlgProc );
85 if (!hWnd) return FALSE;
86
87 // Fill the NOTIFYICONDATA structure and call Shell_NotifyIcon
88
89 // zero the structure - note: Some Windows funtions require this but
90 // I can't be bothered which ones do and
91 // which ones don't.
92 ZeroMemory(&niData,sizeof(NOTIFYICONDATA));
93
94#ifndef __WIN32OS2__
95 // get Shell32 version number and set the size of the structure
96 // note: the MSDN documentation about this is a little
97 // dubious and I'm not at all sure if the method
98 // bellow is correct
99 ULONGLONG ullVersion = GetDllVersion(_T("Shell32.dll"));
100 if(ullVersion >= MAKEDLLVERULL(5, 0,0,0))
101 niData.cbSize = sizeof(NOTIFYICONDATA);
102 else niData.cbSize = NOTIFYICONDATA_V2_SIZE;
103#else
104 niData.cbSize = sizeof(NOTIFYICONDATA);
105#endif
106
107 // the ID number can be anything you choose
108 niData.uID = TRAYICONID;
109
110 // state which structure members are valid
111 niData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
112
113 // load the icon
114 niData.hIcon = (HICON)LoadImage(hInstance,MAKEINTRESOURCE(IDI_STEALTHDLG),
115 IMAGE_ICON, GetSystemMetrics(SM_CXSMICON),GetSystemMetrics(SM_CYSMICON),
116 LR_DEFAULTCOLOR);
117
118 // the window to send messages to and the message to send
119 // note: the message value should be in the
120 // range of WM_APP through 0xBFFF
121 niData.hWnd = hWnd;
122 niData.uCallbackMessage = SWM_TRAYMSG;
123
124 // tooltip message
125 lstrcpyn(niData.szTip, _T("Time flies like an arrow but\n fruit flies like a banana!"), sizeof(niData.szTip)/sizeof(TCHAR));
126
127 Shell_NotifyIcon(NIM_ADD,&niData);
128
129 // free icon handle
130 if(niData.hIcon && DestroyIcon(niData.hIcon))
131 niData.hIcon = NULL;
132
133 // call ShowWindow here to make the dialog initially visible
134
135 return TRUE;
136}
137
138BOOL OnInitDialog(HWND hWnd)
139{
140 HMENU hMenu = GetSystemMenu(hWnd,FALSE);
141 if (hMenu)
142 {
143 AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
144 AppendMenu(hMenu, MF_STRING, IDM_ABOUT, _T("About"));
145 }
146 HICON hIcon = (HICON)LoadImage(hInst,
147 MAKEINTRESOURCE(IDI_STEALTHDLG),
148 IMAGE_ICON, 0,0, LR_SHARED|LR_DEFAULTSIZE);
149printf("### hIcon %x\n", hIcon);
150 SendMessage(hWnd,WM_SETICON,ICON_BIG,(LPARAM)hIcon);
151 SendMessage(hWnd,WM_SETICON,ICON_SMALL,(LPARAM)hIcon);
152 return TRUE;
153}
154
155// Name says it all
156void ShowContextMenu(HWND hWnd)
157{
158 POINT pt;
159 GetCursorPos(&pt);
160 HMENU hMenu = CreatePopupMenu();
161 if(hMenu)
162 {
163 if( IsWindowVisible(hWnd) )
164 InsertMenu(hMenu, (UINT)-1, MF_BYPOSITION, SWM_HIDE, _T("Hide"));
165 else
166 InsertMenu(hMenu, (UINT)-1, MF_BYPOSITION, SWM_SHOW, _T("Show"));
167 InsertMenu(hMenu, (UINT)-1, MF_BYPOSITION, SWM_EXIT, _T("Exit"));
168
169 // note: must set window to the foreground or the
170 // menu won't disappear when it should
171 SetForegroundWindow(hWnd);
172
173 TrackPopupMenu(hMenu, TPM_BOTTOMALIGN,
174 pt.x, pt.y, 0, hWnd, NULL );
175 DestroyMenu(hMenu);
176 }
177}
178
179#ifndef __WIN32OS2__
180// Get dll version number
181ULONGLONG GetDllVersion(LPCTSTR lpszDllName)
182{
183 ULONGLONG ullVersion = 0;
184 HINSTANCE hinstDll;
185 hinstDll = LoadLibrary(lpszDllName);
186 if(hinstDll)
187 {
188 DLLGETVERSIONPROC pDllGetVersion;
189 pDllGetVersion = (DLLGETVERSIONPROC)GetProcAddress(hinstDll, "DllGetVersion");
190 if(pDllGetVersion)
191 {
192 DLLVERSIONINFO dvi;
193 HRESULT hr;
194 ZeroMemory(&dvi, sizeof(dvi));
195 dvi.cbSize = sizeof(dvi);
196 hr = (*pDllGetVersion)(&dvi);
197 if(SUCCEEDED(hr))
198 ullVersion = MAKEDLLVERULL(dvi.dwMajorVersion, dvi.dwMinorVersion,0,0);
199 }
200 FreeLibrary(hinstDll);
201 }
202 return ullVersion;
203}
204#endif
205
206// Message handler for the app
207INT_PTR CALLBACK DlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
208{
209 int wmId, wmEvent;
210
211 switch (message)
212 {
213 case SWM_TRAYMSG:
214 switch(lParam)
215 {
216 case WM_LBUTTONDBLCLK:
217 ShowWindow(hWnd, SW_RESTORE);
218 break;
219 case WM_RBUTTONDOWN:
220 case WM_CONTEXTMENU:
221 ShowContextMenu(hWnd);
222 }
223 break;
224 case WM_SYSCOMMAND:
225 if((wParam & 0xFFF0) == SC_MINIMIZE)
226 {
227 ShowWindow(hWnd, SW_HIDE);
228 return 1;
229 }
230 else if(wParam == IDM_ABOUT)
231 DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
232 break;
233 case WM_COMMAND:
234 wmId = LOWORD(wParam);
235 wmEvent = HIWORD(wParam);
236
237 switch (wmId)
238 {
239 case SWM_SHOW:
240 ShowWindow(hWnd, SW_RESTORE);
241 break;
242 case SWM_HIDE:
243 case IDOK:
244 ShowWindow(hWnd, SW_HIDE);
245 break;
246 case SWM_EXIT:
247 DestroyWindow(hWnd);
248 break;
249 case IDM_ABOUT:
250 DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
251 break;
252 }
253 return 1;
254 case WM_INITDIALOG:
255 return OnInitDialog(hWnd);
256 case WM_CLOSE:
257 DestroyWindow(hWnd);
258 break;
259 case WM_DESTROY:
260 niData.uFlags = 0;
261 Shell_NotifyIcon(NIM_DELETE,&niData);
262 PostQuitMessage(0);
263 break;
264 }
265 return 0;
266}
267
268// Message handler for about box.
269LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
270{
271 switch (message)
272 {
273 case WM_INITDIALOG:
274 return TRUE;
275
276 case WM_COMMAND:
277 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
278 {
279 EndDialog(hDlg, LOWORD(wParam));
280 return TRUE;
281 }
282 break;
283 }
284 return FALSE;
285}
Note: See TracBrowser for help on using the repository browser.