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


本文整理自网络,侵删。

 Delphi 任务管理器 获取窗口标题 获取窗口图标新建一个Application,在主窗口Form1上添加一个ListView1,一个Timer1, 一个Button1(结束选中进程)后写下面的代码即可。


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.

相关阅读 >>

Delphi xe5 android程序常用的unit

Delphi串口通信编程

Delphi图像处理 -- 图像卷积

Delphi 控制台base64加密解密

Delphi 读取eset nod32 的用户名与密码

Delphi 泛型搜索字符串数组

Delphi readprocessmemory用法

Delphi中format与formatdatetime函数详解

Delphi中实现全角转半角

Delphi最简化异步选择tcp服务器

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...