WebBrowser中html元素如何触发winform事件 z

只要注册一下事件就可以了。

C#代码如下:

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsApplication5
...{
  public partial class Form1 : Form
  ...{
    public Form1()
    ...{
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    ...{
      this.webBrowser1.Navigate("");
      this.webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
    }

    void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    ...{
      if (e.Url.ToString() != this.webBrowser1.Url.ToString()) return;
      foreach(System.Windows.Forms.HtmlElement el in this.webBrowser1.Document.GetElementsByTagName("input"))
      ...{
        if (el.Name == "btnG")
        ...{
          el.Click += new HtmlElementEventHandler(Form1_Click);
        }
      }

    }

    void Form1_Click(object sender, HtmlElementEventArgs e)
    ...{
      MessageBox.Show("jinjazz pass");
    }

  }
}

上一篇:C# webbrowser 多线程操作/跨线程操作


下一篇:C# (转载)webbrowser专题(参考资料:https://www.cnblogs.com/blogpro/p/11458390.html)