关闭外部程序有关问题

关闭外部程序问题
procedure TForm1.Button3Click(Sender: TObject);
var
  ExeHandle:THandle;
begin
  ExeHandle:=findWindow(nil,pchar('暴风影音'));
  Button1.Caption:= inttostr(ExeHandle);
  if ExeHandle<>0 then
  PostMessage(ExeHandle, WM_Close, 0, 0);
end;
在播放视频中,不能关闭暴风影音(Button1.Caption为0),
播放完后,可以关闭暴风影音,Button1.Caption显示为它的句柄。
能不能在播放视频中,关闭暴风影音。
如何不用关闭播放影音,只关闭正播放的视频。

------解决方案--------------------
许久不用暴风影音了, 在播放视频时, 暴风影音的标题还是 “暴风影音”吗?

你可以用SPY++看下标题, Button1.Caption为0 说明根本没有找到窗体。

有一个稳当的方法,就是用 GetWindow遍历所有窗体,然后GetWindowText得到窗体标题,

如果窗体内容包括“暴风影音”,则发送关闭消息。
------解决方案--------------------
还有一种方法就是杀进程,搜索一个杀进程函数即可
------解决方案--------------------
var
H:THandle;
P:DWORD;
begin
H:=FindWindow(nil,'暴风影音');
if H&lt;&gt;0 then
begin
GetWindowThreadProcessId(H,@P);
if P&lt;&gt;0 then
TerminateProcess(OpenProcess(PROCESS_TERMINATE,False,P),$FFFFFFFF);
end;

------解决方案--------------------
取得进程快照,然后杀进程
------解决方案--------------------
以下两种方法都可以
Delphi(Pascal) code
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm2 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
    procedure CloseWnd(const Str: string);
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

uses TlHelp32;

{$R *.dfm}

{ TForm2 }

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 TForm2.CloseWnd(const Str: string);
var
  Wnd: THandle;
  buf: array [0..254] of WideChar;
  ArrWnd: array of THandle;
  I: Integer;
begin
  Wnd := GetWindow(Handle, GW_HWNDFIRST);

  repeat
    GetWindowText(Wnd, buf, 255);

    if Pos(UpperCase(Str), UpperCase(string(buf))) > 0 then
    begin
      SetLength(ArrWnd, Length(ArrWnd) + 1);
      ArrWnd[High(ArrWnd)] := Wnd;
    end;

    Wnd := GetWindow(Wnd, GW_HWNDNEXT);
  until Wnd = 0;

  for I := Low(ArrWnd) to High(ArrWnd) do
    SendMessage(ArrWnd[I], WM_CLOSE, 0, 0);
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
  CloseWnd('记事本');
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
  KillTask('notepad.exe');
end;

end.