delphi 使用WindowsAPI回调函数EnumWindowsProc获取系统窗口列表
本文地址转载请保留:http://blog.csdn.net/sushengmiyan/article/details/8623874
{-----------------------------------------作者:sushengmiyan 2013.02.28备注:仅供学习交流使用博客:http://blog.csdn.net/sushengmiyan功能:枚举系统窗口-----------------------------------------}unit GetWinProcMainForm;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) mmo: TMemo; btn1: TButton; procedure btn1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1; function EnumWindowsProc(hwnd: HWND; lParam: LPARAM): Boolean ;stdcall;implementation{$R *.dfm}procedure TForm1.btn1Click(Sender: TObject);begin// EnumWindows 专用的回调函数的格式:// function EnumWindowsProc(// hwnd: HWND; {找到的窗口句柄}// lParam: LPARAM {EnumWindows 传给的参数; 因为它是指针, 可传入, 但一般用作传出数据}// ): Boolean; stdcall; {函数返回 False 时, 调用它的 EnumWindows 将停止遍历并返回 False} Form1.mmo.Clear; EnumWindows(@EnumWindowsProc ,0);end;function EnumWindowsProc(hwnd: HWND; lParam: LPARAM): Boolean ;stdcall;var WindowText : string ; // 窗体标题 begin if ( IsWindowVisible(hwnd) or IsIconic(hwnd) ) and ( (GetWindowLong(hwnd, GWL_HWNDPARENT) = 0) or (GetWindowLong(hwnd, GWL_HWNDPARENT) = Longint(GetDesktopWindow)) )and ( GetWindowLong(hwnd, GWL_EXSTYLE) and WS_EX_TOOLWINDOW = 0 ) then {-----标题文字------} begin SetLength(WindowText, GetWindowTextLength(hwnd)+2); Getwindowtext(hwnd, Pchar(WindowText), GetWindowTextLength(hwnd)+2); WindowText := string( Pchar(WindowText)); Form1.mmo.Lines.Add(WindowText); end; Result := True; end;// EnumWindows 的功能是遍历所有顶层窗口// function EnumWindows(// lpEnumFunc: TFNWndEnumProc; {回调函数指针}// lParam: LPARAM {给回调函数的参数, 它对应回调函数的第二个参数}// ): BOOL; stdcall; //成功与否, 其实是返回了回调函数的返回值end.
先上源码。再解释
主要使用的windowsAPI回调含函数EnumWindows。本文列举了系统的窗口。并将标题输出到了memo中