我写了一个完整的例程,代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TFrmMain = class(TForm)
BtSend: TButton;
Label1: TLabel;
EdtCaption: TEdit;
Label2: TLabel;
EdtX: TEdit;
Label3: TLabel;
EdtY: TEdit;
EdtMsg: TEdit;
Label4: TLabel;
procedure BtSendClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FrmMain: TFrmMain;
implementation
{$R *.dfm}
procedure TFrmMain.BtSendClick(Sender: TObject);
var
fHd,Hd:HWND;
Pt:Tpoint;
R:Integer;
begin
fHd:=0;
Hd:=0;
fHd:=FindWindow(nil,Pchar(EdtCaption.text));//按标题找父窗口的句柄
if fHd=0 then
begin
ShowMessage('找不到标题为'+EdtCaption.Text+'的窗口!');
Exit;
end;
Pt.X:=StrToInt(EdtX.Text);
Pt.Y:=StrToInt(EdtY.Text);
Hd:=ChildWindowFromPoint(fHd,Pt);//按Pt指定的位置找目标Edit的句柄
if (Hd=0) or (Hd=fHd) then
begin
ShowMessage('在指定位置('+EdtX.Text+','+EdtY.Text+')找不到目标Edit!');
Exit;
end;
R:=SendMessage(Hd,WM_SETTEXT,0,Integer(Pointer(EdtMsg.Text)));
//改变目标Edit的内容
If Boolean(R)=true then
ShowMessage('发送成功!!')
else
ShowMessage('发送失败!');
end;
//Procedure
end.
如果还有问题,请跟贴!