Dmitry
Dmitry

Reputation: 14632

How to get ClassName of active form (may be from another application) on Delphi?

How to get ClassName of active form (may be from another application) on Delphi?

It seems Application.ActiveFormHandle returns active form of Application only.

Upvotes: 6

Views: 4113

Answers (1)

David Heffernan
David Heffernan

Reputation: 613582

The window handle that you are looking for is, I believe, returned by GetForegroundWindow.

To get the class name, pass that window handle to the Windows API function GetClassName. Here's a Delphi wrapper to that API function:

function GetWindowClassName(Window: HWND): string;
const
  MaxClassNameLength = 257;//256 plus null terminator
var
  Buffer: array [0..MaxClassNameLength-1] of Char;
  len: Integer;
begin
  len := GetClassName(Window, Buffer, Length(Buffer));
  if len=0 then
    RaiseLastOSError;
  SetString(Result, Buffer, len);
end;

I used a buffer of length 256 because window class names are not allowed to be longer than that.

Upvotes: 11

Related Questions