40 |
nid.uCallbackMessage = WM_TRAY_NOTIFY; |
nid.uCallbackMessage = WM_TRAY_NOTIFY; |
41 |
strcpy( nid.szTip, szAppName ); |
strcpy( nid.szTip, szAppName ); |
42 |
nid.hIcon = ::LoadIcon( hAppInstance, MAKEINTRESOURCE( IDI_TRAY ) ); |
nid.hIcon = ::LoadIcon( hAppInstance, MAKEINTRESOURCE( IDI_TRAY ) ); |
43 |
|
|
44 |
if ( Shell_NotifyIcon( NIM_ADD, &nid ) != TRUE ) { |
if ( Shell_NotifyIcon( NIM_ADD, &nid ) != TRUE ) { |
45 |
Message( "Unable to create tray icon." ); |
Message( "Unable to create tray icon." ); |
46 |
return false; |
return false; |
47 |
} |
} |
48 |
|
|
49 |
return true; |
return true; |
50 |
} |
} |
51 |
|
|
58 |
Message( "Unable to remove tray icon." ); |
Message( "Unable to remove tray icon." ); |
59 |
return false; |
return false; |
60 |
} |
} |
61 |
|
|
62 |
return true; |
return true; |
63 |
|
|
64 |
} |
} |
65 |
|
|
66 |
// |
// |
74 |
if ( wID == IDOK ) |
if ( wID == IDOK ) |
75 |
DestroyWindow( hwndDlg ); |
DestroyWindow( hwndDlg ); |
76 |
} |
} |
77 |
|
|
78 |
return 0; |
return 0; |
79 |
} |
} |
80 |
|
|
93 |
Message( "Unable to load menu ressource." ); |
Message( "Unable to load menu ressource." ); |
94 |
return ; |
return ; |
95 |
} |
} |
96 |
|
|
97 |
HMENU hSubMenu = GetSubMenu( hMenu, 0 ); |
HMENU hSubMenu = GetSubMenu( hMenu, 0 ); |
98 |
if ( hSubMenu == NULL ) { |
if ( hSubMenu == NULL ) { |
99 |
Message( "Unable to find popup mennu." ); |
Message( "Unable to find popup mennu." ); |
100 |
return ; |
return ; |
101 |
} |
} |
102 |
|
|
103 |
// get the cursor position |
// get the cursor position |
104 |
POINT pt; |
POINT pt; |
105 |
GetCursorPos( &pt ); |
GetCursorPos( &pt ); |
106 |
|
|
107 |
SetForegroundWindow( ghWnd ); |
SetForegroundWindow( ghWnd ); |
108 |
int cmd = TrackPopupMenu( hSubMenu, |
int cmd = TrackPopupMenu( hSubMenu, |
109 |
TPM_RETURNCMD | TPM_LEFTALIGN | TPM_RIGHTBUTTON, |
TPM_RETURNCMD | TPM_LEFTALIGN | TPM_RIGHTBUTTON, |
110 |
pt.x, pt.y, 0, ghWnd, NULL ); |
pt.x, pt.y, 0, ghWnd, NULL ); |
111 |
DeleteObject( hMenu ); |
DeleteObject( hMenu ); |
112 |
|
|
113 |
switch ( cmd ) { |
switch ( cmd ) { |
114 |
case ID_WMEXIT: { |
case ID_WMEXIT: { |
115 |
PostQuitMessage( 0 ); |
PostQuitMessage( 0 ); |
116 |
break; |
break; |
117 |
} |
} |
118 |
case ID_WMABOUT: { |
case ID_WMABOUT: { |
119 |
AboutDlg(); |
AboutDlg(); |
120 |
break; |
break; |
121 |
} |
} |
128 |
LONG WINAPI MainWndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) |
LONG WINAPI MainWndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) |
129 |
{ |
{ |
130 |
switch ( uMsg ) { |
switch ( uMsg ) { |
131 |
case WM_DESTROY: { |
case WM_DESTROY: { |
132 |
PostQuitMessage( 0 ); |
PostQuitMessage( 0 ); |
133 |
return 0; |
return 0; |
134 |
} |
} |
135 |
case WM_TRAY_NOTIFY: { |
case WM_TRAY_NOTIFY: { |
136 |
if ( lParam == WM_RBUTTONDOWN ) |
if ( lParam == WM_RBUTTONDOWN ) |
137 |
DoContextMenu(); |
DoContextMenu(); |
138 |
return 0; |
return 0; |
139 |
} |
} |
140 |
} |
} |
141 |
|
|
142 |
return DefWindowProc( hWnd, uMsg, wParam, lParam ); |
return DefWindowProc( hWnd, uMsg, wParam, lParam ); |
143 |
} |
} |
144 |
|
|
159 |
wndclass.hbrBackground = ( HBRUSH ) ( COLOR_WINDOW + 1 ); |
wndclass.hbrBackground = ( HBRUSH ) ( COLOR_WINDOW + 1 ); |
160 |
wndclass.lpszMenuName = NULL; |
wndclass.lpszMenuName = NULL; |
161 |
wndclass.lpszClassName = szAppName; |
wndclass.lpszClassName = szAppName; |
162 |
|
|
163 |
if ( !RegisterClass( &wndclass ) ) { |
if ( !RegisterClass( &wndclass ) ) { |
164 |
Message( "Unable to register the window class." ); |
Message( "Unable to register the window class." ); |
165 |
return false; |
return false; |
166 |
} |
} |
167 |
|
|
168 |
// create the frame |
// create the frame |
169 |
ghWnd = CreateWindow( szAppName, szAppName, |
ghWnd = CreateWindow( szAppName, szAppName, |
170 |
WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | |
WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | |
171 |
WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, 640, |
WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, 640, |
172 |
480, NULL, NULL, hAppInstance, NULL ); |
480, NULL, NULL, hAppInstance, NULL ); |
173 |
|
|
174 |
// make sure window was created |
// make sure window was created |
175 |
if ( !ghWnd ) { |
if ( !ghWnd ) { |
176 |
Message( "Unable to create the window." ); |
Message( "Unable to create the window." ); |
177 |
return false; |
return false; |
178 |
} |
} |
179 |
|
|
180 |
return true; |
return true; |
181 |
} |
} |
182 |
|
|
191 |
( "Application not installed correctly: Unable to init hookdll.dll." ); |
( "Application not installed correctly: Unable to init hookdll.dll." ); |
192 |
return false; |
return false; |
193 |
} |
} |
194 |
|
|
195 |
// check number of instances |
// check number of instances |
196 |
if ( WTSWinClipper::GetInstanceCount() == 1 ) { |
if ( WTSWinClipper::GetInstanceCount() == 1 ) { |
197 |
// hook in |
// hook in |
213 |
if ( !Init( lpCmdLine ) ) { |
if ( !Init( lpCmdLine ) ) { |
214 |
return 0; |
return 0; |
215 |
} |
} |
216 |
|
|
217 |
// if we have been specified an app to launch, we will wait until the app has closed and use that for |
// if we have been specified an app to launch, we will wait until the app has closed and use that for |
218 |
// our cue to exit |
// our cue to exit |
219 |
if ( strlen( lpCmdLine ) > 0 ) { |
if ( strlen( lpCmdLine ) > 0 ) { |
220 |
// Because we do not have a explorer.exe we need to make this application the replacement |
// Because we do not have a explorer.exe we need to make this application the replacement |
221 |
// shell. We do this by calling SystemParametersInfo. If we don't do this, we won't get the WH_SHELL notifications. |
// shell. We do this by calling SystemParametersInfo. If we don't do this, we won't get the WH_SHELL notifications. |
222 |
|
|
223 |
// From MSDN: |
// From MSDN: |
224 |
// Note that custom shell applications do not receive WH_SHELL messages. Therefore, any application that |
// Note that custom shell applications do not receive WH_SHELL messages. Therefore, any application that |
225 |
// registers itself as the default shell must call the SystemParametersInfo function with SPI_SETMINIMIZEDMETRICS |
// registers itself as the default shell must call the SystemParametersInfo function with SPI_SETMINIMIZEDMETRICS |
226 |
// before it (or any other application) can receive WH_SHELL messages. |
// before it (or any other application) can receive WH_SHELL messages. |
227 |
|
|
228 |
MINIMIZEDMETRICS mmm; |
MINIMIZEDMETRICS mmm; |
229 |
mmm.cbSize = sizeof( MINIMIZEDMETRICS ); |
mmm.cbSize = sizeof( MINIMIZEDMETRICS ); |
230 |
SystemParametersInfo( SPI_SETMINIMIZEDMETRICS, |
SystemParametersInfo( SPI_SETMINIMIZEDMETRICS, |
231 |
sizeof( MINIMIZEDMETRICS ), &mmm, 0 ); |
sizeof( MINIMIZEDMETRICS ), &mmm, 0 ); |
232 |
|
|
233 |
// We require DragFullWindows |
// We require DragFullWindows |
234 |
SystemParametersInfo( SPI_SETDRAGFULLWINDOWS, TRUE, NULL, 0 ); |
SystemParametersInfo( SPI_SETDRAGFULLWINDOWS, TRUE, NULL, 0 ); |
235 |
|
|
236 |
//set the current directory to that of the requested app .exe location |
//set the current directory to that of the requested app .exe location |
237 |
//tokenise lpCmdLine. first is the exe path. second (if exists) is the current directory to set. |
//tokenise lpCmdLine. first is the exe path. second (if exists) is the current directory to set. |
238 |
//SetCurrentDirectory (); |
//SetCurrentDirectory (); |
239 |
|
|
240 |
//start process specified from command line arg. |
//start process specified from command line arg. |
241 |
PROCESS_INFORMATION procInfo; |
PROCESS_INFORMATION procInfo; |
242 |
STARTUPINFO startupInfo = { |
STARTUPINFO startupInfo = { |
246 |
char attr[] = ""; |
char attr[] = ""; |
247 |
LPTSTR process = lpCmdLine; |
LPTSTR process = lpCmdLine; |
248 |
DWORD dwExitCode; |
DWORD dwExitCode; |
249 |
|
|
250 |
BOOL m_create = |
BOOL m_create = |
251 |
CreateProcess( NULL, process, NULL, NULL, FALSE, 0, NULL, NULL, |
CreateProcess( NULL, process, NULL, NULL, FALSE, 0, NULL, NULL, |
252 |
&startupInfo, &procInfo ); |
&startupInfo, &procInfo ); |
253 |
|
|
254 |
if ( m_create != false ) { |
if ( m_create != false ) { |
255 |
// A loop to watch the process. |
// A loop to watch the process. |
256 |
GetExitCodeProcess( procInfo.hProcess, &dwExitCode ); |
GetExitCodeProcess( procInfo.hProcess, &dwExitCode ); |
257 |
|
|
258 |
while ( dwExitCode == STILL_ACTIVE ) { |
while ( dwExitCode == STILL_ACTIVE ) { |
259 |
GetExitCodeProcess( procInfo.hProcess, &dwExitCode ); |
GetExitCodeProcess( procInfo.hProcess, &dwExitCode ); |
260 |
Sleep( 1000 ); |
Sleep( 1000 ); |
261 |
} |
} |
262 |
|
|
263 |
// Release handles |
// Release handles |
264 |
CloseHandle( procInfo.hProcess ); |
CloseHandle( procInfo.hProcess ); |
265 |
CloseHandle( procInfo.hThread ); |
CloseHandle( procInfo.hThread ); |
266 |
} else { |
} else { |
267 |
// CreateProcess failed. |
// CreateProcess failed. |
268 |
char msg[ 256 ]; |
char msg[ 256 ]; |
269 |
snprintf( msg, sizeof( msg ), "Unable to launch the requested application:\n%s", process ); |
snprintf( msg, sizeof( msg ), "Unable to launch the requested application:\n%s", process ); |
270 |
Message( msg ); |
Message( msg ); |
271 |
} |
} |
272 |
} else |
} else |
273 |
// we are launching without an app, therefore we will show the system tray app and wait for the user to close it |
// we are launching without an app, therefore we will show the system tray app and wait for the user to close it |
274 |
{ |
{ |
275 |
// create a dummy window to receive WM_QUIT message |
// create a dummy window to receive WM_QUIT message |
276 |
InitWindow(); |
InitWindow(); |
277 |
|
|
278 |
// create the tray icon |
// create the tray icon |
279 |
InitTrayIcon(); |
InitTrayIcon(); |
280 |
|
|
281 |
// just get and dispatch messages until we're killed |
// just get and dispatch messages until we're killed |
282 |
MSG msg; |
MSG msg; |
283 |
while ( GetMessage( &msg, 0, 0, 0 ) ) { |
while ( GetMessage( &msg, 0, 0, 0 ) ) { |
284 |
TranslateMessage( &msg ); |
TranslateMessage( &msg ); |
285 |
DispatchMessage( &msg ); |
DispatchMessage( &msg ); |
286 |
}; |
}; |
287 |
|
|
288 |
// remove our tray icon |
// remove our tray icon |
289 |
RemoveTrayIcon(); |
RemoveTrayIcon(); |
290 |
} |
} |
291 |
|
|
292 |
|
|
293 |
// remove hook before saying goodbye |
// remove hook before saying goodbye |
294 |
WTSWinClipper::RemoveHooks(); |
WTSWinClipper::RemoveHooks(); |
295 |
|
|
296 |
WTSWinClipper::Done(); |
WTSWinClipper::Done(); |
297 |
|
|
298 |
return 1; |
return 1; |
299 |
} |
} |