delphi EnumWindows 获取窗体句柄 进程ID 窗体信息  
官方Delphi 学习QQ群: 682628230(三千人)\n
频道

delphi EnumWindows 获取窗体句柄 进程ID 窗体信息


delphi  EnumWindows 获取窗体句柄 进程ID 窗体信息 

// EnumWindows 的功能是遍历所有顶层窗口
function EnumWindows(
  lpEnumFunc: TFNWndEnumProc; {回调函数指针}
  lParam: LPARAM              {给回调函数的参数, 它对应回调函数的第二个参数}
): BOOL; stdcall; //成功与否, 其实是返回了回调函数的返回值

// EnumWindows 专用的回调函数的格式:
function EnumWindowsProc(
  hwnd: HWND;        {找到的窗口句柄}
  lParam: LPARAM     {EnumWindows 传给的参数; 因为它是指针, 可传入, 但一般用作传出数据}
): Boolean; stdcall; {函数返回 False 时, 调用它的 EnumWindows 将停止遍历并返回 False}


注以下代码在 delphi 2006版本中测试通过
准备如下 1:在窗体上添加一个listbox  两个按钮


单元代码:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  // 窗口信息
  TwindowStruct = record
   hwnd: HWND;
   classname: string;
  end;
  TArrWindowStruct = array of TwindowStruct;
  PwindowStruct = ^TArrWindowStruct;

  // 进程信息
  TProcessStruct = record
   hwnd,pid: DWORD;
  end;
  TArrProcessStruct = array of TProcessStruct;
  PArrProcessStruct = ^TArrProcessStruct;

  TForm1 = class(TForm)
    lst1: TListBox;
    btn1: TButton;
    btn2: TButton;
    procedure btn2Click(Sender: TObject);
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


//猎取insert的句柄 和标题
function EnumWindowsProc_1(hwnd: HWND;lparam: LPARAM):boolean;stdcall;
var
 buf, classBuf : array[Byte] of Char;
 pws: PwindowStruct;
begin
  GetWindowText(hwnd, buf, SizeOf(buf));
  GetClassName(hwnd, classBuf, SizeOf(buf));

  pws := PwindowStruct(lparam);
  SetLength(pws^, Length(pws^) + 1);
  pws^[High(pws^)].hwnd := hwnd;
  pws^[High(pws^)].classname := buf;

//  if buf<>'' then
//   form1.lst1.Items.Add(IntToStr(hwnd) + ' 窗体标题:' + buf + ' 类名为:' + classBuf)
//  else
//   form1.lst1.Items.Add(IntToStr(hwnd) + ' 没有窗体标题:'+ ' 没有类名');

  Result := true;
end;



//猎取insert的句柄 进程id
function EnumWindowsProc_2(hwnd: HWND;lparam: LPARAM):boolean;stdcall;
var
 pid : DWORD;
 pMyPS: PArrProcessStruct;
 buf: array[Byte] of Char;
begin
  GetWindowThreadProcessId(hwnd,pid);

  GetWindowText(hwnd, buf, SizeOf(buf));
//  GetClassName(hwnd, classBuf, SizeOf(buf));

  pMyPS := PArrProcessStruct(lparam);
  SetLength(pMyPS^, Length(pMyPS^) + 1);
  pMyPS^[High(pMyPS^)].hwnd := hwnd;
  pMyPS^[High(pMyPS^)].pid := pid;

//  if buf<>'' then
//   form1.lst1.Items.Add(IntToStr(hwnd) + ' 窗体标题:' + buf + ' 类名为:' + classBuf)
//  else
//   form1.lst1.Items.Add(IntToStr(hwnd) + ' 没有窗体标题:'+ ' 没有类名');

  Result := true;
end;


procedure TForm1.btn1Click(Sender: TObject);
var
 myWindowStruct: TArrWindowStruct;
 i: Integer;
begin
// EnumWindows(@EnumWindowsProc_1, 0);
 EnumWindows(@EnumWindowsProc_1, Integer(@myWindowStruct));

 for i := Low(myWindowStruct) to High(myWindowStruct) do
 begin
  lst1.Items.Add(Format('%d -类名 %s', [myWindowStruct[i].hwnd, myWindowStruct[i].classname]));

 end;



end;

procedure TForm1.btn2Click(Sender: TObject);
var
 temp : TArrProcessStruct;
 i :integer;
begin

 EnumWindows(@EnumWindowsProc_2,Integer(@temp));
 for i := Low(temp) to High(temp) do
 begin
  lst1.Items.Add(Format('%d -进程ID %D', [temp[i].hwnd, temp[i].pid]));

 end;

end;

end.

推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.066943883895874 seconds