www.pudn.com > getmsg.rar > Unit1.~pas, change:2010-04-09,size:3797b


unit Unit1; 
 
interface 
 
uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, RzButton, StdCtrls, TLHelp32, psapi, ExtCtrls, RzPanel; 
 
type 
  TForm1 = class(TForm) 
    RzPanel1: TRzPanel; 
    RzBitBtn1: TRzBitBtn; 
    Memo1: TMemo; 
    Edit1: TEdit; 
    Edit2: TEdit; 
    Button1: TButton; 
    Button2: TButton; 
    RzPanel2: TRzPanel; 
    ListBox1: TListBox; 
    Button3: TButton; 
    procedure RzBitBtn1Click(Sender: TObject); 
    procedure Button1Click(Sender: TObject); 
    procedure Button2Click(Sender: TObject); 
    procedure Button3Click(Sender: TObject); 
  private 
    { Private declarations } 
    function GetProcessPath(ProcessID: DWORD): string; 
     function   GetWindow1(btitle:string):boolean; 
  public 
    { Public declarations } 
  end; 
 
var 
  Form1: TForm1; 
 
implementation 
 
{$R *.dfm} 
 
procedure TForm1.RzBitBtn1Click(Sender: TObject); 
var  
  h: HWnd; 
  p: array[0..254] of char; 
begin 
  Memo1.Clear; 
  h := GetWindow(Handle, GW_HWNDFIRST); 
  while h <> 0 do begin 
    GetClassName(h, p, Length(p)); 
    Memo1.Lines.Add(p); 
    if SameText(p, Trim(Edit1.Text)) then begin 
      GetWindowText(h, p, Length(p)); 
      Edit2.Text := p; 
      //Exit; 
    end; 
    h := GetWindow(h, GW_HWNDNEXT); 
  end; 
end; 
 
 
function getExeName(AshModule: Cardinal): String; 
var 
  kk: Array[0..MAX_PATH - 1] of char; 
begin 
  windows.GetModuleFileName(AshModule, kk, MAX_PATH); 
  Result   :=   kk;    
end; 
 
procedure TForm1.Button1Click(Sender: TObject); 
begin 
  //Edit2.Text := getExeName; 
end; 
 
function TForm1.GetProcessPath(ProcessID: DWORD): string; 
var  
  Hand: THandle;  
  ModName: array[0..Max_Path - 1] of Char;  
  hMod: HModule;  
  n: DWORD;  
begin  
  Result := '';  
  Hand := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, ProcessID);  
  if Hand > 0 then  
  try  
    ENumProcessModules(Hand, @hMod, Sizeof(hMod), n);  
    if GetModuleFileNameEx(Hand, hMod, ModName, Sizeof(ModName)) > 0 then begin 
      //Result := ExtractFilePath(ModName); 
      Result := ModName; 
    end; 
  except  
  end; 
end; 
 
procedure TForm1.Button2Click(Sender: TObject); 
var  
  ProcessName: string; //进程名  
  ProcessID: integer; //进程表示符 
  ContinueLoop: BOOL;  
  FSnapshotHandle: THandle; //进程快照句柄  
  FProcessEntry32: TProcessEntry32; //进程入口的结构体信息  
begin  
  FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //创建一个进程快照  
  FProcessEntry32.dwSize := Sizeof(FProcessEntry32);  
  ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32); //得到系统中第一个进程  
//循环例举  
  while ContinueLoop do  
  begin  
    ProcessName := FProcessEntry32.szExeFile;  
    ProcessID := FProcessEntry32.th32ProcessID;  
    Listbox1.Items.add('应用程序名:' + ProcessName + '#进程ID:' + inttostr(ProcessID) + '路径:' + GetProcessPath(ProcessID));  
    ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32); 
  end; 
end; 
 
function TForm1.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 memo1.lines.add(title); 
    end; 
    winhandle:=GetWindow(winhandle,GW_HWNDNEXT); 
  end; 
     
  Freemem(title); 
  CloseHandle(winHandle); 
  result:=true; 
end;    
 
 
procedure TForm1.Button3Click(Sender: TObject); 
var 
  asbtitle:string; 
begin 
  GetWindow1(asbtitle); 
end; 
 
end.