假设我有4个运行calc.exe的实例,我必须从我的应用程序以编程方式关闭它们。有没有办法一次性收集所有实例的所有句柄(到一个数组),而不是在这个丑陋的循环中使用下面的代码?
procedure CloseAppGracefully(WindowTitle: string);
var
WindowHandle,
LastWindowHandle: hwnd;
begin
LastWindowHandle := 0;
repeat
WindowHandle := FindWindow(nil, PChar(WindowTitle));
if (WindowHandle <> 0) and (LastWindowHandle <> WindowHandle) then
begin
LastWindowHandle := WindowHandle;
SendMessage(WindowHandle, WM_SYSCOMMAND, SC_CLOSE, 0);
end;
until WindowHandle = 0;
end;