While we are at it, how about this?
function EnumWindowsProc(Handle: HWnd; lParam: LPARAM): Bool; stdcall;
var
S: WideString;
Len: LongInt;
sTitle: string ;
begin
if IsWindowVisible(Handle) then begin
Len := GetWindowTextLengthW(Handle);
if Len > 0 then begin
SetLength(S, Len);
GetWindowTextW(Handle, LPWSTR(S), Len+1);
{Pre-conversion shouldn't be needed, really; but better safe than sorry!}
sTitle := S;
WindowList.Add(sTitle);
end;
end;
Result := True;
end;
What you're really doing is adding to
WindowList the
name of visible windows which have one; hence we test
IsWindowVisible() first of all and only keep going if it is.
Next we check if the window has a name by requesting the size of the buffer needed to hold it. If it's zero, nothing else needs be done so, again, the function can skip the next steps; if
Len, however, is not zero then the window
has a name, so we retrieve it and add it to
WindowList.
Finally, the unconditional
Result := True is used to tell Windows to keep enumerating windows until no more are left.
Quite simple, really.