通过PostMessage(窗口句柄, WM_SYSCOMMAND, SC_CLOSE, 0);
或者PostMessage(窗口句柄, WM_CLOSE, 0, 0);
都不能关闭存在于托盘内的程序,请教咋办?

解决方案 »

  1.   

    先获取所有进程列表,然后TerminateProcess
      

  2.   

    hWndClose := FindWindow(nil, PChar('C:\Program Files\Tencent\qq\QQ.exe'));
          if hWndClose <> 0 then
            SendMessage(hWndClose, WM_CLOSE, 0, 0)
      

  3.   

    获取所有进程列表的程序(在Delphi6、Win2000下调试通过)unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Memo1: TMemo;
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementationuses Tlhelp32;{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    var
      SS: Cardinal;
      status: Boolean;
      processinfo: PROCESSENTRY32;
      buffer: string;
      Length: Integer;
    begin
      Memo1.ScrollBars := ssBoth;
      Memo1.Lines.Clear();  processinfo.dwSize := sizeof(processinfo);
      SS := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);  if SS = 0 then Exit;  status := Process32First(SS, processinfo);
      while (status) do
      begin
        Memo1.Lines.Add('Process Name:'+IntToHex(processinfo.th32ProcessID,8));
        Memo1.Lines.Add('Parent Process Name:'+IntToHex(processinfo.th32ParentProcessID,8));
        Memo1.Lines.Add(processinfo.szExeFile);
        Memo1.Lines.Add('_____________________________________');    status := Process32Next(SS, processinfo);
      end;
      CloseHandle(SS);
    end;end.
      

  4.   

    kill a task?  {For Windows 9x/ME/2000/XP }uses
      Tlhelp32;function KillTask(ExeFileName: string): Integer;
    const
      PROCESS_TERMINATE = $0001;
    var
      ContinueLoop: BOOL;
      FSnapshotHandle: THandle;
      FProcessEntry32: TProcessEntry32;
    begin
      Result := 0;
      FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
      FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
      ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);  while Integer(ContinueLoop) <> 0 do
      begin
        if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
          UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
          UpperCase(ExeFileName))) then
          Result := Integer(TerminateProcess(
                            OpenProcess(PROCESS_TERMINATE,
                                        BOOL(0),
                                        FProcessEntry32.th32ProcessID),
                                        0));
         ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
      end;
      CloseHandle(FSnapshotHandle);
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      KillTask('notepad.exe');
    end;{ 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;
      

  5.   

    將例子中的 notepad , notepad.exe 改成你要的, 應該就可
      

  6.   

    TerminateProcess
    sendmessage都可以的
      

  7.   

    感谢这么多人提供建议
    我发现killprocess可以杀掉QQ但,不能杀掉kaspersky,不知道为什么,有兴趣大家试试看
      

  8.   

    写了一个完整的 KillProcess 例程:
    http://www.rtgame.com/KillProcess.zip完整代码:unit FormMain;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ComCtrls;type
      TForm1 = class(TForm)
        lstProcess: TListView;
        procedure FormCreate(Sender: TObject);
        procedure lstProcessDblClick(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementationuses Tlhelp32;{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    var
      SS: Cardinal;
      Status: Boolean;
      ProcessInfo: PROCESSENTRY32;
    begin
      Caption := Application.Title;
      
      ProcessInfo.dwSize := SizeOf(ProcessInfo);
      SS := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);  if SS = 0 then Exit;  Status := Process32First(SS, ProcessInfo);
      while Status do
      begin
        lstProcess.AddItem(IntToHex(ProcessInfo.th32ProcessID,8), nil);
        with lstProcess do
          with Items[Items.Count-1].SubItems do
          begin
            Add(IntToHex(ProcessInfo.th32ParentProcessID, 8));
            Add(ProcessInfo.szExeFile);
          end;
        Status := Process32Next(SS, ProcessInfo);
      end;
      CloseHandle(SS);
    end;procedure TForm1.lstProcessDblClick(Sender: TObject);
    var
      Index: Integer;
      ProcessID: Cardinal;
      ProcessExeFile: string;
    begin
      Index := lstProcess.ItemIndex;
      if Index = -1 then Exit;  ProcessID := StrToInt('$' + lstProcess.Items.Item[Index].Caption);
      ProcessExeFile := lstProcess.Items.Item[Index].SubItems.Strings[1];
      if MessageBox(Handle, PChar('真的要终止进程 "' + ProcessExeFile + '" 么?'),
        PChar(Application.Title), MB_ICONQUESTION or MB_YESNO) = idYes then
        if TerminateProcess(OpenProcess(PROCESS_TERMINATE or PROCESS_QUERY_INFORMATION, False, ProcessID), 0) then
          lstProcess.Items.Delete(Index)
        else
          MessageBox(Handle, '无法终止指定的进程!', PChar(Application.Title), MB_ICONWARNING)
    end;end.
      

  9.   

    在 Win2000、WinXP 下测试通过。