请问怎么可以向另一个EXE中的edit赋值(40分)

  • 主题发起人 主题发起人 ali_yan
  • 开始时间 开始时间
A

ali_yan

Unregistered / Unconfirmed
GUEST, unregistred user!
请问怎么可以向另一个EXE中的edit赋值<br>我现在有一个delphi5写的EXE在运行。。当前窗口中有两个控件edit1,edit2我现在想写<br>一个程序给edit1赋值‘123’,给edit2赋值'456'应该怎么来做??<br><br><br>
 
1。Dll<br>2。SendMessage。这个比较省事。
 
找要被赋值的EDIT的句柄
 
SendMessage
 
我做过,晚上告诉你吧,程序放在单位了。:)
 
sendmessage or dde
 
新建一工程,有一FORM1和EDIT1和BUTTON1,代码如下:<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var ds:tcopydatastruct;<br>&nbsp; &nbsp; hd:thandle;<br>begin<br>&nbsp; ds.cbData :=length(edit1.text)+1;<br>&nbsp; getmem(ds.lpdata,ds.cbdata);<br>&nbsp; strcopy(ds.lpdata,pchar(edit1.text));<br>&nbsp; hd:=findwindow(nil,'form2');<br>&nbsp; if hd&lt;&gt;0 then<br>&nbsp; &nbsp; sendmessage(hd,wm_copydata,handle,cardinal(@ds))<br>&nbsp; else<br>&nbsp; &nbsp; showmessage('Not find the other form!');<br>&nbsp; freemem(ds.lpdata);<br>end;<br><br>end.<br><br>编译保存。<br><br>再建一工程,有一FORM2和EDIT1,代码如下:<br>unit Unit2;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm2 = class(TForm)<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; &nbsp; procedure mymessage(var t:twmcopydata);message wm_copydata;<br>&nbsp; end;<br><br>var<br>&nbsp; Form2: TForm2;<br><br>implementation<br><br>{$R *.DFM}<br>procedure Tform2.mymessage(var t:twmcopydata);<br>begin<br>&nbsp; edit1.text:='';<br>&nbsp; edit1.text:=strpas(t.copydatastruct^.lpdata);<br>end;<br>end.<br>编译保存。<br><br>执行上述两个可执行文件,按下程序一的BUTTON1,<br>程序一将把其EDIT1中的数据传送至程序二中的EDIT1中显示。
 
找到edit的句柄后,可以用sendkey(^+v),通过clipBoard将数据copy到Edit里
 
  我用SendMessage()发送数据到另一个用VC编的窗体上的EDIT时,虽然能改变显示数据,<br>但是当模拟对该窗体上的按钮做单击时以输出该值时,却发现输出内容为空,这是为什么?
 
注意:在win2k下只能用sendmessage,而不能用setwindowtext;<br>var<br>&nbsp; buffer:pchar;<br>&nbsp; getmem(buffer,100);<br>&nbsp; buffer:='hello world';<br>&nbsp; sendmessage(edit1.handle,wm_settext,0,buffer);
 
假如程序1是别人写的应用程序,程序2是自己写的,<br>我怎样可以通过程序2来给程序1的edit赋值???<br>
 
