如何让WebBrowser获取单击事件

这个问题来自论坛提问,vs2005的webbrowser控件如何接收鼠标事件,很多事情其实自己动动脑子就有办法的。主要是3步,给dom对象插入js脚本去响应鼠标-〉通过url跳转去通知webbrowser-〉截获跳转事件去c#中处理


示例代码:

  1. using System;
  2. using System.Windows.Forms;
  3. using mshtml;
  4. using SHDocVw;
  5. namespace WindowsApplication2
  6. {
  7. public partial class Form1:Form
  8. {
  9. public Form1()
  10. {
  11. InitializeComponent();
  12. }
  13. private void Form1_Load( object sender,EventArgse)
  14. {
  15. this .webBrowser1.Navigating+= new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
  16. this .webBrowser1.Navigate( "http://www.google.com" );
  17. SHDocVw.WebBrowserwb= this .webBrowser1.ActiveXInstance as SHDocVw.WebBrowser;
  18. wb.NavigateComplete2+= new SHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(wb_NavigateComplete2);
  19. }
  20. void webBrowser1_Navigating( object sender,WebBrowserNavigatingEventArgse)
  21. {
  22. if (e.Url.ToString().ToLower().Trim( '/' )== "cmd://onmousedown" )
  23. {
  24. MessageBox.Show( "jinjazz路过" );
  25. e.Cancel= true ;
  26. }
  27. }
  28. void wb_NavigateComplete2( object pDisp, ref object URL)
  29. {
  30. mshtml.IHTMLDocument2doc=( this .webBrowser1.ActiveXInstance as SHDocVw.WebBrowser).Document as mshtml.IHTMLDocument2;
  31. doc.parentWindow.execScript( "document.onmousedown=function(e){window.location='cmd://onmousedown'}" , "javascript" );
  32. }
  33. }
  34. }

你可能感兴趣的:(JavaScript,c,windows,Google,脚本)