我想替换某个应用程序Edit的文本,我该...(100分)

sendmessage WM_SETTEXT 去设
 
IE 浏览器本身是一个COM服务程序, 可以用SHDocVw.dll中定义的IWebBrowser2得到<br>他的指针, 或者用CoInternerExplorer.Create来新建一个. 要对浏览器进行完全控<br>制, 可以Import MSHTML.DLL, 里面有所有的浏览器的各个元素的COM定义. 通过<br>MSHTML中的IHTMLDocument, 可以得到一个网页里面所有的详细内容.<br><br>Uses ComObj, MSDocW_TLB, MSHTML_TLB;<br><br>var<br>&nbsp; IE: IWebBrowser2;<br>&nbsp; IDOC: IHTMLDocument2;<br>&nbsp; Title: string;<br>procedure StartIE;<br>var Address, p1, p2, p3, p4: OleVariant;<br>begin<br>&nbsp; IE := CoInternetExplorer.Create; //启动IE浏览器<br>&nbsp; IE.Visible := true; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//显示浏览器<br>&nbsp; Address := 'www.microsoft.com'; &nbsp;//浏览地址<br>&nbsp; IE.Navigate(Address,p1,p2,p3,p4);//浏览<br>&nbsp; ......<br>&nbsp; IDOC := IE.Document; //获得Document<br>&nbsp; Title:= IDOC.Title; &nbsp;//取得网页的Title<br>&nbsp; ......<br>end;<br><br>procedure CloseIE;<br>begin<br>&nbsp; IE.Quit;<br>end;<br><br>要完全控制浏览器的内容, 不要忘记Implement浏览器DWebBrowserEvents2中的事件<br>DocumentComplete.
 
会长: 你的方法只能控制自己程序启动的IE实例吧? 如果是正常方法打开的IE呢?
 
简单一点的方法,先用getcursorpos()函数得到当前鼠标在屏幕上的坐标,<br>然后用函数windowfrompoint()得到窗口的句柄(可以是子窗口,例如edit,<br>button等),然后就可以向该窗口发送消息了。如有需要,我可以给你一个完<br>整的源程序。
 
化石: 问题是windowfrompoint无法得到html表单里面的edit句柄.
 
cAkk:我刚才试了一下,用windowfrompoint无法得到html表单里面的edit句柄,<br>暂时还没有其他的方法。不知你有何高招?
 
to cAkk:<br><br>哈哈, 你真令俺老人家生气, 告诉你们用IHTMLDocument就足够了. 这里给你们一个<br>简单的例子, 从IShellWindows(同样在SHDocVw.DLL中)提取IE, 以及其标题, URL.<br>至于想替换HTML中的Form, 甚至Script语言, 都可以在IHTMLDocument下做文章得<br>到.<br><br>想用Windows的API函数得到IE的窗口是根本不可能的, 因为IE的HTML窗口以及其中<br>的控件根本没有注册到Windows的ROT(running object table). 所有的信息都在<br>IHTMLDocument之中, 多花点时间研究一下吧.<br><br><br>Uses<br>&nbsp; MSDocW_TLB, MSHTML_TLB;<br><br>procedure TForm1.Button3Click(Sender: TObject);<br>var<br>&nbsp; ShellWindow: IShellWindows;<br>&nbsp; nCount: integer;<br>&nbsp; spDisp: IDispatch;<br>&nbsp; i: integer;<br>&nbsp; vi: OleVariant;<br>&nbsp; IE1: IWebBrowser2;<br>&nbsp; IDoc1: IHTMLDocument2;<br>begin<br>&nbsp; ShellWindow := CoShellWindows.Create;<br>&nbsp; nCount := ShellWindow.Count;<br><br>&nbsp; for i := 0 to nCount - 1 do<br>&nbsp; begin<br>&nbsp; &nbsp; vi := i;<br>&nbsp; &nbsp; spDisp := ShellWindow.Item(vi);<br>&nbsp; &nbsp; spDisp.QueryInterface( iWebBrowser2, IE1 );<br>&nbsp; &nbsp; //或者用: IE1 := spDisp as IWebBrowser2;<br>&nbsp; &nbsp; if IE1 &lt;&gt; nil then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; memo1.Lines.Add( 'Location: '+ie1.LocationName );<br><br>&nbsp; &nbsp; &nbsp; IDoc1 := IE1.Document as IHTMLDocument2;<br>&nbsp; &nbsp; &nbsp; if iDoc1 &lt;&gt; nil then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; memo1.Lines.add( 'Title: ' + IDOC1.Title );<br>&nbsp; &nbsp; &nbsp; &nbsp; memo1.Lines.add( 'Url: ' + IDOC1.url);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br>
 
我用会长的方法基本成功,但是有几个问题:<br>MSDocW_TLB 是从哪个DLL引入的? 我的机器上有一个SHDocVw_TLB, 我用他<br>代替到是编译成功了,但是这句话出错:<br>IDoc1 := IE1.Document as IHTMLDocument2;<br>出错信息为:<br>"interface not support"<br>
 
cAkk:<br>改成:<br>&nbsp; &nbsp; &nbsp; IE1.Document.QueryInterface(IHTMLDocument2,iDoc1);<br>就好了。<br>
 
cAkk:<br>&nbsp; &nbsp;我知道怎么回事了。如果用了ie5,那么打开本地目录,用的也是ie,可以得到<br>IE1(iWebBrowser2),但是这个IE1.Document不是IHTMLDocument2。<br>&nbsp; &nbsp;嗯,还是用QueryInterface比较安全。<br>
 
接受答案了.
 
顶部