I’m in the middle of porting a very large legacy code base from MS Windows to Linux.
After looking at the alternatives, I chose SDL as my cross-platform window manager.
As I go, I’ve noticed SDL handles a lot of MS Windows quirks the way we do. The most recent outpoint I found involves raising the current window, i.e. SDL_RaiseWindow()
. MS Windows goes through a lot of trouble to make it nearly impossible to programmatically move a window to the foreground, for “security” reasons. I don’t know if there’s any interest in replacing SDL_windowswindow.c’s WIN_RaiseWindow()
with this, or maybe have another function like SDL_ForceRaiseWindow(), but I at least wanted to start the discussion.
// Technique taken from http://stackoverflow.com/questions/916259/ .
// Specifically, http://stackoverflow.com/a/34414846 .
//
// The issue is that Microsoft has gone through a lot of trouble to make it
// nearly impossible to programmatically move a window to the foreground,
// for "security" reasons. Apparently, the following song-and-dance gets
// around their objections.
HWND m_hWnd = m_pScreen->getWindowHandle();
HWND hCurWnd = ::GetForegroundWindow();
DWORD dwMyID = ::GetCurrentThreadId();
DWORD dwCurID = ::GetWindowThreadProcessId(hCurWnd, NULL);
::ShowWindow(m_hWnd, SW_RESTORE);
::AttachThreadInput(dwCurID, dwMyID, TRUE);
::SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
::SetWindowPos(m_hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
::SetForegroundWindow(m_hWnd);
::AttachThreadInput(dwCurID, dwMyID, FALSE);
::SetFocus(m_hWnd);
::SetActiveWindow(m_hWnd);