private void ForceForegroundWindow(IntPtr hWnd) { if (WindowHandle != IntPtr.Zero) { uint lpdwProcessId; uint foreThread = Interop.GetWindowThreadProcessId(Interop.GetForegroundWindow(), out lpdwProcessId); uint appThread = Interop.GetCurrentThreadId(); const uint SW_SHOW = 5; if (foreThread != appThread) { Interop.AttachThreadInput(foreThread, appThread, true); Interop.BringWindowToTop(hWnd); Interop.ShowWindow(hWnd, SW_SHOW); Interop.AttachThreadInput(foreThread, appThread, false); } else { Interop.BringWindowToTop(hWnd); Interop.ShowWindow(hWnd, SW_SHOW); } } }
public static void RestoreWindow(WindowBase window) { try { // This is the only reliable method that also doesn't result in issues like this: // https://www.reddit.com/r/playnite/comments/f6d73l/bug_full_screen_ui_wont_respond_to_left_stick/ // Adapted from https://ask.xiaolee.net/questions/1040342 window.Show(); if (!window.Activate()) { window.Topmost = true; window.Topmost = false; } if (window.WindowState == WindowState.Minimized) { window.WindowState = WindowState.Normal; } //Get the process ID for this window's thread var interopHelper = new WindowInteropHelper(window); var thisWindowThreadId = Interop.GetWindowThreadProcessId(interopHelper.Handle, IntPtr.Zero); //Get the process ID for the foreground window's thread var currentForegroundWindow = Interop.GetForegroundWindow(); var currentForegroundWindowThreadId = Interop.GetWindowThreadProcessId(currentForegroundWindow, IntPtr.Zero); //Attach this window's thread to the current window's thread Interop.AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, true); //Set the window position Interop.SetWindowPos(interopHelper.Handle, new IntPtr(0), 0, 0, 0, 0, Interop.SWP_NOSIZE | Interop.SWP_NOMOVE | Interop.SWP_SHOWWINDOW); //Detach this window's thread from the current window's thread Interop.AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, false); } catch (Exception e) { logger.Error(e, "Failed to restore window."); } }