NT下面SetForegroundWindow API函数有小小问题,只能够让窗口闪烁而已,你可以使用下面的<br>函数,不是我写的,摘抄自 Kingron 的猛料。<br><br>procedure ForceForegroundWindow(hwnd: THandle); <br>begin<br> mouse_event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTDOWN,0,0,0,0);<br> mouse_event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTUP,0,0,0,0);<br> SetForegroundWindow(hwnd);<br>end;<br><br>procedure ForceTopmostWindow(hwnd: THandle); <br>begin<br> SetWindowPos(hwnd, HWND_TOPMOST,Form.Left,Form.Top,Form.Width,Form.Height,0);<br>end;<br><br><br>该方法通过模拟鼠标按键来实现激活窗口并最前显示。<br>***********************************************************************************************************<br><br>function ForceForegroundWindow(hwnd: THandle): boolean;<br>const<br> SPI_GETFOREGROUNDLOCKTIMEOUT = $2000;<br> SPI_SETFOREGROUNDLOCKTIMEOUT = $2001;<br>var<br> timeout: DWORD;<br>begin<br> if ((Win32Platform = VER_PLATFORM_WIN32_NT) and (Win32MajorVersion > 4)) or<br> ((Win32Platform = VER_PLATFORM_WIN32_WINDOWS) and<br> ((Win32MajorVersion > 4) or ((Win32MajorVersion = 4) and (Win32MinorVersion > 0)))) then begin<br> SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, @timeout, 0);<br> SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, TObject(0), SPIF_SENDCHANGE);<br> Result := SetForegroundWindow(hWnd);<br> SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, TObject(timeout), SPIF_SENDCHANGE);<br> end else<br> Result := SetForegroundWindow(hWnd);<br>end; { ForceForegroundWindow }<br><br>***********************************************************************************<br><br>function ForceForegroundWindow(hwnd: THandle): boolean; <br>const <br> SPI_GETFOREGROUNDLOCKTIMEOUT = $2000; <br> SPI_SETFOREGROUNDLOCKTIMEOUT = $2001; <br>var <br> ForegroundThreadID: DWORD; <br> ThisThreadID : DWORD; <br> timeout : DWORD; <br>begin <br> if IsIconic(hwnd) then ShowWindow(hwnd, SW_RESTORE); <br><br> if GetForegroundWindow = hwnd then Result := true <br> else begin <br> if ((Win32Platform = VER_PLATFORM_WIN32_NT) and (Win32MajorVersion > 4)) or <br> ((Win32Platform = VER_PLATFORM_WIN32_WINDOWS) and <br> ((Win32MajorVersion > 4) or ((Win32MajorVersion = 4) and <br> (Win32MinorVersion > 0)))) then <br> begin <br> Result := false; <br> ForegroundThreadID := GetWindowThreadProcessID(GetForegroundWindow,nil); <br> ThisThreadID := GetWindowThreadPRocessId(hwnd,nil); <br> if AttachThreadInput(ThisThreadID, ForegroundThreadID, true) then begin <br> BringWindowToTop(hwnd); // IE 5.5 related hack <br> SetForegroundWindow(hwnd); <br> AttachThreadInput(ThisThreadID, ForegroundThreadID, false); <br> Result := (GetForegroundWindow = hwnd); <br> end; <br> if not Result then begin <br> SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, @timeout, 0); <br> SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, TObject(0), SPIF_SENDCHANGE); <br> BringWindowToTop(hwnd); // IE 5.5 related hack <br> SetForegroundWindow(hWnd); <br> SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, TObject(timeout), SPIF_SENDCHANGE); <br> end; <br> end <br> else begin <br> BringWindowToTop(hwnd); // IE 5.5 related hack <br> SetForegroundWindow(hwnd); <br> end; <br><br> Result := (GetForegroundWindow = hwnd); <br> end; <br>end; { ForceForegroundWindow } <br>