From 641bfc041e0762fc759a06691cdbdbcb59da02f2 Mon Sep 17 00:00:00 2001 From: Elisha Riedlinger Date: Tue, 10 Sep 2024 17:29:33 -0700 Subject: [PATCH] Only set foreground if not already in foreground --- Dllmain/BuildNo.rc | 2 +- d3d9/IDirect3D9Ex.cpp | 41 ++++++++++++++++++++++++++--------------- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/Dllmain/BuildNo.rc b/Dllmain/BuildNo.rc index 3ee7a7a4..423fc51f 100644 --- a/Dllmain/BuildNo.rc +++ b/Dllmain/BuildNo.rc @@ -1 +1 @@ -#define BUILD_NUMBER 7134 +#define BUILD_NUMBER 7135 diff --git a/d3d9/IDirect3D9Ex.cpp b/d3d9/IDirect3D9Ex.cpp index f7eb6b33..ef5c46d0 100644 --- a/d3d9/IDirect3D9Ex.cpp +++ b/d3d9/IDirect3D9Ex.cpp @@ -619,21 +619,6 @@ void AdjustWindow(HWND MainhWnd, LONG displayWidth, LONG displayHeight, bool isW // Set window active and focus if (Config.EnableWindowMode || isWindowed) { - DWORD currentThreadId = GetCurrentThreadId(); - DWORD foregroundThreadId = GetWindowThreadProcessId(GetForegroundWindow(), NULL); - - // Attach the input of the foreground window and current window - AttachThreadInput(currentThreadId, foregroundThreadId, TRUE); - - // Set the window as the foreground window and active - SetForegroundWindow(MainhWnd); - SetFocus(MainhWnd); - SetActiveWindow(MainhWnd); - BringWindowToTop(MainhWnd); - - // Detach the input from the foreground window - AttachThreadInput(currentThreadId, foregroundThreadId, FALSE); - // Move window to top if not already topmost LONG lExStyle = GetWindowLong(MainhWnd, GWL_EXSTYLE); if (!(lExStyle & WS_EX_TOPMOST)) @@ -643,6 +628,32 @@ void AdjustWindow(HWND MainhWnd, LONG displayWidth, LONG displayHeight, bool isW SetWindowLong(MainhWnd, GWL_EXSTYLE, lExStyle & ~WS_EX_TOPMOST); SetWindowPos(MainhWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED); } + + // Set active and foreground if needed + if (MainhWnd != GetForegroundWindow() || MainhWnd != GetFocus() || MainhWnd != GetActiveWindow()) + { + DWORD currentThreadId = GetCurrentThreadId(); + DWORD foregroundThreadId = GetWindowThreadProcessId(GetForegroundWindow(), NULL); + + bool isForeground = (MainhWnd == GetForegroundWindow()) || (currentThreadId == foregroundThreadId); + + // Attach the input of the foreground window and current window + if (!isForeground) + { + AttachThreadInput(currentThreadId, foregroundThreadId, TRUE); + SetForegroundWindow(MainhWnd); + } + + SetFocus(MainhWnd); + SetActiveWindow(MainhWnd); + BringWindowToTop(MainhWnd); + + // Detach the input from the foreground window + if (!isForeground) + { + AttachThreadInput(currentThreadId, foregroundThreadId, FALSE); + } + } } // Get screen width and height