如何获取WebBrowser内的鼠标坐标 ( 积分: 100 )

  • 主题发起人 主题发起人 PROSE
  • 开始时间 开始时间
P

PROSE

Unregistered / Unconfirmed
GUEST, unregistred user!
我在From内放了一个WebBrowser控件<br>使用getcursorpos可以得到屏幕坐标,但是到了Webbrowser内就得不到了,只能得到整个控件的边缘坐标,<br>代码如下:<br>procedure TMain_Form.Panel1MouseMove(Sender: TObject; Shift: TShiftState;<br> &nbsp;X, Y: Integer);<br><br> &nbsp;var P :TPoint ;<br>begin<br><br> &nbsp;getcursorpos(p) ;<br> &nbsp;Label5.Caption :=inttostr(p.x) +' &nbsp;, ' + inttostr(p.y) ;<br>end;<br>我想得到Webbrowser上鼠标点击的具体坐标,如何做??
 
我在From内放了一个WebBrowser控件<br>使用getcursorpos可以得到屏幕坐标,但是到了Webbrowser内就得不到了,只能得到整个控件的边缘坐标,<br>代码如下:<br>procedure TMain_Form.Panel1MouseMove(Sender: TObject; Shift: TShiftState;<br> &nbsp;X, Y: Integer);<br><br> &nbsp;var P :TPoint ;<br>begin<br><br> &nbsp;getcursorpos(p) ;<br> &nbsp;Label5.Caption :=inttostr(p.x) +' &nbsp;, ' + inttostr(p.y) ;<br>end;<br>我想得到Webbrowser上鼠标点击的具体坐标,如何做??
 
关键是你想做什么,如果是点击里面的东西比较好处理。<br><br>一般控件里面都有个类似ClientToScreen这样的方法
 
用javascript可以得到,你在网页里写上script,再想办法传出来
 
我只想记录鼠标在Webbrowser内的位置。<br>所以只能先取相对屏幕,或者Panel的坐标,在计算了
 
实现HTMLDocumentEvents2就能截获这些消息,从以前找到的:<br><br>satanmonkey (2003-07-10 9:47:00) &nbsp;<br>首先找一个类实现IDispatch接口<br>就叫classA吧。<br>其次得到TWebBrowser的IWebBrowser接口<br>IE := WebBrowser1 as IWebbrowser2;<br>然后<br>InterfaceConnect(IE, HTMLDocumentEvents2, classA, FConnection);<br>在classA的Invoke函数里<br>就可以截获很多事件,包括<br> &nbsp;HTMLDocumentEvents2 = dispinterface<br> &nbsp; &nbsp;['{3050F613-98B5-11CF-BB82-00AA00BDCE0B}']<br> &nbsp; &nbsp;function &nbsp;onhelp(const pEvtObj: IHTMLEventObj): WordBool; dispid -2147418102;<br> &nbsp; &nbsp;function &nbsp;onclick(const pEvtObj: IHTMLEventObj): WordBool; dispid -600;<br> &nbsp; &nbsp;function &nbsp;ondblclick(const pEvtObj: IHTMLEventObj): WordBool; dispid -601;<br> &nbsp; &nbsp;procedure onkeydown(const pEvtObj: IHTMLEventObj); dispid -602;<br> &nbsp; &nbsp;procedure onkeyup(const pEvtObj: IHTMLEventObj); dispid -604;<br> &nbsp; &nbsp;function &nbsp;onkeypress(const pEvtObj: IHTMLEventObj): WordBool; dispid -603;<br> &nbsp; &nbsp;procedure onmousedown(const pEvtObj: IHTMLEventObj); dispid -605;<br> &nbsp; &nbsp;procedure onmousemove(const pEvtObj: IHTMLEventObj); dispid -606;<br> &nbsp; &nbsp;procedure onmouseup(const pEvtObj: IHTMLEventObj); dispid -607;<br> &nbsp; &nbsp;procedure onmouseout(const pEvtObj: IHTMLEventObj); dispid -2147418103;<br> &nbsp; &nbsp;procedure onmouseover(const pEvtObj: IHTMLEventObj); dispid -2147418104;<br> &nbsp; &nbsp;procedure onreadystatechange(const pEvtObj: IHTMLEventObj); dispid -609;<br> &nbsp; &nbsp;function &nbsp;onbeforeupdate(const pEvtObj: IHTMLEventObj): WordBool; dispid -2147418108;<br> &nbsp; &nbsp;procedure onafterupdate(const pEvtObj: IHTMLEventObj); dispid -2147418107;<br> &nbsp; &nbsp;function &nbsp;onrowexit(const pEvtObj: IHTMLEventObj): WordBool; dispid -2147418106;<br> &nbsp; &nbsp;procedure onrowenter(const pEvtObj: IHTMLEventObj); dispid -2147418105;<br> &nbsp; &nbsp;function &nbsp;ondragstart(const pEvtObj: IHTMLEventObj): WordBool; dispid -2147418101;<br> &nbsp; &nbsp;function &nbsp;onselectstart(const pEvtObj: IHTMLEventObj): WordBool; dispid -2147418100;<br> &nbsp; &nbsp;function &nbsp;onerrorupdate(const pEvtObj: IHTMLEventObj): WordBool; dispid -2147418099;<br> &nbsp; &nbsp;function &nbsp;oncontextmenu(const pEvtObj: IHTMLEventObj): WordBool; dispid 1023;<br> &nbsp; &nbsp;function &nbsp;onstop(const pEvtObj: IHTMLEventObj): WordBool; dispid 1026;<br> &nbsp; &nbsp;procedure onrowsdelete(const pEvtObj: IHTMLEventObj); dispid -2147418080;<br> &nbsp; &nbsp;procedure onrowsinserted(const pEvtObj: IHTMLEventObj); dispid -2147418079;<br> &nbsp; &nbsp;procedure oncellchange(const pEvtObj: IHTMLEventObj); dispid -2147418078;<br> &nbsp; &nbsp;procedure onpropertychange(const pEvtObj: IHTMLEventObj); dispid -2147418093;<br> &nbsp; &nbsp;procedure ondatasetchanged(const pEvtObj: IHTMLEventObj); dispid -2147418098;<br> &nbsp; &nbsp;procedure ondataavailable(const pEvtObj: IHTMLEventObj); dispid -2147418097;<br> &nbsp; &nbsp;procedure ondatasetcomplete(const pEvtObj: IHTMLEventObj); dispid -2147418096;<br> &nbsp; &nbsp;procedure onbeforeeditfocus(const pEvtObj: IHTMLEventObj); dispid 1027;<br> &nbsp;end;<br><br>具体怎么实现,参考BHO方面的内容,本方法应该是可行的,只是我没具体做过。在BHO里倒是做过。<br>另外你去http://www.euromind.com/iedelphi/index.htm看看,对你有帮助 <br> <br>不过我还没弄明白怎么搞出来。。。。。下午再试试
 
