From a4d96e74f79fd0a5832e61b0ed73f4fd19be5f56 Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Thu, 22 Dec 2016 22:49:30 +0100 Subject: [PATCH] Implemented SetConsoleIcon() fallback method. --- src/GUI.cpp | 10 ++++------ src/Terminal_Win32.cpp | 46 +++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/GUI.cpp b/src/GUI.cpp index 7cb8e39..6e0bbd1 100644 --- a/src/GUI.cpp +++ b/src/GUI.cpp @@ -95,7 +95,7 @@ namespace MUtils SendMessage(parent->winId(), WM_SETICON, (bIsBigIcon ? ICON_BIG : ICON_SMALL), LPARAM(hIcon)); } - ~WindowIconHelper(void) + virtual ~WindowIconHelper(void) { if(m_hIcon) { @@ -115,12 +115,10 @@ bool MUtils::GUI::set_window_icon(QWidget *const window, const QIcon &icon, cons if((!icon.isNull()) && window->winId()) { const int extend = (bIsBigIcon ? 32 : 16); - if(HICON hIcon = (HICON) MUtils::Win32Utils::qicon_to_hicon(icon, extend, extend)) + if(const HICON hIcon = (HICON) MUtils::Win32Utils::qicon_to_hicon(icon, extend, extend)) { - if(new Internal::WindowIconHelper(window, hIcon, bIsBigIcon)) - { - return true; - } + new Internal::WindowIconHelper(window, hIcon, bIsBigIcon); /*will be free'd using QObject parent mechanism*/ + return true; } } return false; diff --git a/src/Terminal_Win32.cpp b/src/Terminal_Win32.cpp index ceb2530..e0b9ff8 100644 --- a/src/Terminal_Win32.cpp +++ b/src/Terminal_Win32.cpp @@ -72,6 +72,9 @@ static QScopedPointer g_fileBuf_stderr; //The log file static QScopedPointer g_terminal_log_file; +//Terminal icon +static HICON g_terminal_icon = NULL; + /////////////////////////////////////////////////////////////////////////////// // HELPER FUNCTIONS /////////////////////////////////////////////////////////////////////////////// @@ -145,6 +148,15 @@ static inline size_t clean_string(char *const str) return out; } +static inline void set_hicon(HICON *const ptr, const HICON val) +{ + if (*ptr) + { + DestroyIcon(*ptr); + } + *ptr = val; +} + /////////////////////////////////////////////////////////////////////////////// // TERMINAL SETUP /////////////////////////////////////////////////////////////////////////////// @@ -172,6 +184,7 @@ static void terminal_shutdown(void) if(stdout) freopen_s(&temp[0], "NUL", "wb", stdout); if(stderr) freopen_s(&temp[1], "NUL", "wb", stderr); FreeConsole(); + set_hicon(&g_terminal_icon, NULL); g_terminal_attached = false; } } @@ -406,13 +419,36 @@ void MUtils::Terminal::set_icon(const QIcon &icon) if(g_terminal_attached && (!(icon.isNull() || MUtils::OS::running_on_wine()))) { - typedef DWORD(__stdcall *SetConsoleIconFun)(HICON); - if(const SetConsoleIconFun setConsoleIconFun = MUtils::Win32Utils::resolve(QLatin1String("kernel32"), QLatin1String("SetConsoleIcon"))) + if(const HICON hIcon = (HICON) MUtils::Win32Utils::qicon_to_hicon(icon, 16, 16)) { - if(HICON hIcon = (HICON) MUtils::Win32Utils::qicon_to_hicon(icon, 16, 16)) + typedef BOOL(__stdcall *SetConsoleIconFun)(HICON); + bool success = false; + if (const SetConsoleIconFun pSetConsoleIconFun = MUtils::Win32Utils::resolve(QLatin1String("kernel32"), QLatin1String("SetConsoleIcon"))) + { + const DWORD before = GetLastError(); + qWarning("[Before: 0x%08X]", before); + if (pSetConsoleIconFun(hIcon)) + { + success = true; + } + else + { + const DWORD error = GetLastError(); + qWarning("SetConsoleIcon() has failed! [Error: 0x%08X]", error); + } + } + if (!success) + { + const HWND hwndConsole = GetConsoleWindow(); + if ((hwndConsole != NULL) && (hwndConsole != INVALID_HANDLE_VALUE)) + { + SendMessage(hwndConsole, WM_SETICON, ICON_SMALL, LPARAM(hIcon)); + success = true; + } + } + if (success) { - setConsoleIconFun(hIcon); - DestroyIcon(hIcon); + set_hicon(&g_terminal_icon, hIcon); } } } -- 2.11.4.GIT