From 03d5c7ab73e69b6f7051c107643a273c73140627 Mon Sep 17 00:00:00 2001 From: Elisha Riedlinger Date: Thu, 22 Aug 2024 15:07:07 -0700 Subject: [PATCH] Set non-exclusive windows top, active and focused --- Dllmain/BuildNo.rc | 2 +- d3d9/IDirect3D9Ex.cpp | 29 +++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/Dllmain/BuildNo.rc b/Dllmain/BuildNo.rc index 7c3dacca..a58289c7 100644 --- a/Dllmain/BuildNo.rc +++ b/Dllmain/BuildNo.rc @@ -1 +1 @@ -#define BUILD_NUMBER 7102 +#define BUILD_NUMBER 7103 diff --git a/d3d9/IDirect3D9Ex.cpp b/d3d9/IDirect3D9Ex.cpp index 27acab55..64d4ebec 100644 --- a/d3d9/IDirect3D9Ex.cpp +++ b/d3d9/IDirect3D9Ex.cpp @@ -20,7 +20,7 @@ // WndProc hook bool EnableWndProcHook = false; -void AdjustWindow(HWND MainhWnd, LONG displayWidth, LONG displayHeight); +void AdjustWindow(HWND MainhWnd, LONG displayWidth, LONG displayHeight, bool isWindowed); HRESULT m_IDirect3D9Ex::QueryInterface(REFIID riid, void** ppvObj) { @@ -533,7 +533,7 @@ void UpdatePresentParameter(D3DPRESENT_PARAMETERS* pPresentationParameters, HWND GetClientRect(DeviceDetails.DeviceWindow, &Rect); if (AnyChange || Rect.right - Rect.left != DeviceDetails.BufferWidth || Rect.bottom - Rect.top != DeviceDetails.BufferHeight) { - AdjustWindow(DeviceDetails.DeviceWindow, DeviceDetails.BufferWidth, DeviceDetails.BufferHeight); + AdjustWindow(DeviceDetails.DeviceWindow, DeviceDetails.BufferWidth, DeviceDetails.BufferHeight, pPresentationParameters->Windowed); } // Set fullscreen resolution @@ -591,7 +591,7 @@ void UpdatePresentParameterForMultisample(D3DPRESENT_PARAMETERS* pPresentationPa } // Adjusting the window position for WindowMode -void AdjustWindow(HWND MainhWnd, LONG displayWidth, LONG displayHeight) +void AdjustWindow(HWND MainhWnd, LONG displayWidth, LONG displayHeight, bool isWindowed) { if (!IsWindow(MainhWnd) || !displayWidth || !displayHeight) { @@ -600,11 +600,32 @@ void AdjustWindow(HWND MainhWnd, LONG displayWidth, LONG displayHeight) } // Set window active and focus - if (Config.EnableWindowMode) + 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)) + { + SetWindowLong(MainhWnd, GWL_EXSTYLE, lExStyle | WS_EX_TOPMOST); + SetWindowPos(MainhWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED); + SetWindowLong(MainhWnd, GWL_EXSTYLE, lExStyle & ~WS_EX_TOPMOST); + SetWindowPos(MainhWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED); + } } // Get screen width and height