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.

相关阅读 >>

Delphi 接口尚未调用尚未调用coinitialize解决办法

Delphi tdownloadurl下载网络文件

Delphi f1026 file not found: ''quickrpt.dcu''解决方法

Delphi xe新语法/新功能

Delphi xe将图标和图像添加为资源

Delphi编程实现图像的淡入浅出

Delphi 记录鼠标点击坐标

Delphi 内存分配 stralloc

Delphi 字串转16进制

Delphi tparallel.for 老外得示例代码

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



打赏

取消

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

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

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

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

评论

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