查找APP1的Form1的FormHandle: integer;<br>在Form1中循环查找Edit的EditHandle;<br>sendMessage(EditHandle,WM_SETTEXT,0,Integer(pchar('Your Mesaage')));<br><br>procudure &nbsp;DoSomeThing;<br>var<br>&nbsp; &nbsp;Handle,FormHandle: Integer;<br>&nbsp; &nbsp;tmpHandle: Integer;<br>&nbsp; &nbsp;FindFlag: Boolean;<br>&nbsp; &nbsp;editHandle, ButtonHandle: integer;<br>begin<br>&nbsp; &nbsp; Handle:=0; &nbsp;TmpHandle:=0;<br>&nbsp; &nbsp; edithandle:=0; &nbsp; &nbsp; buttonHandle:=0;<br>&nbsp; &nbsp; FindFlag:=False;<br>&nbsp; &nbsp; while FindFlag=False do<br>&nbsp; &nbsp; begin &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Handle := FindWindow(nil,'Form1Caption'); <br>&nbsp; &nbsp; &nbsp; &nbsp; if Handle&lt;&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FindFlag:=True;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tmpHandle := Handle;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FormHandle:=Handle;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;repeat<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EnumChildWindows(Handle,@GetButtonHandle,Integer(@Handle));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FButtonHandle := Handle;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;until FButtonHandle&lt;&gt;FormHandle;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;repeat<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EnumChildWindows(tmpHandle,@GetEditHandle,Integer(@tmpHandle));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FTextHandle := tmpHandle;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;until FTextHandle&lt;&gt;FormHandle;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SendMessage(FTextHandle,WM_SETTEXT,0,Integer(pchar('Your Message Here')));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SendMessage(FButtonHandle,WM_LBUTTONDOWN,0,0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SendMessage(FButtonHandle,WM_LBUTTONUP,0,0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; end;<br>end;<br><br>function GetEditHandle(hwnd: Integer; lparam: Longint):Boolean; stdcall;<br>var<br>&nbsp; &nbsp; buffer: array[0..255] of Char;<br>&nbsp; &nbsp; buffer1: array[0..255] of Char;<br>begin<br>&nbsp; &nbsp; Result := True; &nbsp; &nbsp;<br>&nbsp; &nbsp; GetClassName(hwnd,buffer,256); <br>&nbsp; &nbsp; if StrPas(Buffer)='Edit' then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; GetWindowText(hwnd,buffer1,100);<br>&nbsp; &nbsp; &nbsp; &nbsp; PInteger(lparam)^ := hwnd; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; Result:=False;<br>&nbsp; &nbsp; end;<br>end;<br><br>function GetButtonHandle(hwnd: Integer; lparam: Longint):Boolean; stdcall;<br>var<br>&nbsp; &nbsp; buffer: array[0..255] of Char;<br>&nbsp; &nbsp; buffer1: array[0..255] of Char;<br>begin<br>&nbsp; &nbsp; &nbsp;Result := True; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp;GetClassName(hwnd,buffer,256);<br>&nbsp; &nbsp; &nbsp;if StrPas(Buffer)='Button' then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetWindowText(hwnd,buffer1,100);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if buffer1 = '确定' then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PInteger(lparam)^ := hwnd;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result:=False; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp;end;<br>end;<br><br>待会儿再来
 
今天心情好,接着灌。<br>上面的代码本来是写在线程中的,改了一下,可能有错。<br>如'xx狂人'所说,App1是现成的,那就稍微麻烦一点,你可能需要下面的调试工具。<br>procedure TForm1.GetMousePosHwndAndClassName(Sender: TPoint); <br>var <br>&nbsp; &nbsp; hWnd: THandle; <br>&nbsp; &nbsp; aName: array [0..255] of char; <br>begin &nbsp;<br>&nbsp; &nbsp; hWnd := WindowFromPoint(Sender);<br>&nbsp; &nbsp; Label1.Caption := 'Handle : ' + IntToStr(hWnd); <br>&nbsp; &nbsp; if boolean(GetClassName(hWnd, aName, 256)) then <br>&nbsp; &nbsp; &nbsp; &nbsp;Label2.Caption := 'ClassName : ' + string(aName)<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; Label2.Caption := 'ClassName : not found';<br>end;<br><br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; &nbsp; Form1.FormStyle := fsStayOnTop; <br>&nbsp; &nbsp; Timer1.Interval := 50;<br>end;<br><br>procedure TForm1.Timer1Timer(Sender: TObject);<br>var <br>&nbsp; &nbsp;rPos: TPoint; <br>begin <br>&nbsp; &nbsp;if boolean(GetCursorPos(rPos)) then <br>&nbsp; &nbsp; &nbsp; &nbsp;GetMousePosHwndAndClassName(rPos); <br>end; <br><br>你先用这个工具抓一下App1的FormHandle, 记下来<br>再取Edit的EditHandle和ClassName,记下来<br>再取Button的ButtonHandle和ClassName,记下来<br>if StrPas(Buffer)='Edit' then<br>if StrPas(Buffer)='Button' then<br>以上的'Edit'和'Button'就是要改的ClassName; 如果App1是用Delphi做的,<br>那就是'TEdit'和'TButton'.<br><br>if buffer1 = '确定' then<br>'确定'要在调试时跟一下.<br><br>应该搞定了,打字好辛苦.<br>上面的代码大都是DFWS奉献出来的,感谢他们吧!<br><br>
 
FindWindow() and FindWindowEx //找到Edit的句柄vHandle<br>SendMessage(vHandle, WM_SETTEXT, 0, Integer(PChar('发送的字符')));
 
后退
顶部