delphi 使用WindowsAPI回调函数EnumWindowsProc获取系统窗口列表


unit GetWinProcMainForm;
interface
 
uses
  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.

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值