使用下面这个程序可以获得其他窗体上的Tedit控件并清空它的值<br><br>unit Unit1;<br>interface<br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls;<br><br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> ListBox1: TListBox;<br> Button2: TButton;<br> ListBox2: TListBox;<br> Label1: TLabel;<br> Label2: TLabel;<br> procedure Button1Click(Sender: TObject);<br> procedure Button2Click(Sender: TObject);<br> procedure ListBox1Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br> function EnumWindowsProc( Hwnd: HWND; MyForm: TForm1): Boolean; stdcall; //回调函数<br> function EnumChildControlProc( Hwnd: HWND; MyForm: TForm1): Boolean; stdcall; //回调函数, 处理返回来的窗体控件的句柄<br><br>implementation<br><br>{$R *.dfm}<br><br>function EnumWindowsProc( Hwnd: HWND; MyForm: TForm1): Boolean; stdcall;<br>var<br> WndWindowText: array[0..254] of char; //用来存储窗口名<br>begin<br> GetWindowText(Hwnd,WndWindowText,254);<br> MyForm.ListBox1.Items.Add('窗口名: '+StrPas(WndWindowText));<br> Result:= True;<br>end;<br><br>function EnumChildControlProc( Hwnd: HWND; MyForm: TForm1): Boolean; stdcall;<br>var<br> ComponentClassName, ComponentText: array[0..254] of char;<br> Buffer:array[0..255]of char;<br>begin<br> GetClassName(Hwnd,ComponentClassName,254);<br> GetWindowText(Hwnd, ComponentText, 254);<br> if StrPas(ComponentClassName)='TEdit' then<br> begin<br> Buffer='';<br> SendMessage(Hwnd,WM_SETTEXT,0,Longint(@Buffer)); //使用 WM_SetText消息设置Tedit的值.<br> <br> end;<br> Result:= True;<br>end;<br><br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> ListBox1.Items.Clear;<br> EnumWindows(@EnumWindowsProc, Longint(self)); // 注册回调函数入口地址的函数<br>end;<br><br><br>procedure TForm1.Button2Click(Sender: TObject);<br>var<br> Hwn: HWND;<br>begin<br> Form1.ListBox2.Items.Clear;<br> ListBox2.Items.Add('有如下控件名称');<br> Hwn:= FindWindow(nil,PChar(Label2.Caption));<br> if Hwn<>0 then<br> EnumChildWindows(Hwn,@EnumChildControlProc,LongInt(Self))<br> else<br> MessageBox(self.Handle,'没有获得该窗口句柄','提示',0);<br>end;<br><br>procedure TForm1.ListBox1Click(Sender: TObject);<br>begin<br> Label2.Caption:=Copy(ListBox1.Items.Strings[ListBox1.itemIndex],9,Length(ListBox1.Items.Strings[ListBox1.itemIndex]));<br>end;<br><br>end.