How to bring window to the foreground?
In the past the question was trivial - SetForegroundWindow()
Win32 API function was working fine in the time of Win95 and WinNT4. But since Win98 the things have been changed. The function call will not bring the appication window to the foreground in Win98/2K if the application is not currently active (has an input focus). This gives only minimized window blinking in the task bar. In general, that a good idea (recommended by Microsoft) to not irritate a user with suddenly popuping window, but sometimes it is really required. There are some overcomings of the matter. I know three of them.
The first one intends using undocumented SwitchToThisWindow()
API function from user32.dll.
HMODULE hLib = GetModuleHandle("user32.dll"); void (__stdcall *SwitchToThisWindow)(HWND, BOOL); (FARPROC &)SwitchToThisWindow = GetProcAddress(hLib, "SwitchToThisWindow"); SwitchToThisWindow(hWnd, TRUE);
But it does not work in the lates versions of Win98/2K.
Another means is to use something like this:
DWORD dwTimeout; SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, &dwTimeout, 0); SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, 0, 0); SetForegroundWindow(hWnd); SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, (LPVOID)dwTimeout, 0);
It works in Win98 but not in Win2K.
And the last third trick is to use AttachThreadInput()
to help with the desired behaviour.
HWND hCurrWnd; int iMyTID; int iCurrTID; hCurrWnd = ::GetForegroundWindow(); iMyTID = GetCurrentThreadId(); iCurrTID = GetWindowThreadProcessId(hCurrWnd,0); AttachThreadInput(iMyTID, iCurrTID, TRUE); SetForegroundWindow(hWnd); AttachThreadInput(iMyTID, iCurrTID, FALSE);
This works in Win98/ME/2K always. I have tested it in Win2K. Works indeed. For the details see recommended links.