Delphi_任务管理器

Delphi_任务管理器

Delphi 任务管理器 获取窗口标题 获取窗口图标

新建一个Application,在主窗口Form1上添加一个ListView1,一个Timer1, 一个Button1(结束选中进程)后写下面的代码即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, ImgList, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
ImageList: TImageList;
Timer1: TTimer;
ListView1: TListView;
Button1: TButton;
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ListView1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
procedure ShowWindowsList;
{ Private declarations }
public
{ Public declarations }
end;
TWinInfo=class
FIcon:HICON;
FCaption:String;
FClsName:String;
FHandle:THandle;
end;
var
Form1: TForm1;
implementation
{$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.FormCreate(Sender: TObject);
begin
ListView1.Clear;
ListView1.Columns.Clear;
ListView1.Columns.Add;
//ListView1.Columns.Add;
//ListView1.Columns.Add;
ListView1.Columns.Items[0].Caption:='任务';
ListView1.Columns.Items[0].AutoSize:=true;
//ListView1.Columns.Items[0].Width:=self.Width;
//ListView1.Columns.Items[1].Caption:='状态';
//ListView1.Columns.Items[2].Caption:=' ';
//ListView1.Columns.Items[0].AutoSize:=true;
//ListView1.Columns.Items[2].Width:=300;
Listview1.ViewStyle:=vsreport;
//Listview1.GridLines:=true; //注:此处代
ListView1.Align:=Alclient;
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;
end.

热评文章