使用回调函数,枚举窗口 子窗口
回调函数写出来不是自己的程序去调用的,反而是让其他的东西去调用,比如windows操作系统,比如其他的程序等等之类的。但是什么时候被调用却不知道了。回调函数一般是按照调用者的要求定义好参数和返回值的类型,你向调用者提供你的回调函数的入口地址,然后调用者有什么事件发生的时候就可以随时按照你提供的地址调用这个函数通知你,并按照预先规定好的形式传递参数。所以很多人打比方,说回调函数还真有点像您随身带的BP机:告诉别人号码,在它有事情时Call您!
function
EnumWindowsProc(AhWnd:LongInt;lParam:LongInt):boolean;stdcall;
function EnumWindows(lpEnumFunc: TFNWndEnumProc; lParam: LPARAM): BOOL; stdcall;
EnumWindows(@EnumWindowsProc,0);
function EnumWindowsProc(AhWnd:LongInt;lParam:LongInt):boolean;stdcall;
function GetWindowText(hWnd: HWND; lpString: PChar; nMaxCount: Integer): Integer; stdcall;
function GetClassName(hWnd: HWND; lpClassName: PChar; nMaxCount: Integer): Integer; stdcall;
为了显示窗口标题和类名,我们用一个TMemo控件。
function EnumWindowsProc(AhWnd:LongInt;AForm:TForm1):boolean;stdcall;
function EnumWindowsProc(AhWnd:LongInt;AForm:TForm1):boolean;
var
lpszClassName,lpszWindowText:array[0..254] of char; //定义两个缓冲区。
begin
GetWindowText(AhWnd,lpszWindowText,254);//得到窗口标题
GetClassName(AhWnd,lpszClassName,254);//得到窗口类名。
Aform.memo1.lines.add(StrPas(lpszWindowText));
Aform.memo1.lines.add(StrPas(lpszClassName));
Aform.memo1.lines.add('--------------------');
Result:=True;
end;
procedure TForm1.btn_listwindowClick(Sender: TObject);
begin
EnumWindows(@EnumWindowsProc,LongInt(self));
end;
程序清单如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
btn_listwindow: TButton;
procedure btn_listwindowClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
function EnumWindowsProc(AhWnd:LongInt;AForm:TForm1):boolean;stdcall;
implementation
{$R *.dfm}
function EnumWindowsProc(AhWnd:LongInt;AForm:TForm1):boolean;
var
lpszClassName,lpszWindowText:array[0..254] of char;
begin
GetWindowText(AhWnd,lpszWindowText,254);
GetClassName(AhWnd,lpszClassName,254);
Aform.memo1.lines.add(StrPas(lpszWindowText));
Aform.memo1.lines.add(StrPas(lpszClassName));
Aform.memo1.lines.add('--------------------');
Result:=True;
end;
procedure TForm1.btn_listwindowClick(Sender: TObject);
begin
EnumWindows(@EnumWindowsProc,LongInt(self));
end;
end.
F9,运行,看看结果。最好是F7单步跟踪调试一下,看看回调函数是怎么被调用的。
有了回调函数的概念及上面的例子,我们可以继续了。其实想要找到一个标题已知的窗口句柄,用一个API函数就可以了:FindWindow.
其函数原形是:
function FindWindow(lpClassName, lpWindowName: PChar): HWND; stdcall;
lpClassName:窗口类名.如果只知道标题,可以为空.窗口类名可以用很多工具获得.如winsignt32.
lpWindowName:窗口标题.
调用方式举例:
var wndhwnd:HWND;
wndhwnd:=FindWindow(nil,'某窗口标题');
if wndhwnd<>0 then file://找到此窗口句柄.
begin
xxxxx
end
else begin
MessageBox(self.handle,'没找到该窗口句柄','提示',0);
end;
function EnumChildWindows(hWndParent: HWND; lpEnumFunc: TFNWndEnumProc;lParam: LPARAM): BOOL; stdcall;
function EnumChildWndProc(AhWnd:LongInt;
AlParam:lParam):boolean;stdcall;
var
WndClassName: array[0..254] of Char;
WndCaption: array[0..254] of Char;
begin
GetClassName(AhWnd,wndClassName,254);
GetWindowText(aHwnd,WndCaption,254);
with form1.memo1 do
begin
lines.add( string(wndClassName));
lines.add( string(wndCaption));
lines.add('-------');
end;
result:=true;
end;
然后在一事件里调用EnumChildWindows函数.
procedure TForm1.Button1Click(Sender: TObject);
var
hWnd:LongInt;
begin
memo1.Lines.Clear;
Memo1.Lines.Add(Edit1.Text+' 有如下控件类名称');
hWnd:=FindWindow(nil,pchar(Edit1.Text));
if hWnd<>0 then
begin
EnumChildWindows(hWnd,@EnumChildWndProc,0);
end
else MessageBox(self.handle,'没找到该窗口句柄','提示',0);
end;
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo; file://用来显示找到的控件
Label1: TLabel;
Edit1: TEdit; file://输入标题.
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
function EnumChildWndProc(AhWnd:LongInt;
AlParam:lParam):boolean;stdcall;
implementation
{$R *.dfm}
function EnumChildWndProc(AhWnd:LongInt;
AlParam:lParam):boolean;stdcall;
var
WndClassName: array[0..254] of Char;
WndCaption: array[0..254] of Char;
begin
GetClassName(AhWnd,wndClassName,254);
GetWindowText(aHwnd,WndCaption,254);
with form1.memo1 do
begin
lines.add( string(wndClassName));
lines.add( string(wndCaption));
lines.add('-------');
end;
result:=true;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
hWnd:LongInt;
begin
memo1.Lines.Clear;
Memo1.Lines.Add(Edit1.Text+' 有如下控件类名称');
hWnd:=FindWindow(nil,pchar(Edit1.Text));
if hWnd<>0 then
begin
EnumChildWindows(hWnd,@EnumChildWndProc,0);
end
else MessageBox(self.handle,'没找到该窗口句柄','提示',0);
end;
end.
SendMessage(hWnd,WM_SETTEXT,0,LongInt(Pchar('哈哈!')));