如何向其他程序窗口中某一个Edit控件里发送文本?(50分)

  • 主题发起人 主题发起人 w8u
  • 开始时间 开始时间
W

w8u

Unregistered / Unconfirmed
GUEST, unregistred user!
有一个用delphi编写的程序,其中有个窗体,上面有一个Edit控件.<br>现在我要编写另一个程序,在这个程序中动态的控制那个窗体上的Edit控件,<br>比如得到该控件里的值,向该控件里发送文本等.要怎么处理呢?<br>
 
用VB中的Tcomm控件;
 
我想通过delphi 或者api实现。实在没办法的话,TComm哪里有?
 
var<br>&nbsp; Handle,BTN,BTN2: Integer;<br>begin<br>&nbsp; Handle := FindWindow(nil,'XXXXXXX'); &nbsp;//就是窗口的Caption<br>&nbsp; if Handle&lt;&gt;0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp;//如果找到就是Handle,如果没找到Handle,还是窗口<br>&nbsp; &nbsp; &nbsp;//下面这句是测试<br>&nbsp; &nbsp; &nbsp; BTN:=FindWindowEx(Handle,0,'Edit',nil);<br>&nbsp; &nbsp; &nbsp; SendMessage(BTN,WM_SETTEXT,0,Integer(edit1.Text));<br>end
 
如果该文本框是IE的,怎么办?
 
to Rik:<br>Handle := FindWindow(nil,'XXXXXXX'); &nbsp;//就是窗口的Caption<br>if Handle&lt;&gt;0 then<br>begin<br>&nbsp; //如果找到就是Handle,如果没找到Handle,还是窗口<br>&nbsp; //下面这句是测试<br>&nbsp; BTN:=FindWindowEx(Handle,0,'Edit',nil);<br>&nbsp; SendMessage(BTN,WM_SETTEXT,0,Integer(edit1.Text));<br>end;<br><br>有个问题是那窗体上有很多个DBEdit,我怎么才能找到我想要的那个DBEdit呢?<br>
 
EnumChildWindow枚举所有子窗口,也就是控件之类的,然后设置每个Edit不同的值,看看<br>那个是你想要的,记下是第几次枚举到它的(估计你要设置的程序不会经常改变吧),就可已<br>了。
 
如果那窗体是hide的,可以找到吗?<br>是否一定要是当前窗体?
 
向另一程序发送WM_COPYDATA吧<br>var<br>cdstr: TCopyDataStruct;<br>H:THandle;<br>s:string;<br>begin<br>&nbsp; s:=Edit1.text;<br>&nbsp; cdstr.dwdata:=111;<br>&nbsp; cdstr.cbdata:=length(s)+1;<br>&nbsp; GetMen(cdstr.lpdata,cdstr.cbdata);<br>&nbsp; StrCopy(cdstr.lpdata,PChar(s));<br>&nbsp; h:=findwindow('被查窗体类名','窗体名');<br>&nbsp; if h&lt;&gt;0 then<br>&nbsp; &nbsp; sendmessage(h,wm_copydata,0,0);<br>&nbsp; freemem(cdstr.lpdata);<br>另一程序使用GetWindowText接收数据<br>var<br>s:string;<br>begin<br>setLength(s,100);<br>getwindowtext(Msg.form,PChar(s),1000);<br>s:=PChar(s);<br>Edit1.caption:=PChar(Msg.Copydatastruct.lpdata);<br>end;<br>
 
老大们,能来举例说明吗?
 
改一下Rik的代码。<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; Handle,BTN,BTN2: Integer;<br>begin<br>&nbsp; Handle := FindWindow(nil,'Form1'); &nbsp;//就是窗口的Caption<br>&nbsp; if Handle&lt;&gt;0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp;//如果找到就是Handle,如果没找到Handle,还是窗口<br>&nbsp; &nbsp; &nbsp;//下面这句是测试<br>&nbsp; &nbsp; &nbsp; BTN:=FindWindowEx(Handle,0,'TEdit','edit1');{看看这里}<br>&nbsp; &nbsp; &nbsp; SendMessage(BTN,WM_SETTEXT,0,lParam(memo1.Text));<br>end;<br>end;
 
同意楼上,就是调用Win32 API
 
SendMessage发送(wm_copy,wm_paste)消息
 
影子的代码:<br>&nbsp; &nbsp; BTN:=FindWindowEx(Handle,0,'TEdit','edit1');{看看这里}<br>可以找到TEdit控件的handle.但是怎么样向该控件发送字符串呢?<br>SendMessage(BTN,WM_SETTEXT,0,lParam(memo1.Text));<br>好象不行。<br><br>另外,如果控件是TDBEdit,结果就找不到了,怎么办? &nbsp; &nbsp; &nbsp;
 
如何得到TEdit里的字符串~~<br>帮忙成功的我会给分
 
如果有10个窗口标题名相同的窗体,那又怎么能挨个给每个窗体中的EDIT控件发文本那?<br>注意哦caption都相同啊
 
to 影 子:很感谢你.我已经找到一个窗体上的所有的控件的handle.<br>根据你的提示我做了如下的一个程序已经可以找到窗体上的控件类名,<br>但是还是不知道怎么样找到我想要的控件。<br>------------------------------------------------------------------------<br>unit Unit1;<br>interface<br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls;<br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Memo1: TMemo;<br>&nbsp; &nbsp; Label1: TLabel;<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>&nbsp; function &nbsp;EnumChildWndProc(AhWnd:HWnd;<br>&nbsp; &nbsp; AlParam:lParam):boolean;stdcall;<br><br>implementation<br><br>{$R *.dfm}<br>function &nbsp;EnumChildWndProc(AhWnd:HWnd;<br>&nbsp; AlParam:lParam):boolean;stdcall;<br>var<br>&nbsp; WndClassName: array[0..254] of Char;<br>begin<br>&nbsp; GetClassName(AhWnd,wndClassName,254);<br>&nbsp; with form1.memo1 do<br>&nbsp; begin<br>&nbsp; &nbsp; lines.add( string(wndClassName));<br>&nbsp; &nbsp; lines.add('-------');<br>&nbsp; end;<br>&nbsp; result:=true;<br>end;<br><br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; hWnd:LongInt;<br>begin<br>&nbsp; memo1.Lines.Clear;<br>&nbsp; Memo1.Lines.Add(Edit1.Text+' 有如下控件类名称');<br>&nbsp; hWnd:=FindWindow(nil,pchar(Edit1.Text));<br>&nbsp; if hWnd&lt;&gt;0 then<br>&nbsp; begin<br>&nbsp; &nbsp; EnumChildWindows(hWnd,@EnumChildWndProc,0);<br>&nbsp; end;<br>end;<br>end.<br>------------------------------------------------------------------<br><br>补充: <br>比如 <br>程序1: <br>&nbsp; 窗体标题: 职工信息 <br>&nbsp; 窗体上的控件: DBZGBH :TDBEdit <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DBZGXM :TDBEdit <br>&nbsp; &nbsp; &nbsp; &nbsp; ..... <br>现在编写程序2,要求得到DBZGBH的handle. &nbsp; <br>&nbsp;<br>
 
Rik, <br>说得对
 
后退
顶部