各位好!我找到一段程序,是在listbox中显示出进程和进程ID,请教如何点击listbox,从而关闭其中一个进程?请给参考程序,非常谢谢!
源程序:
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,Tlhelp32;type
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    procedure Button1Click(Sender: TObject);
     private
    { Private declarations }
  public
  proid:DWORD;
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
 var
FSnapshotHandle:THandle;
FProcessEntry32:TProcessEntry32;
Ret:BOOL;
ProcessID:integer;
ProcessHndle:THandle;
begin
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
Ret:=Process32First(FSnapshotHandle,FProcessEntry32);
while Ret do
begin
ProcessID:=FProcessEntry32.th32ProcessID;
ListBox1.Items.Add(FProcessEntry32.szExeFile+'    '+IntToHex(FProcessEntry32.th32ProcessID,8));
ret:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
end;
end.

解决方案 »

  1.   

    { For Windows NT/2000/XP } procedure KillProcess(hWindowHandle: HWND); 
    var 
      hprocessID: INTEGER; 
      processHandle: THandle; 
      DWResult: DWORD; 
    begin 
      SendMessageTimeout(hWindowHandle, WM_CLOSE, 0, 0, 
        SMTO_ABORTIFHUNG or SMTO_NORMAL, 5000, DWResult);   if isWindow(hWindowHandle) then 
      begin 
        // PostMessage(hWindowHandle, WM_QUIT, 0, 0);     { Get the process identifier for the window} 
        GetWindowThreadProcessID(hWindowHandle, @hprocessID); 
        if hprocessID <> 0 then 
        begin 
          { Get the process handle } 
          processHandle := OpenProcess(PROCESS_TERMINATE or PROCESS_QUERY_INFORMATION, 
            False, hprocessID); 
          if processHandle <> 0 then 
          begin 
            { Terminate the process } 
            TerminateProcess(processHandle, 0); 
            CloseHandle(ProcessHandle); 
          end; 
        end; 
      end; 
    end; procedure TForm1.Button2Click(Sender: TObject); 
    begin 
      KillProcess(FindWindow('notepad',nil)); 
    end;