From c059ec65acad357e94b891ee9f5e27f4aeeeedcb Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Mon, 31 Jul 2023 23:43:00 +0200 Subject: [PATCH] Add 'ForceForegroundWindow' windows utility Forces the window handle to be on top. --- r5dev/windows/window.cpp | 30 ++++++++++++++++++++++++++++++ r5dev/windows/window.h | 3 +++ 2 files changed, 33 insertions(+) create mode 100644 r5dev/windows/window.cpp create mode 100644 r5dev/windows/window.h diff --git a/r5dev/windows/window.cpp b/r5dev/windows/window.cpp new file mode 100644 index 00000000..ca4fb4b7 --- /dev/null +++ b/r5dev/windows/window.cpp @@ -0,0 +1,30 @@ +//=============================================================================// +// +// Purpose: Windows window procedure utilities +// +//=============================================================================// +#include "window.h" + +//----------------------------------------------------------------------------- +// Purpose: forces the window handle to the front +// Input : hwnd - +// Output : non-zero on success, null otherwise +//----------------------------------------------------------------------------- +BOOL ForceForegroundWindow(HWND hwnd) +{ + BOOL ret = TRUE; + + DWORD windowThreadProcessId = GetWindowThreadProcessId(GetForegroundWindow(), LPDWORD(0)); + DWORD currentThreadId = GetCurrentThreadId(); + + if (!AttachThreadInput(windowThreadProcessId, currentThreadId, true)) + ret = FALSE; + if (!BringWindowToTop(hwnd)) + ret = FALSE; + if (!ShowWindow(hwnd, SW_SHOW)) + ret = FALSE; + if (!AttachThreadInput(windowThreadProcessId, currentThreadId, false)) + ret = FALSE; + + return ret; +} diff --git a/r5dev/windows/window.h b/r5dev/windows/window.h new file mode 100644 index 00000000..5d48d736 --- /dev/null +++ b/r5dev/windows/window.h @@ -0,0 +1,3 @@ +#pragma once + +BOOL ForceForegroundWindow(HWND hwnd);