如何查找同一应用程序的所有实例的窗口句柄(例如calc.exe)

时间:2017-09-03 12:15:12

标签: delphi

假设我有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;

0 个答案:

没有答案