DelphiFAQ Home Search:
General :: Windows :: Programming :: Windows with Delphi :: Windows Forms
Code snippets, Q+A around the Windows user interface. From a Delphi perspective, but usually applicable to other languages.

Articles:

This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.

Only the 40 most recently viewed articles are shown.
You can see the full list here.

Featured Article

Retrieve the focused control in another process

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;