I am trying to make a program similar to Winspector Spy. My problem is that I would like my Virtual Treeview to be updated at all times - that is, update it when a window is created, when a window is destroyed, etc. Of course, all external HWND's.
For that, I was thinking of writing a data container that contained all the Handles + information, and do the EnumWindows and EnumChildWindows in a seperate Thread, where I would fill my data container with said information.
Would you recommend I do it that way, or do you have another solution? If I do it this way, then should I make my Thread run during the whole Program Lifetime, and then just have an infinite loop within the Execute
that would clear my datacontainer, and fill it again, every second, or something?
Here is my Data Container:
unit WindowList;
interface
Uses
Windows, SysUtils, Classes, VirtualTrees, WinHandles, Messages,
Generics.Collections;
type
TWindow = class;
TWindowList = class(TObjectList<TWindow>)
public
constructor Create;
function AddWindow(Wnd : HWND):TWindow;
end;
///////////////////////////////////////
TWindow = class
public
Node : PVirtualNode;
Children : TObjectList<TWindow>;
Handle : HWND;
Icon : HICON;
ClassName : string;
Text : string;
constructor Create(Wnd : HWND);
destructor Destroy;
function AddWindow(Wnd : HWND):TWindow;
end;
implementation
{ TWindowList }
function TWindowList.AddWindow(Wnd: HWND): TWindow;
var
Window : TWindow;
begin
Window := TWindow.Create(Wnd);
Add(Window);
Result := Window;
end;
constructor TWindowList.Create;
begin
inherited Create(True);
end;
{ TWindow }
function TWindow.AddWindow(Wnd: HWND): TWindow;
var
Window : TWindow;
begin
Window := TWindow.Create(Wnd);
Children.Add(Window);
Result := Window;
end;
constructor TWindow.Create(Wnd: HWND);
begin
Handle := Wnd;
if Handle = 0 then Exit;
ClassName := GetClassName(Handle);
Text := GetHandleText(Handle);
Node := Nil;
Children := TObjectList<TWindow>.Create(True);
end;
destructor TWindow.Destroy;
begin
ClassName := '';
Text := '';
Children.Free;
end;
end.