unit winHWND;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
ListBox1: TListBox;
Button2: TButton;
procedure Button1Click(Sender: TObject);
private
function EnumWndProc(AWnd: HWND; AlParam: LPARAM): Bool; stdcall;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
EnumWindows(@EnumWndProc,1);
end;
function TForm1.EnumWndProc(AWnd: HWND; AlParam: LPARAM): Bool;stdcall;
var
szText: array[0..254] of char;
begin
Result := True;
if IsWindowVisible(awnd) and (awnd<>Form1.Handle) then //要排除掉本窗口
begin
GetWindowText(awnd, @szText, 255);
if szText<>'' then
begin
listBox1.Items.Add(inttostr(awnd)+': '+strPas(@szText));
Result:=False;
end;
end;
end;
end.
这段代码展示了如何在Delphi应用程序中使用EnumWindows函数遍历并显示所有可见窗口的句柄及其标题。它定义了一个TForm1类,包含一个按钮,当点击按钮时,会触发遍历过程,将非本应用窗口的标题和句柄添加到ListBox中。
2036






