Title: How to read the text of a TEdit from another application
Question: With this method you can read a TEdit from another application. For instance the TEdit containing the internet address from MS-IE.
Answer:
(* You'll need a form with a TEdit, a Timer and a Label on it.
1) Open two instances of this application.
2) Enter a text in the TEdit of application 1.
3) Just look at what you see in the label of application 2 if you go with the mouse over the TEdit of application 1
Do you get the point? *)
// Modified 28/02/2002
// Added WM_GETTEXT
// By WM_GETTEXT f.i. the passwordbox of Excel can be read
// Now the application also reads EditBox (not only TEdit, Edit and RichEdit)
// Added ChildWindowFromPoint (thanks to Scalabium)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Timer1: TTimer;
Procedure FormCreate(Sender : TObject);
Procedure FormClose(Sender: TObject; var Action: TCloseAction);
Procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Bo : Boolean;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
Edit1.PasswordChar := '*';
Bo := False;
Timer1.Interval := 250;
Timer1.Enabled := True;
Edit1.Text := '';
Label1.Caption := '';
end;
procedure TForm1.Timer1Timer(Sender: TObject);
Var
p : TPoint;
x,y,oldx,oldy : Integer;
s : String;
b : Array[0..255] of Char;
i,j,l : Integer;
h,oldh, h1 : Thandle;
begin
Timer1.Enabled := False;
oldx := 0;
oldy := 0;
oldh := 0;
Repeat
Application.ProcessMessages;
GetCursorPos(p);
x := p.x;
y := p.y;
h := WindowFromPoint(p);
h1 := ChildWindowFromPoint(h,p);
If h1 0 then
h := h1;
if (oldx x) or (oldy y) then
if h oldh then
Begin
FillChar(b,255,#0);
b[0] := #255;
j := integer(@b);
i := SendMessage(h,EM_GETPASSWORDCHAR,0,0); // no encryption
l := SendMessage(h,EM_GETLINE,0,j); // get line
If l = 0 then
SendMessage(h,WM_GETTEXT,sizeof(b),j);
SendMessage(h,EM_SETPASSWORDCHAR,i,0); // encrypt back
s := StrPas(b);
If s '' then
Label1.Caption := s; // show unencrypted line
oldh := h;
oldx := x;
oldy := y;
End;
Until Bo;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Bo := True;
end;
end.