findwindow(nil,'程序名称');//得到窗体句柄 enumwindows(@函数名,0);//枚举所有窗体中的子窗体 unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; Button2: TButton; ListBox1: TListBox; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1; h:hwnd; implementation{$R *.dfm} procedure pro(var h:hwnd;LPARAM:lParam); //回调函数 begin if h<>0 then form1.ListBox1.Items.Add(inttostr(h));//加入到LISTBOX1 end;procedure TForm1.Button1Click(Sender: TObject); begin h:=findwindow(nil,'form1'); EnumChildWindows(form1.Handle,@pro,0);//枚举窗口中的子窗体 end;end.
如果就是想控制某一个按钮的话可以看这个例子 var h:hwnd; h:=findwindow(nil,'form1');//找到 sendmessage(h,WM_CLOSE,0,0);//关闭
var Handle, ChildHandle: HWND; clsName: pchar; text:array[0..255] of char; begin getmem(clsName, 255); Handle := FindWindow(pchar('SciCalc'), nil); if Handle <> 0 then begin ChildHandle := GetWindow(Handle, GW_Child); while ChildHandle <> 0 do begin GetClassName(ChildHandle, clsName, 255); //showmessage(clsName); if string(clsName) = 'Static' then begin getwindowtext(ChildHandle,text,255); showmessage(text); end; ChildHandle := GetWindow(ChildHandle, GW_HWNDNEXT); end; end; freemem(clsName, 255); end;
Wnd:Hwnd
ExeTitle: pchar
Wnd := FindWindow(nil, ExeTitle);
button直接取Handle就可以。
Wnd := button.handle;
enumwindows(@函数名,0);//枚举所有窗体中的子窗体
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
h:hwnd;
implementation{$R *.dfm}
procedure pro(var h:hwnd;LPARAM:lParam); //回调函数
begin
if h<>0 then
form1.ListBox1.Items.Add(inttostr(h));//加入到LISTBOX1
end;procedure TForm1.Button1Click(Sender: TObject);
begin
h:=findwindow(nil,'form1');
EnumChildWindows(form1.Handle,@pro,0);//枚举窗口中的子窗体
end;end.
var h:hwnd;
h:=findwindow(nil,'form1');//找到
sendmessage(h,WM_CLOSE,0,0);//关闭
Handle, ChildHandle: HWND;
clsName: pchar;
text:array[0..255] of char;
begin
getmem(clsName, 255);
Handle := FindWindow(pchar('SciCalc'), nil);
if Handle <> 0 then
begin
ChildHandle := GetWindow(Handle, GW_Child);
while ChildHandle <> 0 do
begin
GetClassName(ChildHandle, clsName, 255);
//showmessage(clsName);
if string(clsName) = 'Static' then
begin
getwindowtext(ChildHandle,text,255);
showmessage(text);
end;
ChildHandle := GetWindow(ChildHandle, GW_HWNDNEXT);
end;
end;
freemem(clsName, 255);
end;
findwindow(nil,'程序名称');//得到窗体句柄
enumwindows(@函数名,0);//枚举所有窗体中的子窗体
刚才写的:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
TreeView1: TTreeView;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure EnumChildWindows(Node:TTreeNode;hParent:hwnd);
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
function _hasChild(handle:hwnd):boolean;
begin
Result:=FindWindowEx(handle,0,nil,nil)<>0;
end;
procedure TForm1.EnumChildWindows(Node:TTreeNode;hParent:hwnd);
var hWindow:hwnd;s:PChar;child:TTreeNode;
begin
hWindow:=FindWindowEx(hParent,0,nil,nil);
while hWindow>0 do
begin
GetMem(s,500);
GetClassName(hWindow,s,500);
child:=TreeView1.Items.AddChild(Node,Format('%x [%s]',[hWindow,String(s)]));
freemem(s);
if _hasChild(hWindow) then
EnumChildWindows(child,hWindow);
hWindow:=FindWindowEx(hParent,hWindow,nil,nil);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var t:TTreeNode;hWindow:hwnd;s:String;
begin
TreeView1.Items.Clear ;
hWindow:=FindWindow(nil,'Win32 SDK/API - Microsoft Internet Explorer');
SetLength(s,200);
GetClassName(hWindow,PChar(s),200);
t:=TreeView1.Items.AddFirst(nil,s);
EnumChildWindows(t,hWindow);
end;end.
EnumChildWindows(form1.Handle,@pro,0);//枚举窗口中的子窗体
Delphi中很多控件是没有handle,比如TLable就是一个典型。要对他发送消息肯定不行,楼上各位对于没有handle控件如何对他们进行操作