SDL: Reset the window icon when returning from fullscreen mode

From 8fa93d64abcd0bbc02d78c462cbcd27976192078 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 24 Nov 2025 13:28:36 -0800
Subject: [PATCH] Reset the window icon when returning from fullscreen mode

Fixes https://github.com/libsdl-org/SDL/issues/14522
---
 src/video/windows/SDL_windowswindow.c | 20 +++++++++++++++++---
 src/video/windows/SDL_windowswindow.h |  1 +
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/src/video/windows/SDL_windowswindow.c b/src/video/windows/SDL_windowswindow.c
index d8413c4f32b39..7bb17710a75fa 100644
--- a/src/video/windows/SDL_windowswindow.c
+++ b/src/video/windows/SDL_windowswindow.c
@@ -582,7 +582,6 @@ static void CleanupWindowData(SDL_VideoDevice *_this, SDL_Window *window)
     SDL_WindowData *data = window->internal;
 
     if (data) {
-
 #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
         if (data->drop_target) {
             WIN_AcceptDragAndDrop(window, false);
@@ -591,6 +590,9 @@ static void CleanupWindowData(SDL_VideoDevice *_this, SDL_Window *window)
         if (data->keyboard_hook) {
             UnhookWindowsHookEx(data->keyboard_hook);
         }
+        if (data->hicon) {
+            DestroyIcon(data->hicon);
+        }
         ReleaseDC(data->hwnd, data->hdc);
         RemoveProp(data->hwnd, TEXT("SDL_WindowData"));
 #endif
@@ -837,7 +839,8 @@ void WIN_SetWindowTitle(SDL_VideoDevice *_this, SDL_Window *window)
 bool WIN_SetWindowIcon(SDL_VideoDevice *_this, SDL_Window *window, SDL_Surface *icon)
 {
 #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
-    HWND hwnd = window->internal->hwnd;
+    SDL_WindowData *data = window->internal;
+    HWND hwnd = data->hwnd;
     HICON hicon = NULL;
     BYTE *icon_bmp;
     int icon_len, mask_len, row_len, y;
@@ -888,9 +891,14 @@ bool WIN_SetWindowIcon(SDL_VideoDevice *_this, SDL_Window *window, SDL_Surface *
     SDL_small_free(icon_bmp, isstack);
 
     if (!hicon) {
-        result = SDL_SetError("SetWindowIcon() failed, error %08X", (unsigned int)GetLastError());
+        return SDL_SetError("SetWindowIcon() failed, error %08X", (unsigned int)GetLastError());
     }
 
+    if (data->hicon) {
+        DestroyIcon(data->hicon);
+    }
+    data->hicon = hicon;
+
     // Set the icon for the window
     SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hicon);
 
@@ -1364,6 +1372,12 @@ SDL_FullscreenResult WIN_SetWindowFullscreen(SDL_VideoDevice *_this, SDL_Window
         WIN_MaximizeWindow(_this, window);
     }
 
+    if (!fullscreen && data && data->hicon) {
+        // Reset the icon for the window when returning from fullscreen mode
+        SendMessage(hwnd, WM_SETICON, ICON_SMALL, 0);
+        SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)data->hicon);
+    }
+
 #ifdef HIGHDPI_DEBUG
     SDL_Log("WIN_SetWindowFullscreen: %d finished. Set window to %d,%d, %dx%d", (int)fullscreen, x, y, w, h);
 #endif
diff --git a/src/video/windows/SDL_windowswindow.h b/src/video/windows/SDL_windowswindow.h
index 5d22b4fb9ea12..d461660d017d1 100644
--- a/src/video/windows/SDL_windowswindow.h
+++ b/src/video/windows/SDL_windowswindow.h
@@ -68,6 +68,7 @@ struct SDL_WindowData
     HDC mdc;
     HINSTANCE hinstance;
     HBITMAP hbm;
+    HICON hicon;
     WNDPROC wndproc;
     HHOOK keyboard_hook;
     WPARAM mouse_button_flags;