@@ -111,11 +111,20 @@ LRESULT CALLBACK TrayWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lPar
111
111
return DefWindowProc (hwnd , uMsg , wParam , lParam );
112
112
}
113
113
114
+ static UINT s_taskbarRestart = 0 ;
115
+ if (s_taskbarRestart == 0 ) {
116
+ s_taskbarRestart = RegisterWindowMessage (TEXT ("TaskbarCreated" ));
117
+ }
118
+ if (uMsg == s_taskbarRestart ) {
119
+ Shell_NotifyIconW (NIM_ADD , & tray -> nid );
120
+ Shell_NotifyIconW (NIM_SETVERSION , & tray -> nid );
121
+ }
122
+
114
123
switch (uMsg ) {
115
124
case WM_TRAYICON :
116
125
if (LOWORD (lParam ) == WM_CONTEXTMENU || LOWORD (lParam ) == WM_LBUTTONUP ) {
117
126
SetForegroundWindow (hwnd );
118
-
127
+
119
128
if (tray -> menu ) {
120
129
TrackPopupMenu (tray -> menu -> hMenu , TPM_BOTTOMALIGN | TPM_RIGHTALIGN , GET_X_LPARAM (wParam ), GET_Y_LPARAM (wParam ), 0 , hwnd , NULL );
121
130
}
@@ -216,6 +225,51 @@ void SDL_UpdateTrays(void)
216
225
{
217
226
}
218
227
228
+ static void SDL_PropTrayCleanupCb (void * userdata , void * cls ) {
229
+ WNDCLASSEX wcex ;
230
+
231
+ wcex .hIcon = NULL ;
232
+ wcex .hIconSm = NULL ;
233
+ HINSTANCE h = GetModuleHandle (NULL );
234
+ if (GetClassInfoEx (h , cls , & wcex )) {
235
+ UnregisterClass (cls , h );
236
+ }
237
+ }
238
+
239
+ static bool SDL_RegisterTrayClass (LPCWSTR className )
240
+ {
241
+ SDL_PropertiesID g = SDL_GetGlobalProperties ();
242
+ if (!g ) {
243
+ return false;
244
+ }
245
+ void * p = SDL_GetPointerProperty (g , SDL_PROP_TRAY_CLEANUP , NULL );
246
+ if (p ) {
247
+ return true;
248
+ }
249
+
250
+ WNDCLASSEX wcex ;
251
+
252
+ wcex .cbSize = sizeof (WNDCLASSEX );
253
+ wcex .hCursor = NULL ;
254
+ wcex .hIcon = NULL ;
255
+ wcex .hIconSm = NULL ;
256
+ wcex .lpszMenuName = NULL ;
257
+ wcex .lpszClassName = className ;
258
+ wcex .style = 0 ;
259
+ wcex .hbrBackground = NULL ;
260
+ wcex .lpfnWndProc = TrayWindowProc ;
261
+ wcex .hInstance = NULL ;
262
+ wcex .cbClsExtra = 0 ;
263
+ wcex .cbWndExtra = 0 ;
264
+
265
+ if (!RegisterClassEx (& wcex )) {
266
+ return SDL_SetError ("Couldn't register tray class" );
267
+ }
268
+
269
+ SDL_SetPointerPropertyWithCleanup (g , SDL_PROP_TRAY_CLEANUP , (void * )wcex .lpszClassName , SDL_PropTrayCleanupCb , NULL );
270
+ return true;
271
+ }
272
+
219
273
SDL_Tray * SDL_CreateTray (SDL_Surface * icon , const char * tooltip )
220
274
{
221
275
if (!SDL_IsMainThread ()) {
@@ -230,8 +284,12 @@ SDL_Tray *SDL_CreateTray(SDL_Surface *icon, const char *tooltip)
230
284
}
231
285
232
286
tray -> menu = NULL ;
233
- tray -> hwnd = CreateWindowEx (0 , TEXT ("Message" ), NULL , 0 , 0 , 0 , 0 , 0 , HWND_MESSAGE , NULL , NULL , NULL );
234
- SetWindowLongPtr (tray -> hwnd , GWLP_WNDPROC , (LONG_PTR ) TrayWindowProc );
287
+ if (!SDL_RegisterTrayClass (TEXT ("SDL_TRAY" ))) {
288
+ SDL_SetError ("Failed to register SDL_TRAY window class" );
289
+ return NULL ;
290
+ }
291
+ tray -> hwnd = CreateWindowEx (0 , TEXT ("SDL_TRAY" ), NULL , WS_OVERLAPPEDWINDOW ,
292
+ CW_USEDEFAULT , 0 , CW_USEDEFAULT , 0 , NULL , NULL , NULL , NULL );
235
293
236
294
WIN_UpdateDarkModeForHWND (tray -> hwnd );
237
295
0 commit comments