Code:
procedure ActivateApplication;
var
ActiveThreadId, CurrentThreadId: DWORD;
begin
if GetForegroundWindow <> Application.Handle then
begin
ActiveThreadId := GetWindowThreadProcessId(GetForegroundWindow, nil);
CurrentThreadId := GetCurrentThreadId;
AttachThreadInput(CurrentThreadId, ActiveThreadId, True);
SetForegroundWindow(Application.Handle);
AttachThreadInput(CurrentThreadId, ActiveThreadId, False);
BringWindowToTop(Application.Handle);
end;
end;