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
Getting the Current Time (In Milliseconds) from the System Clock in Windows
G++ Error:/Usr/Lib/Rpm/Redhat/Redhat-Hardened-Cc1: No Such File or Directory
Why Don't I Need to Specify "Typename" Before a Dependent Type in C++20
Zero Initialization and Static Initialization of Local Scope Static Variable
Warning C4003 and Errors C2589 and C2059 On: X = Std::Numeric_Limits<Int>::Max();
How to Build Openssl with Mingw in Windows
Is Std::Vector<T> a 'User-Defined Type'
How to Use C++ Modules in Clang
Member Initializer Does Not Name a Non-Static Data Member or Base Class
How to Use a Lambda Expression as a Template Parameter
Purpose of Explicit Default Constructors
In C++, Differencebetween a Method and a Function
Qapplication in Non-Main Thread
C++ Const Keyword - Use Liberally
Node Packages Not Building on Windows 8.1 - Missing Microsoft.Cpp.Default.Props