function GetActiveControl: THandle;
var
ThreadID: Integer;
begin
ThreadID:= GetWindowThreadProcessID(GetForegroundWindow,
nil);
if AttachThreadInput(GetCurrentThreadID, ThreadID, True)
then begin
Result:= GetFocus;
// Control in fremden Thread
AttachThreadInput(GetCurrentThreadID, ThreadID, False);
end else
Result:= GetFocus;
// Control in eigenem Thread
end;
function GetWindowText(
const Handle: THandle):
string;
var
Len: Integer;
Text: PChar;
begin
Result:= '
';
Len:= SendMessage(
Handle, WM_GETTEXTLENGTH, 0, 0);
if Len <> 0
then begin
GetMem(Text, Len+1);
if Assigned(Text)
then try
SendMessage(
Handle, WM_GETTEXT, Len+1, Integer(Text));
Result:= Text;
finally
FreeMem(Text);
end;
end;
end;
function GetSelText(
const Handle: THandle):
string;
var
SelStart, SelEnd: Integer;
begin
SendMessage(
Handle, EM_GETSEL, Integer(@SelStart), Integer(@SelEnd));
Result:= Copy(GetWindowText(
Handle), SelStart+1, SelEnd - SelStart);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Caption:= GetSelText(GetActiveControl);
end;