我想遍历其他程序窗体上的控件procedure TForm1.Button2Click(Sender: TObject); var hParent: HWND; i:integer; begin hParent := FindWindow(nil, 'MyForm'); for i:=0 to TForm(hParent).ControlCount-1 do if TForm(hParent).Controls[i] is TEdit then memo1.Lines.Add(TEdit(TForm(hParent).Controls[i]).Text); end;不行耶????
var TheForm: TForm; TheEditText: string; begin TheForm := FindControl(Handle: HWnd): TWinControl; for i := 0 to TheForm.ComponentCount do begin if TheForm.Components[i] is TEdit then //是EDIT begin TheEditText := TEdit(TheForm.Components[i]).Text; //得到内容 end; end;
TForm(hParent) 就有問題了, 你實際運行試試
//×××好像有问题耶××× var TheForm: TForm; TheEditText: string; begin TheForm := FindControl(Handle: HWnd): TWinControl; //×××好像有问题耶××× for i := 0 to TheForm.ComponentCount do begin if TheForm.Components[i] is TEdit then //是EDIT begin TheEditText := TEdit(TheForm.Components[i]).Text; //得到内容 end; end;
大家注意一下,你们可能没看清楼主的意思,要遍历的窗体对我们的程序来说是不可见的(不能直接访问其内存),对吧,楼主? var hCurrentWindow:HWND; szText: array[0..255] of char; buffer: array[0..255] of char; begin hCurrentWindow := GetWindow(yourHandle, GW_HWNDFIRST);//yourhandle 你得到的窗体句柄 while hCurrentWindow <> 0 do begin GetClassName(hCurrentWindow, @szText, 255); if Strpas(@szText) = 'TEdit' then begin GetWindowText(hCurrentWindow, @szText, 255); EnumChildWindows(hCurrentWindow,@EnumChildWindowsProc,Integer(@buffer[0])); showmessage(StrPas(buffer)); end; hCurrentWindow:=GetWindow(hCurrentWindow,GW_HWNDNEXT); end; end; 方案绝对可行,你自己调试一下。
Form.ComponentCount;
Components[i]
來查詢得到你想要的!
var
hParent: HWND;
i:integer;
begin
hParent := FindWindow(nil, 'MyForm');
for i:=0 to TForm(hParent).ControlCount-1 do
if TForm(hParent).Controls[i] is TEdit then
memo1.Lines.Add(TEdit(TForm(hParent).Controls[i]).Text);
end;不行耶????
TheForm: TForm;
TheEditText: string;
begin
TheForm := FindControl(Handle: HWnd): TWinControl;
for i := 0 to TheForm.ComponentCount do
begin
if TheForm.Components[i] is TEdit then //是EDIT
begin
TheEditText := TEdit(TheForm.Components[i]).Text; //得到内容
end;
end;
var
TheForm: TForm;
TheEditText: string;
begin
TheForm := FindControl(Handle: HWnd): TWinControl; //×××好像有问题耶×××
for i := 0 to TheForm.ComponentCount do
begin
if TheForm.Components[i] is TEdit then //是EDIT
begin
TheEditText := TEdit(TheForm.Components[i]).Text; //得到内容
end;
end;
var
hCurrentWindow:HWND;
szText: array[0..255] of char;
buffer: array[0..255] of char;
begin
hCurrentWindow := GetWindow(yourHandle, GW_HWNDFIRST);//yourhandle 你得到的窗体句柄
while hCurrentWindow <> 0 do
begin
GetClassName(hCurrentWindow, @szText, 255);
if Strpas(@szText) = 'TEdit' then
begin
GetWindowText(hCurrentWindow, @szText, 255);
EnumChildWindows(hCurrentWindow,@EnumChildWindowsProc,Integer(@buffer[0]));
showmessage(StrPas(buffer));
end;
hCurrentWindow:=GetWindow(hCurrentWindow,GW_HWNDNEXT);
end;
end;
方案绝对可行,你自己调试一下。