有没有简单点的方法?
 
WebBrowser 的消息都发到 Application 去了。加一个 ApplicationEvents 控件,在 OnMessage 中写:<br>procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;<br> &nbsp;var Handled: Boolean);<br>var<br> &nbsp;p: TPoint;<br>begin<br> &nbsp;if Msg.message = WM_MOUSEMOVE then<br> &nbsp;begin<br> &nbsp; &nbsp;p := ScreenToClient(Msg.pt);<br> &nbsp; &nbsp;Label5.Caption := Inttostr(p.x) + ', ' + Inttostr(p.y);<br> &nbsp;end;<br> &nbsp;{如果要取得 WebBrowser 的点击坐标,这么写:<br> &nbsp;if (Msg.hwnd = WebBrowser1.Handle) and (Msg.message = WM_LBUTTONDOWN) then<br> &nbsp;begin<br> &nbsp; &nbsp;p := ScreenToClient(Msg.pt);<br> &nbsp; &nbsp;ShowMessage(Inttostr(p.x) + ', ' + Inttostr(p.y));<br> &nbsp;end;}<br>end;<br>不知道这个算不算简单。
 
嘿嘿,不简单 楼上的代码如果webbrowser打开了一个网页就无法获得消息了,你可以试试<br>这是个取巧的办法<br>看看我这个吧,和楼上的一样,不过在打开网页后也能获得坐标,刚试完:<br><br>procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;<br> &nbsp;var Handled: Boolean);<br>var<br> &nbsp;p: TPoint;<br>begin<br> &nbsp;if IsChild(WebBrowser1.Handle, Msg.Hwnd) then &nbsp; &nbsp;//关键是这个<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp;p := webbrowser1.ScreenToClient(Msg.pt);<br> &nbsp; &nbsp;if Msg.message = WM_MOUSEMOVE then<br> &nbsp; &nbsp; &nbsp;Label4.Caption := Inttostr(p.x) + ', ' + Inttostr(p.y)<br> &nbsp; &nbsp;else if Msg.message = WM_LBUTTONDOWN then<br> &nbsp; &nbsp; &nbsp;label5.Caption:=Inttostr(p.x) + ', ' + Inttostr(p.y);<br> &nbsp; &nbsp;end;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>WebBrowser1.Navigate('http://www.163.com');<br>end;
 
楼上几位的方法不错,不过结果是错的,因为那个位置是form的坐标,不是网页的坐标<br><br>我的想法是能不能利用window对象的event。还没有试验成功,在hs的基础上改的<br><br>if msg.message = wm_mousemove then begin<br> &nbsp; &nbsp; &nbsp; &nbsp;if WebBrowser1.Document &lt;&gt; nil then begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;win:=IHTMLWindow2(IHTMLDocument2(WebBrowser1.Document).ParentWindow);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IHTMLWindow2(WebBrowser1.Document).focus ;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if Assigned(win.event) then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Label1.Caption := Inttostr(win.event.x) + ', ' + Inttostr(win.event.y) ;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; end;<br>其中<br>var<br> &nbsp;win :IHTMLWindow2;
 
我试过的,是webbrowser的坐标啊,难道你指的网页坐标是指相对整个页面的?<br><br>那估计只能用HTMLDocumentEvents2 来实现了。。。。
 
是webbrowser的坐标,但是鼠标左键点击是整个Form的点击都截获了,能不能只截获Webbrowser的区域的鼠标点击
 
.......我上面发的帖子里的东西你试了没???!!!<br>这个只获取webbrowser的点击!!!<br><br>procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;<br> &nbsp;var Handled: Boolean);<br>var<br> &nbsp;p: TPoint;<br>begin<br> &nbsp;if IsChild(WebBrowser1.Handle, Msg.Hwnd) then &nbsp; &nbsp;//关键是这个<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp;p := webbrowser1.ScreenToClient(Msg.pt);<br> &nbsp; &nbsp;if Msg.message = WM_MOUSEMOVE then<br> &nbsp; &nbsp; &nbsp;Label4.Caption := Inttostr(p.x) + ', ' + Inttostr(p.y)<br> &nbsp; &nbsp;else if Msg.message = WM_LBUTTONDOWN then<br> &nbsp; &nbsp; &nbsp;label5.Caption:=Inttostr(p.x) + ', ' + Inttostr(p.y);<br> &nbsp; &nbsp;end;<br>end;
 
后退
顶部