DelphiFAQ Home Search:

Get caption and class of active window in active (other) process

 

comments1 comments. Current rating: 4 stars (1 votes). Leave comments and/ or rate it.

Question:

How can I find the active child window (like the 'follow focus' option in WinSight). I know how to enumerate child windows, but I don't know how to detect if a child window is active (has the caret / focus).


Answer:

Use the procedure below. It will return the handle, class name and caption text through 3 var parameters.

procedure GetFocusedControl(var FocusWin: HWND; var ClassName, ControlText: string);
var
  hOtherWin: HWND;
  OtherThreadID: Cardinal;
  aDwordvar: DWORD;
  hFocusWin: HWND;
  S,
  ss: string;
  I: Integer;
begin
  S := '';
  ss := '';
  hFocusWin := -1;
  hOtherWin := GetForegroundWindow;
  OtherThreadID := GetWindowThreadProcessID(hOtherWin, @aDwordvar);
  if AttachThreadInput(GetCurrentThreadID, OtherThreadID, True) then
  begin
    hFocusWin := GetFocus;
    if hFocusWin<>0 then
      try
        I := SendMessage(hFocusWin, WM_GETTEXTLENGTH, 0, 0);
        SetLength(S, I+1);
        SendMessage(hFocusWin, WM_GETTEXT, I+1, Integer(PChar(S)));

        SetLength(ss, 260);
        GetClassName(hFocusWin, PChar(ss), 260);
      finally
        AttachThreadInput(GetCurrentThreadID, OtherThreadID, False);
      end;
  end;
  FocusWin := hFocusWin;
  ClassName := Trim(ss);
  ControlText := Trim(S);
end;
You don't like the formatting? Check out SourceCoder then!
Content-type: text/html

Comments:

2007-09-21, 07:26:12
[hidden] from Turkey  
rating
How can I find the name of Control for ex: (Class = TEdit)

(Name = 'edtServerName' )

?

 

 

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: