如何获取其它程序Edit中的文本?(请高手帮忙改写完善一段代码) ( 积分: 100 )

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

ax518598

Unregistered / Unconfirmed
GUEST, unregistred user!
其它程序不是我自己编写的。在网上查到一段类似代码,但因自己太菜,不会改写,请高手帮忙改写一段代码:(假设另一程序标题为&quot;***管理系统&quot;,需要获取的Edit坐标为(80,40)-(160,60))<br>以下为网上找的感觉类似的代码:<br>porcedure 得到目标窗体的句炳;<br>var<br> &nbsp; hCurWindow,hwnds: HWnd;// 窗口句柄<br> &nbsp; WinText: array [0..255] of char;<br>begin<br> &nbsp; // 获取第一个窗口的句柄<br> &nbsp; hCurWindow := GetWindow(Handle, GW_HWNDFIRST);<br> &nbsp; while hCurWindow &amp;lt;&amp;gt; 0 do<br> &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; // 获取窗口的名称<br> &nbsp; &nbsp; &nbsp; if GetWindowText(hCurWindow, @WinText, 255)&amp;gt;0 then<br> &nbsp; &nbsp; &nbsp; if strpas(@wintext))='目标窗体的标题' then<br> &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hwnds:=hcurwindow;//储存句炳<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;跳出循环; <br> &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; &nbsp; listbox1.items.Add(StrPas(@WinText));<br> &nbsp; &nbsp; &nbsp; &nbsp; // 获取下一个窗口的句柄<br> &nbsp; &nbsp; &nbsp;hCurWindow:=GetWindow(hCurWindow, GW_HWNDNEXT);<br> &nbsp; &nbsp;end;<br>end;<br><br>function GetButtonHandle(hwnds: hwnd; lparam: Longint):Boolean; stdcall;<br>var<br> &nbsp; buffer: array[0..255] of Char;<br> &nbsp; buffer1: array[0..255] of Char;<br>begin<br> &nbsp; Result := True;<br> &nbsp; &nbsp; //得到目标窗口的控件<br> &nbsp; GetClassName(hwnd,buffer,256);<br> &nbsp; &nbsp; //目标窗口的目标控件<br> &nbsp; if StrPas(Buffer)='Button' then<br> &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; GetWindowText(hwnd,buffer1,100);<br> &nbsp; &nbsp; &nbsp; if buffer1 = '确定' then<br> &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PInteger(lparam)^ := hwnd; //得到目标控件的Hwnd(句柄)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result:=False;//终止循环<br> &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp;end;<br>end;<br><br>procedure TfrmMain.bbtnStartClick(Sender: TObject);<br>var<br> &nbsp; Handle: Integer;<br> &nbsp; tmpHandle: Integer;<br>begin<br> &nbsp; &nbsp;{取句柄}<br> &nbsp; Handle := FindWindow(nil,'对话模式');//就是窗口的Caption<br> &nbsp; if Handle&amp;lt;&amp;gt;0 then<br> &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp;tmpHandle := Handle;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//在这里循环取到想要的句柄为止 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (****此处不知如何改)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//取发送按钮的,FButtonHandle<br> &nbsp; &nbsp; &nbsp;EnumChildWindows(Handle,@GetButtonHandle,Integer(@Handle));<br> &nbsp; &nbsp; &nbsp;FButtonHandle := Handle;<br> &nbsp; &nbsp;end;<br>end;<br><br>procedure 控制按钮;<br>begin<br> &nbsp; SendMessage(FButtonHandle,WM_LBUTTONDOWN,0,0);<br> &nbsp; SendMessage(FButtonHandle,WM_LBUTTONUP,0,0);<br>end; <br> <br>
 
其它程序不是我自己编写的。在网上查到一段类似代码,但因自己太菜,不会改写,请高手帮忙改写一段代码:(假设另一程序标题为&quot;***管理系统&quot;,需要获取的Edit坐标为(80,40)-(160,60))<br>以下为网上找的感觉类似的代码:<br>porcedure 得到目标窗体的句炳;<br>var<br> &nbsp; hCurWindow,hwnds: HWnd;// 窗口句柄<br> &nbsp; WinText: array [0..255] of char;<br>begin<br> &nbsp; // 获取第一个窗口的句柄<br> &nbsp; hCurWindow := GetWindow(Handle, GW_HWNDFIRST);<br> &nbsp; while hCurWindow &amp;lt;&amp;gt; 0 do<br> &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; // 获取窗口的名称<br> &nbsp; &nbsp; &nbsp; if GetWindowText(hCurWindow, @WinText, 255)&amp;gt;0 then<br> &nbsp; &nbsp; &nbsp; if strpas(@wintext))='目标窗体的标题' then<br> &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hwnds:=hcurwindow;//储存句炳<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;跳出循环; <br> &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; &nbsp; listbox1.items.Add(StrPas(@WinText));<br> &nbsp; &nbsp; &nbsp; &nbsp; // 获取下一个窗口的句柄<br> &nbsp; &nbsp; &nbsp;hCurWindow:=GetWindow(hCurWindow, GW_HWNDNEXT);<br> &nbsp; &nbsp;end;<br>end;<br><br>function GetButtonHandle(hwnds: hwnd; lparam: Longint):Boolean; stdcall;<br>var<br> &nbsp; buffer: array[0..255] of Char;<br> &nbsp; buffer1: array[0..255] of Char;<br>begin<br> &nbsp; Result := True;<br> &nbsp; &nbsp; //得到目标窗口的控件<br> &nbsp; GetClassName(hwnd,buffer,256);<br> &nbsp; &nbsp; //目标窗口的目标控件<br> &nbsp; if StrPas(Buffer)='Button' then<br> &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; GetWindowText(hwnd,buffer1,100);<br> &nbsp; &nbsp; &nbsp; if buffer1 = '确定' then<br> &nbsp; &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PInteger(lparam)^ := hwnd; //得到目标控件的Hwnd(句柄)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result:=False;//终止循环<br> &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp;end;<br>end;<br><br>procedure TfrmMain.bbtnStartClick(Sender: TObject);<br>var<br> &nbsp; Handle: Integer;<br> &nbsp; tmpHandle: Integer;<br>begin<br> &nbsp; &nbsp;{取句柄}<br> &nbsp; Handle := FindWindow(nil,'对话模式');//就是窗口的Caption<br> &nbsp; if Handle&amp;lt;&amp;gt;0 then<br> &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp;tmpHandle := Handle;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//在这里循环取到想要的句柄为止 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (****此处不知如何改)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//取发送按钮的,FButtonHandle<br> &nbsp; &nbsp; &nbsp;EnumChildWindows(Handle,@GetButtonHandle,Integer(@Handle));<br> &nbsp; &nbsp; &nbsp;FButtonHandle := Handle;<br> &nbsp; &nbsp;end;<br>end;<br><br>procedure 控制按钮;<br>begin<br> &nbsp; SendMessage(FButtonHandle,WM_LBUTTONDOWN,0,0);<br> &nbsp; SendMessage(FButtonHandle,WM_LBUTTONUP,0,0);<br>end; <br> <br>
 
