This site is temporarily down. Please come back. Retrieve the focused control in another process
DelphiFAQ Home Search:

Retrieve the focused control in another process

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

To retrieve the window handle of the currently focused control can easily be done by GetFocs() if it is part of your own process.

For controls in other processes windows you need to attach the input processing mechanism of the thread with the focus to that your own thread. Function AttachThreadInput will attach or detach depending on the last parameter.

You may use this code:

function GetFocussedWindow(ParentWnd:HWnd):HWnd;
var 
  OtherThread,
  Buffer : DWord;
begin
  OtherThread := GetWindowThreadProcessID(ParentWnd, @Buffer);
  if AttachThreadInput(GetCurrentThreadID, OtherThread, true) then 
  begin
    Result := GetFocus;
    AttachThreadInput(GetCurrentThreadID, OtherThread, false);
  end
  else
    Result:=0;
end;

Content-type: text/html

Comments:

2008-03-09, 07:10:09
anonymous  
Here is another example of this that I use to be able to paste something into
an edit box in another program below my program. If this is not used, the
program below will not come into focus after your program is minimized or
closed.

Place this in the last window in your program to be displayed before going to
the program's window below it (for pasteing, etc).

//FormActivate
procedure TFormMD5.FormActivate(Sender: TObject);
begin
Th1 := GetCurrentThreadId;
Th2 := GetWindowThreadProcessId(GetForegroundWindow, nil);
AttachThreadInput(Th2, Th1, true);

Application.Restore; //if previous Application.Minimize;

try
SetForegroundWindow(Application.Handle);
finally
AttachThreadInput(Th2, Th1, false);
end;
end;

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: