{$R *.dfm}
//结束指定进程
procedure KillProcess(hprocessID: HWND);
var
processHandle:HWND;
DWResult: DWORD;
begin
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;
function EnumWindowsProc(Handle: HWND; List: TStrings):boolean;stdcall;
var
WinStyles : DWord;
WinText:Array[0..100] of Char;
WinClsName:Array[0..100] of Char;
strTmp:string;
aIcon:HICON;
AI:TWinInfo;
begin
Result := true;
WinStyles := GetWindowLong(Handle, GWL_STYLE);
If ((WinStyles and WS_VISIBLE) > 0) then
begin
GetClassName(Handle,WinClsName,100);
GetWindowText(Handle,WinText,100);
strTmp := WinText;
If Trim(strTmp)<>'' then
begin
AI := TWinInfo.Create;
AI.FIcon := GetClassLong(Handle,GCL_HICON);
AI.FCaption := strTmp;
AI.FClsName := WinClsName;
AI.FHandle := Handle;
List.AddObject(IntToStr(Handle),AI);
end;
end;
end;
procedure GetAllWindow(List:TStrings);
begin
EnumWindows(@EnumWindowsProc,integer(List));
end;
procedure TForm1.Button1Click(Sender: TObject);
var
anItem:TListItem;
AI:TWinINfo;
ah:HWND;
begin
anItem := ListView1.Selected;
If Assigned(anItem) then
begin
try
Timer1.Enabled := false;
aI := TWinInfo(anItem.Data);
GetWindowThreadProcessId(aI.FHandle,@aH);
//ShowMessage(IntToStr(aH));
If ah<>0 then
KillProcess(ah);
finally
Timer1.Enabled := true;
end;
end;
end;
procedure TForm1.ListView1Click(Sender: TObject);
var
h: HWND;
ps: array[0..254] of Char;
begin //显示选择中的窗口名称
ShowMessage(ListView1.Selected.Caption); //返回选中行第三列中的值
begin
h := FindWindow(nil, pchar(ListView1.Selected.Caption)); {这句是获取记事本窗口的句柄}
GetClassName(h, ps, 255);
ShowMessage('选择的窗口类名为:'+ps); {Notepad}
end;
end;
procedure TForm1.ShowWindowsList;
var
List:TStringList;
I,Index,ImgIndex,J:Integer;
AItem:TListItem;
AI,NewAI:TWinInfo;
AIcon:TIcon;
ProcIDList:TStringList;
function CheckProcID(aWinHandle:THandle;clsType:String):Boolean;
var
strH:String;
aPID:HWND;
begin
If clsType='Progman' then
begin
Result := false;
Exit;
end;
If (clsType='IEFrame') Or (clsType='CabinetWClass') then //IE,exploerer
begin
Result := true;
Exit;
end;
GetWindowThreadProcessId(aWinHandle, @aPID);
strH := IntToStr(aPID);
If ProcIDList.IndexOf(strH)=-1 then
begin
ProcIDList.Add(strH);
Result := true;
end else Result := false;
end;
function GetWinIcon(aHandle:THandle):TICON;
var
tmpIcon:TIcon;
begin
tmpICon := TICon.Create;
tmpICon.Handle := HICON(SendMessage(aHandle,WM_GETICON, ICON_SMALL,0));
If tmpICon.Handle=0 then
tmpIcon.Handle := HICON(SendMessage(aHandle,WM_GETICON, ICON_Big,0));
If tmpIcon.Handle=0 then
tmpIcon.Handle := GetClassLong(aHandle,GCL_HICON);
Result := tmpICon;
end;
begin
//过程可能内存泄露
List := TStringList.Create;
ProcIDList := TStringList.Create;
try
GetAllWindow(List);
ImageList.Clear;
J := 0;
ListView1.Items.BeginUpdate;
For I := 0 To ListView1.Items.Count-1 do
begin
If J<0 then
J := 0;
AItem := ListView1.Items.Item[J];
AI := TWinInfo(AItem.Data);
If Not CheckProcID(AI.FHandle,AI.FClsName) then
begin
INC(J);
Continue;
end;
Index := List.IndexOf(IntToStr(AI.FHandle));
If Index<>-1 then
begin
NewAI := TWinInfo(List.Objects[Index]);
AIcon := GetWinIcon(NewAI.FHandle);
ImgIndex := ImageList.AddIcon(AIcon);
AItem.ImageIndex := ImgIndex;
AItem.Caption := NewAI.FCaption;
//AItem.SubItems.Strings[0] := '正在运行';
AItem.Data := NewAI;
List.Delete(Index);
INC(J);
end else
begin
ListView1.Items.Delete(J);
DEC(J);
end;
AI.Free;
end;
For I :=0 To List.Count-1 do
begin
NewAI := TWinInfo(List.Objects[I]);
If Not CheckProcID(NewAI.FHandle,NewAI.FClsName) then
Continue;
AIcon := GetWinIcon(NewAI.FHandle);
ImgIndex := ImageList.AddIcon(AIcon);
AItem := ListView1.Items.Add;
AItem.ImageIndex := ImgIndex;
AItem.Caption := NewAI.FCaption;
//AItem.SubItems.Add(NewAi.FClsName);
//AItem.SubItems.Add('正在运行');
AItem.Data := NewAI;
end;
finally
ListView1.Items.EndUpdate;
List.Free;
ProcIDList.Free;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin //在ListView中显示进程列表
ShowWindowsList;
end;