C# WebBrowser

 WebBrowser中的按钮调用WinForm中的事件:

        private void Form1_Load(object sender, EventArgs e)
        {
            this.webBrowser1.Navigate(Application.StartupPath + @"\HTMLPage1.htm");
        }
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            HtmlDocument htmlDocument = webBrowser1.Document;
            HtmlElement btnElement = htmlDocument.All["Button1"];

            if (btnElement != null)
            {
                btnElement.Click += new HtmlElementEventHandler(btnElement_Click);
            }
            btnElement = htmlDocument.All["Button2"];
            if (btnElement != null)
            {
                btnElement.Click += new HtmlElementEventHandler(btnElement_Click);
            }
        }

        public void btnElement_Click(object sender, HtmlElementEventArgs e)
        {
            HtmlElement element = sender as HtmlElement;

            if (element.Id == "Button1")
            {
                MessageBox.Show("第一个按钮");
          System.Diagnostics.Process.Start("explorer.exe", "http://www.baidu.com");
}
else if (element.Id == "Button2") { MessageBox.Show("第二个按钮");          System.Diagnostics.Process.Start(@"C:\Users\Fish\Desktop\Debug\WindowsFormsApplication1.exe");
} }

找到这个元素,然后为这个元素写一个响应事件

 

 

 

参考:

http://zhidao.baidu.com/question/391410267.html

你可能感兴趣的:(WebBrowser)