procedure GetWindowHandle;<br>var <br> &nbsp;MainHandle:THandle;<br>begin<br> &nbsp;MainHandle:= Findwindow(nil,'***管理系统')//此处的caption可以sqy++获得。<br> &nbsp;if MainHandle &amp;lt;&amp;gt; 0 then <br> &nbsp; &nbsp;windows.EnumChildWindows(MainHandle,@GetEditHandle,0);<br>end; &nbsp;<br> <br>Function GetEditHandle(hwnd:hwnd; lparam:LPARAM):boolean;<br>var<br> &nbsp;MyStr: Pchar;<br> &nbsp;//MyHandle: array[0..255] of THandle;<br> &nbsp;ClassName: array[0..255] of char;<br>begin<br> &nbsp;GetClassName(hwnd,@ClassName[0],255);<br> &nbsp;if className= 'Edit' then <br> &nbsp;begin <br> &nbsp; &nbsp;sendmessage(hwnd,wm_settext,10,integer(MyStr));<br> &nbsp; &nbsp;listview1.items.add(Mystr);<br> &nbsp;end;<br> &nbsp;result:= true; &nbsp;<br> &nbsp;<br>end; &nbsp;<br> &nbsp;
 
minglove,你好,我按你的修改后仍没有正常结果,我后来查了GetEditHandle(hwnd:hwnd; lparam:LPARAM):boolean;函数,显示得知 MyStr 是空的,ClassName 值为&quot;4?&quot;,不知为什么。(我另外用Delphi写了一个很简单的Form,上面只有一个Name为&quot;Edit&quot;的文本框,Form.Caption为&quot;***管理系统&quot;);<br>
 
我通过下面的函数都能运行正常,但我仍取不出文本框中输入的字符串,不知道该用什么函数。([red]解决了该问题就结题了[/red])<br>function GetButtonHandle(hwnds: hwnd; lparam: Longint):Boolean; stdcall;<br>var<br> &nbsp; buffer: array[0..255] of Char;<br> &nbsp; buffer1: array[0..255] of Char;<br>begin<br> &nbsp; GetClassName(hwnds,buffer,256); &nbsp; <br> &nbsp; if StrPas(Buffer)='TEdit' then &nbsp; &nbsp; <br> &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; GetWindowText(hwnds,buffer1,100);<br> &nbsp; &nbsp; &nbsp; if buffer1 = 'Edit888' then &nbsp;//Edit888为一个Edit的Name,我在该框中输入‘1234’时,我希望取出它们(1234)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Form1.Memo1.Lines.Add(buffer1);<br> &nbsp; &nbsp; &nbsp; if buffer1 = 'Edit999' then &nbsp;//Edit999为另一个Edit的Name<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Form1.Memo1.Lines.Add(buffer1);<br> &nbsp; &nbsp; &nbsp; Result := True;<br> &nbsp; &nbsp; end;<br>end;<br>
 
据我从前所知<br>EDIT中的内容属于要用地址获取的那种,然而如果是其他程序的控件,是在其进程的地址空间中无法访问<br>更绝的是,我发现用API返回的指针是NULL。<br>更进一步的我没有实验,不知先用OPENPROCESS打开进程可否得到有效地址<br>继续的话我想还需要用一个什么函数打开进程的地址空间,够麻烦的
 
郁闷无比,查Delphi的Help,发现GetWindowText函数其它的内容都能取出来,唯独就Edit的输入内容不能取出来,哪位高手能指点一下,用其它方式能取出来吗?
 
不是edit特殊,是你的句柄没取对<br>var<br> &nbsp;h:thandle;<br> &nbsp;a:array[0..255] of char;<br><br>h:= Findwindow(nil,'***管理系统');<br>if h&amp;lt;&amp;gt;0 then<br>begin<br> &nbsp;h:=findwindowex(h,0,'tedit',nil);<br> &nbsp;if h&amp;lt;&amp;gt;0 then<br> &nbsp; begin<br> &nbsp; &nbsp; &nbsp;getwindowtext(h,a,256);<br> &nbsp; &nbsp; &nbsp;showmessage(a);<br> &nbsp; end;<br>end;<br>
 
请教各位:能否知道另一个程序中的两个Edit,究竟当前是哪一个Edit处理焦点状态?(即输入光标停留在其上面),给人感觉应该能取出来,因为在WinSight32中光标处于不同的Edit上会有不同的变化。看来只能和键盘钩子结合我才能取出它们的值了。
 
多人接受答案了。
 
后退
顶部