R.Wieser a écrit :
I use the following function, which works most of the time (tested only
on XP SP3 & Windows 7 SP1)
In some rare cases, I add a "fake" SendInput() then I call the function
again.
void SwitchToWindow(HWND hWnd)
{
DWORD dwLockTimeOut = 0;
HWND hWndForeground = GetForegroundWindow();
DWORD dwCurrentTID = GetCurrentThreadId(), dwForegroundTID =
GetWindowThreadProcessId(hWndForeground, 0);
if (dwCurrentTID != dwForegroundTID)
{
AttachThreadInput(dwCurrentTID, dwForegroundTID, TRUE);
SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, &dwLockTimeOut, 0);
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, 0,
SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
AllowSetForegroundWindow(ASFW_ANY);
}
if (IsIconic(hWnd))
ShowWindow(hWnd, SW_RESTORE);
HWND hWndLastActivePopup = GetLastActivePopup(hWnd);
void (FAR STDAPICALLTYPE * pSTTW)(HWND hWnd, BOOL bAltTab);
HINSTANCE hInst = LoadLibrary(L"USER32.DLL");
if (hInst)
{
(FARPROC&)pSTTW = GetProcAddress(hInst, "SwitchToThisWindow");
if (pSTTW != NULL)
pSTTW(hWndLastActivePopup, TRUE);
FreeLibrary(hInst);
}
SetForegroundWindow(hWnd);
if (dwCurrentTID != dwForegroundTID)
{
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0,
(PVOID)dwLockTimeOut, SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
AttachThreadInput(dwCurrentTID, dwForegroundTID, FALSE);
}
}