在delphi中如何获取所有已运行程序的名称和该程序的窗口标题,有谁知道,非常感谢!

解决方案 »

  1.   

    枚举进程用 CreateToolhelp32Snapshot,Process32First,Process32Next
    可以得到程序名称并不是所有的程序都是有窗体的,对于有窗体的程序可以先用EnumWindows得到窗体句柄
    再用GetWindowThreadProcessId得到窗体所属进程ProcessId,然后用OpenProcess得到进程句柄
    用进程句柄查上面枚举出来的进程信息,就可以知道窗体所属进程其它信息
      

  2.   

    如果已知进程和窗口句柄,可以使用GetModuleFileName和GetWindowText函数得到进程文件名及窗口标题
      

  3.   

    //以下MEMO1列出进程名单uses  TlHelp32var
      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;
      

  4.   

    unit Unit1;interfaceuses
      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。