在delphi中如何获取所有已运行程序的名称和该程序的窗口标题,有谁知道,非常感谢!
//以下MEMO1列出进程名单
uses TlHelp32
var
p: pProcessInfo;
ContinueLoop: BOOL;
FSnapshotHandle, hProcess: THandle;
FProcessEntry32: TProcessEntry32;
begin
New(p);
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);
while integer(ContinueLoop)<>0 do
begin
p.ExeFile:= FProcessEntry32.szExeFile;
memo1.lines.add(p.ExeFile);
ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;
//以下MEMO2列出窗口名单
function GetWindow1(btitle:string):boolean;
var winhandle: Hwnd;
winprocess: Dword;
title:pchar;
begin
result:=false;
Getmem(title,255);
winhandle:=GetWindow(GetForeGroundWindow,GW_HWNDFIRST);
while winhandle<>0 do
begin
if (IsWindowEnabled(winhandle) and IsWindow(winhandle)) then
begin
// GetWindowThreadProcessId(winhandle,@winprocess);
GetWindowText(winhandle,title,255);
if length(title)>0 then
begin
memo2.lines.add(title);
end;
end;
winhandle:=GetWindow(winhandle,GW_HWNDNEXT);
end;
Freemem(title);
CloseHandle(winHandle);
result:=true;
end;
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
WindowsNameListBox: TListBox;
ListButton: TButton;
procedure ListButtonClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.ListButtonClick(Sender: TObject);
var
hCurrentWindow: HWnd;
szText: array[0..254] of char;
begin
WindowsNameListBox.Clear;
//获取第一个窗口的句柄
hCurrentWindow := GetWindow(Handle, GW_HWNDFIRST);
while hCurrentWindow <> 0 do
begin
//获取窗口的名称
if GetWindowText(hCurrentWindow, @szText, 255)>0 then
WindowsNameListBox.Items.Add(StrPas(@szText));
//获取下一窗口的句柄
hCurrentWindow:=GetWindow(hCurrentWindow, GW_HWNDNEXT);
end;
end;
end.
接分ing。。。。