好友
阅读权限10
听众
最后登录1970-1-1
|
{回调函数例子 For 枚举窗口}
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
btn1: TButton;
lst1: TListBox;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//利用EnumWindow函数枚举所有窗口
{ 枚举函数 C原型
BOOL EnumWindows(
WNDENUMPROC lpEnumFunc, // address of callback function
LPARAM lParam // application-defined value
);}
{ 枚举函数delphi原型
function EnumWindows
(lpEnumFunc: TFNWndEnumProc; lParam: LPARAM):
BOOL; stdcall;}
//定义回调函数
// WNDENUMPROC lpEnumFunc
//TFNWndEnumProc其实就是一个指针类型
//msdn定义
{BOOL CALLBACK EnumWindowsProc (
HWND hwnd, // handle of parent window
LPARAM lParam // application-defined value
);}
//回调函数类型定义
type
EnumProc = function (hwnd:THandle;
lp:Pointer): BOOLean; stdcall;
//兼容回调函数声明
function Gettitle (hwnd:THandle;
lp:Pointer): Boolean; stdcall;
//标题存放buffer
var
text:string;
begin
SetLength(text,100);
GetWindowText(hwnd,PChar(text),100);
Form1.lst1.Items.Add (IntToStr (hwnd)
+': '
+text);
Result := True;
end;
procedure TForm1.btn1Click(Sender: TObject);
var
Enumwindow_proc: EnumProc;
begin
lst1.Clear;
//回调函数指针给enum...
Enumwindow_proc := Gettitle;
EnumWindows(@Enumwindow_proc,0);
end;
end. |
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|