// NOTE : This solution does not work with Windows 2000 (nee NT5). void CMyDlg::ActualSetForegroundWindow (HWND hWnd) { DWORD dwTimeoutMS; // Get the current lock timeout. SystemParametersInfo (0x2000, 0, &dwTimeoutMS, 0); // Set the lock timeout to zero SystemParametersInfo (0x2001, 0, 0, 0); // Perform the SetForegroundWindow ::SetForegroundWindow (hWnd); // Set the timeout back SystemParametersInfo (0x2001, 0, (LPVOID)dwTimeoutMS, 0); } // If you have the latest header files, you'll see the following // definitions: #define SPI_GETFOREGROUNDLOCKTIMEOUT 0x2000 #define SPI_SETFOREGROUNDLOCKTIMEOUT 0x2001 // I just use the literal values here to ensure it compiles // for everybody. BOOL W2K_SetForegroundWindow (HWND hWndToForce) { HWND hCurrWnd; int iMyTID; int iCurrTID; BOOL bRetVal; hCurrWnd = GetForegroundWindow (); iMyTID = GetCurrentThreadId (); iCurrTID = GetWindowThreadProcessId (hCurrWnd,0); // Connect to the current process. AttachThreadInput (iMyTID, iCurrTID, TRUE); // Now we look like the foreground process, we can set the // foreground window. bRetVal = SetForegroundWindow (hWndToForce); if (!bRetVal) { char szBuffer [60]; wsprintf (szBuffer, "Error %u from SetForegroundWindow\n", GetLastError()); OutputDebugString (szBuffer); } // Now detach. AttachThreadInput (iMyTID, iCurrTID, FALSE); return bRetVal; }