I tried the following code but it does not retrieve text from foreground window!
procedure TForm1.Button1Click(Sender: TObject);
var
title : pansichar;
s : string;
begin
GetWindowText(GetForegroundWindow(), title,GetWindowTextLength(GetForegroundWindow()) + 1);
s := title;
showmessage(s);
end;