mirror of
https://github.com/ngeor/Chameleon.git
synced 2025-12-19 09:53:43 +01:00
refactor: Simplify implementation of WindowEnumerator
This commit is contained in:
parent
91f81c941b
commit
fa7f696a53
@ -8,59 +8,37 @@ uses
|
|||||||
Windows;
|
Windows;
|
||||||
|
|
||||||
type
|
type
|
||||||
|
{ An object method that accepts a HWND }
|
||||||
TWndConsumer = procedure (Wnd: HWND) of object;
|
TWndConsumer = procedure (Wnd: HWND) of object;
|
||||||
|
|
||||||
|
{ Calls the given consumer for every child window of the given window }
|
||||||
procedure EnumerateChildWindows(Wnd: HWND; Consumer: TWndConsumer);
|
procedure EnumerateChildWindows(Wnd: HWND; Consumer: TWndConsumer);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
type
|
type
|
||||||
TWindowEnumerator = class
|
{ Wrapper structure to hold the object procedure and pass it as a pointer }
|
||||||
private
|
PWrapper = ^TWrapper;
|
||||||
FWnd: HWND;
|
TWrapper = record
|
||||||
FConsumer: TWndConsumer;
|
Consumer: TWndConsumer;
|
||||||
class function MyEnumChildrenProc(Wnd: HWND; Lp: LPARAM): BOOL; static; stdcall;
|
|
||||||
procedure Add(Wnd: HWND);
|
|
||||||
public
|
|
||||||
constructor Create(Wnd: HWND; Consumer: TWndConsumer);
|
|
||||||
procedure Run;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure EnumerateChildWindows(Wnd: HWND; Consumer: TWndConsumer);
|
{ Callback for Windows' EnumChildWindows }
|
||||||
|
function MyEnumChildrenProc(Wnd: HWND; Lp: LPARAM): BOOL; stdcall;
|
||||||
var
|
var
|
||||||
Enumerator: TWindowEnumerator;
|
Wrapper: PWrapper;
|
||||||
begin
|
begin
|
||||||
Enumerator := TWindowEnumerator.Create(Wnd, Consumer);
|
Wrapper := PWrapper(Lp);
|
||||||
try
|
Wrapper^.Consumer(Wnd);
|
||||||
Enumerator.Run;
|
|
||||||
finally
|
|
||||||
Enumerator.Free;
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
|
|
||||||
constructor TWindowEnumerator.Create(Wnd: HWND; Consumer: TWndConsumer);
|
|
||||||
begin
|
|
||||||
FWnd := Wnd;
|
|
||||||
FConsumer := Consumer;
|
|
||||||
end;
|
|
||||||
|
|
||||||
procedure TWindowEnumerator.Run;
|
|
||||||
begin
|
|
||||||
EnumChildWindows(FWnd, @TWindowEnumerator.MyEnumChildrenProc, LPARAM(Self));
|
|
||||||
end;
|
|
||||||
|
|
||||||
class function TWindowEnumerator.MyEnumChildrenProc(Wnd: HWND; Lp: LPARAM): BOOL; static; stdcall;
|
|
||||||
var
|
|
||||||
Me: TWindowEnumerator;
|
|
||||||
begin
|
|
||||||
Me := TWindowEnumerator(Lp);
|
|
||||||
Me.Add(Wnd);
|
|
||||||
Result := True;
|
Result := True;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TWindowEnumerator.Add(Wnd: HWND);
|
procedure EnumerateChildWindows(Wnd: HWND; Consumer: TWndConsumer);
|
||||||
|
var
|
||||||
|
Wrapper: TWrapper;
|
||||||
begin
|
begin
|
||||||
FConsumer(wnd);
|
Wrapper.Consumer := Consumer;
|
||||||
|
EnumChildWindows(Wnd, @MyEnumChildrenProc, LPARAM(@Wrapper));
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user