如果当前窗体form1中有各类控件:Tlable,Tedit,T...procedure Tform1.button1click(Sender:Tobject); var i:integer; begin for i:=0 to componentcounts-1 do if Tcomponent[i] is Tlable then Tlable(Tcomponent[i]).caption:=inttostr(i); end;这段代码是得到每个lable控件的序号,并把它赋值给各自的caption属性.你想要得到caption的话,可以从这段代码中得到启发.(这代码我没运行过,可能有拼错的,出错的话发我短消息吧)
for i:=0 to TForm(FindControl(AHandle)).componentcounts-1 do
To:VisualLion(狮子) 你的方法不行,编译不过
To:VisualLion(狮子)我又试了一下你的方法,当那个句柄为当前窗体的句柄就可以,即: for i:=0 to TForm(FindControl(Handle)).componentcounts-1 do 如将"Handle"替换成用FindWindow函数得到的其它应用程序的窗体句柄时就不行,即: Hwnd:=FindWindow(nil,'句柄练习'); for i:=0 to TForm(FindControl(Hwnd)).componentcounts-1 do
你在vcl版搜一下,回答了N遍了,懒得回答了
刚才写的,机子上没有Delphi,也就没有测试 var hMain:HWND;hChild:HWND;buffer:array[1..200] of char; begin hMain:=FindWindow(nil,'XXXXX'); hChild:=FindWindowEx(hMain,0,nil,nil); while hChild do begin GetWIndowText(hChild,@buffer,200); ListBox1.Items.Add(StrPas(@Buffer)); hChild:=FidWidowEx(hMain,hChild,nil,nil);//look for next control end; end;
To: Eastunfail(浴血雏龙)==(恶鱼杀手)谢谢你的回答。 我做了试验,窗体上有文本框、标签和按钮,你的方法只取到了按钮上Caption
能说详细点吗?最好能给个例子,谢谢
窗体上有些控件是没有句柄的,除非得到窗体实例,然后遍历COMPONENTS数组........
说的是个办法。
如果这个程序是Delphi写的,那么你只能得到 那些从TWinControl继承下来的控件(他都是windows标准的窗口类),其他的这办法不行,其他办法我也不知道,呵呵。
当然是delphi写了,你知道吗?能给个例子吗?
你的方法只适合于得到窗体实例的情况,可我只得到窗体句柄而已
我等你的好消息:)
TForm1 = class(TForm)
btn1: TButton;
lst1: TListBox;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
FIndexOfHandle:Integer;
FArrHandle:Array of THandle;
FStrings:TStrings;
function MyEnumChild(AHwnd: THandle; AParam: Cardinal):Boolean;stdcall;
implementation{$R *.dfm}
function MyEnumChild(AHwnd: THandle; AParam: Cardinal):Boolean;
var
lPChar:PChar;
str:string;
begin
SetLength(FArrHandle,FIndexofHandle+1);
FArrHandle[FIndexOfHandle] := AHwnd;
GetMem(lPChar,256);
GetClassName(AHwnd,lpchar,256);
FStrings.Add(lpChar);
FreeMem(lPChar);
Inc(FIndexOfHandle);
Result := True;
end;procedure TForm1.btn1Click(Sender: TObject);
var
lhwd:THandle;
begin
lhwd := FindWindow('TForm1','Form1');
if lhwd =0 then Exit;
FStrings := TStringList.Create;
if EnumChildWindows(lhwd,@MyEnumChild,110) then
begin
ShowMessage('Successed');
lst1.Items.Assign(FStrings);
end;
FStrings.Free;
end;
得到你需要的窗体上所有子窗口的类名及句柄,当然,没有句柄的界面元素是没有办法找出来的。
var
i:integer;
begin
for i:=0 to componentcounts-1 do
if Tcomponent[i] is Tlable then
Tlable(Tcomponent[i]).caption:=inttostr(i);
end;这段代码是得到每个lable控件的序号,并把它赋值给各自的caption属性.你想要得到caption的话,可以从这段代码中得到启发.(这代码我没运行过,可能有拼错的,出错的话发我短消息吧)
你的例子应该是得到了窗体的实例的情况下吧?可我只能得到窗体的句柄。因为我要读取的是另一个应用程序的某个窗体
for i:=0 to TForm(FindControl(AHandle)).componentcounts-1 do
你的方法不行,编译不过
for i:=0 to TForm(FindControl(Handle)).componentcounts-1 do 如将"Handle"替换成用FindWindow函数得到的其它应用程序的窗体句柄时就不行,即:
Hwnd:=FindWindow(nil,'句柄练习');
for i:=0 to TForm(FindControl(Hwnd)).componentcounts-1 do
var hMain:HWND;hChild:HWND;buffer:array[1..200] of char;
begin
hMain:=FindWindow(nil,'XXXXX');
hChild:=FindWindowEx(hMain,0,nil,nil);
while hChild do
begin
GetWIndowText(hChild,@buffer,200);
ListBox1.Items.Add(StrPas(@Buffer));
hChild:=FidWidowEx(hMain,hChild,nil,nil);//look for next control
end;
end;
我做了试验,窗体上有文本框、标签和按钮,你的方法只取到了按钮上Caption
先占个位子 明天给你贴代码 嘿嘿
我得代码只是当前进程的,没看好你的题目
for i := 0 to ComponentCount - 1 do
begin
if not (Components[i] is TControl) then Continue;
end;你要修改其他进程的控件可能不行的,会引发windows系统的一般保护性异常。
这个例子倒是可以参考一下:
http://www.csdn.net/develop/Read_Article.asp?Id=22661