33 Votes

Delphi/Lazarus: Get Title of Active Window

Tip by Delphian | 2014-08-13 at 17:05

Today, I would like to show you how to retrieve the title respectively caption of the window currently having the focus in Windows.

The following function is working with Delphi as well as Lazarus (the unit Windows has to be included).

function GetTitleOfActiveWindow: string;
var
  AHandle: THandle;
  ATitle: string;
  ALen: Integer;
begin
  Result := '';
  AHandle := GetForegroundWindow;

  if AHandle <> 0 then begin
    ALen := GetWindowTextLength(AHandle) + 1;
    SetLength(ATitle, ALen);
    GetWindowText(AHandle, PChar(ATitle), ALen);
    result := Trim(ATitle);
  end;
end;  

First of all, we determine the handle of the window that is in the foreground at the moment and thus has the focus. After that, we are reading out the title of this window returning it as the result of the function.

ReplyPositiveNegative

About the Author

AvatarThe author has not added a profile short description yet.
Show Profile

 

Related Topics

Delphi: System-Wide HotKey

Tutorial | 1 Comment

VirtualBox: Change Date and Time

Tutorial | 10 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.