Win32 Setforegroundwindow Unreliable

Win32 SetForegroundWindow unreliable

Your AttachThreadInput() hack is (I think) a known way to defeat the focus stealing counter-measures in Windows. You are using the wrong handle though, you want to attach to the thread that currently has the focus. Which won't be hApp, you wouldn't need this code otherwise.

Use GetForegroundWindow() to get the handle to the window with the focus.

AttachThreadInput(
GetWindowThreadProcessId(GetForegroundWindow(), NULL),
GetCurrentThreadId(), TRUE
);

Although I think the 2nd argument needs to be thread ID of hApp. Because you don't want to shove your own window if I understood correctly. Not sure if that can work.

Issue with SetForegroundWindow in .NET

David was right and led me to the right direction . Followed the code project article which says
"The system automatically enables calls to SetForegroundWindow if the user presses the ALT key or takes some action that causes the system itself to change the foreground window"

How to bring window to top with SetForegroundWindow()

Win32 ::SetForegroundWindow() not working all the time

The system restricts which processes can set the foreground window. A
process can set the foreground window only if one of the following
conditions is true:

  • The process is the foreground process.
  • The process was started by the foreground process.
  • The process received the last input event.
  • There is no foreground process.
  • The foreground process is being debugged.
  • The foreground is not locked (see LockSetForegroundWindow).
  • The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
  • No menus are active.

See the SetForegroundWindow() docs for more details.

Steal focus as SetForegroundWindow can't do it

This always works, no matter what app is in foreground (like Task Manager, etc.):

AllocConsole(); 
auto hWndConsole = GetConsoleWindow();
SetWindowPos(hWndConsole, 0, 0, 0, 0, 0, SWP_NOZORDER);
FreeConsole();
SetForegroundWindow(hWnd);

Put application to foreground

From MSDN

The system restricts which processes can set the foreground window. A process can set the foreground window only if one of the following conditions is true:

  • The process is the foreground process.
  • The process was started by the foreground process.
  • The process received the last input event.
  • There is no foreground process.
  • The process is being debugged.
  • The foreground process is not a Modern Application or the Start Screen.
  • The foreground is not locked (see LockSetForegroundWindow).
  • The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
  • No menus are active.

So, if your program does not correspond to the above, you can not set foreground automatically.

I think you can use below code for your case and this link can be help as well.

void SetForegroundWindowForce(HWND hWnd)
{
HWND hWndForeground = ::GetForegroundWindow();
if(hWndForeground == hWnd) return;

DWORD Strange = ::GetWindowThreadProcessId(hWndForeground, NULL);
DWORD My = ::GetWindowThreadProcessId(hWnd, NULL);
if( !::AttachThreadInput(My, Strange, TRUE) )
{
ASSERT(0);
}
::SetForegroundWindow(hWnd);
::BringWindowToTop(hWnd);
if( !::AttachThreadInput(My, Strange, FALSE) )
{
ASSERT(0);
}
}

SetForgroundWindow() fails after program was Updated

I ended up using the method shown here:
http://www.codeproject.com/Tips/76427/How-to-bring-window-to-top-with-SetForegroundWindo



Related Topics



Leave a reply



Submit