如何读取窗口或控件中的内容?(100分)

  • 主题发起人 fulminate
  • 开始时间
F

fulminate

Unregistered / Unconfirmed
GUEST, unregistred user!
如题,有哪些办法读到一个窗口或控件中的数据?
 
读取窗口或控件的什么数据(如Edit编辑框中数据?)?是自己的程序还是外部的程序?如果是自己的程序,可以使窗口或控件属性读取,如:Edit1.Text等。如果是外部程序,则先取得外部窗口或控件的句柄(可以使用EnumWindow、FindWindow等函数获取窗口或控件句柄),然后用GetWindowText函数获取。
 
主要是edit中的数据,是外部程序的
 
在Win9x系统比较好办,只需使用hEdit := FindWindow('EditClassName','EditWindowName');找到Edit控件的句柄,然后用SendMessage(hEdit,WM_GETTEXT,sizeof(StrBuff),LongWord(@StrBuff));发送WM_GETTEXT消息就可以获取了。而在Win2k以上的系统,直接使用前面的方法就不行了,必须使用一个远程Hook Dll(比如可以使用WH_GETMESSAGE消息钩子)注入到相应的进程中,再使用前面的代码才能获取的Edit控件中的数据。
 
要获得 Edit 的文本用 GetWindowText 就可以了,麻烦在于怎么去获得该 Edit 的句柄。
关键是确定该 Edit 与其它控件的区分点,这样才能找到它的句柄。
又或者通过它的父窗体去获得它的句柄。
 
可以测试出那个EDIT的句柄,遍历那个窗体上的所有的EDIT,就会得到那个窗体的句柄,
 
多谢各位!
 
没特殊情况,这样即可
procedure TForm1.Button1Click(Sender: TObject);
begin
try
ShowMessage(TEdit(FindComponent('edit1')).Text);
except
end;
end;
 
如果是XP系统,你可以试试 AccessibleObjectFromPoint
 
另外的程序里,FORM上有很多个 Edit和 Combobox ,那个程序是自己的,可以知道所有控件的 名称。这种情况下,如何向特定的Edit 或者Combobox 发送文字 或者读取特定的Edit 或者 Combobox文字
 
首先用findwindow函数找到那个外部程序的句柄,然后使用遍历窗体上所有的控件,判断是否TEdit控件,若是的话就取得它的值。 可用EnumChildWindows函数来获得某一窗体上所有的控件句柄。
 
使用下面这个程序可以获得其他窗体上的Tedit控件的值。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
Button2: TButton;
ListBox2: TListBox;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
function EnumWindowsProc( Hwnd: HWND;
MyForm: TForm1): Boolean;
stdcall;
//回调函数
function EnumChildControlProc( Hwnd: HWND;
MyForm: TForm1): Boolean;
stdcall;
//回调函数, 处理返回来的窗体控件的句柄
implementation
{$R *.dfm}
function EnumWindowsProc( Hwnd: HWND;
MyForm: TForm1): Boolean;
stdcall;
var
WndWindowText: array[0..254] of char;
//用来存储窗口名
begin
GetWindowText(Hwnd,WndWindowText,254);
MyForm.ListBox1.Items.Add('窗口名: '+StrPas(WndWindowText));
Result:= True;
end;

function EnumChildControlProc( Hwnd: HWND;
MyForm: TForm1): Boolean;
stdcall;
var
ComponentClassName, ComponentText: array[0..254] of char;
Buffer:array[0..255]of char;
begin
GetClassName(Hwnd,ComponentClassName,254);
GetWindowText(Hwnd, ComponentText, 254);
if StrPas(ComponentClassName)='TEdit' then
begin
SendMessage(Hwnd,WM_GETTEXT,256,Longint(@Buffer));
//使用 WM_GetText消息获得Tedit的值.
MyForm.ListBox2.Items.Add('TEdit的内容: '+StrPas(Buffer));
end;
Result:= True;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ListBox1.Items.Clear;
EnumWindows(@EnumWindowsProc, Longint(self));
// 注册回调函数入口地址的函数
end;

procedure TForm1.Button2Click(Sender: TObject);
var
Hwn: HWND;
begin
Form1.ListBox2.Items.Clear;
ListBox2.Items.Add('有如下控件名称');
Hwn:= FindWindow(nil,PChar(Label2.Caption));
if Hwn<>0 then
EnumChildWindows(Hwn,@EnumChildControlProc,LongInt(Self))
else
MessageBox(self.Handle,'没有获得该窗口句柄','提示',0);
end;

procedure TForm1.ListBox1Click(Sender: TObject);
begin
Label2.Caption:=Copy(ListBox1.Items.Strings[ListBox1.itemIndex],9,Length(ListBox1.Items.Strings[ListBox1.itemIndex]));
end;

end.
 
zuodan的方法不错!
 
多谢各位的帮助!
 
多人接受答案了。
 
顶部