【问题标题】:How to capture button click event of webpage (opened inside WebBrowser Control) in WPF form?如何以 WPF 形式捕获网页的按钮单击事件(在 WebBrowser 控件中打开)?
【发布时间】:2011-07-26 09:44:45
【问题描述】:

假设我在 WPF 应用程序中有一个 WebBrowser 控件。 在 WebBrowser 控件中加载了一个网页。该网页包含一个按钮。 该网页是 ASP.NET 应用程序。

我想将网页的按钮单击事件捕获到 WPF 表单(托管 WebBrowser 控件)中。有没有办法实现这个功能?

谢谢,

塔盘

【问题讨论】:

    标签: asp.net wpf webbrowser-control buttonclick event-capturing


    【解决方案1】:

    这里的代码应该完全按照您的要求使用 cmets 来解释发生了什么:

    public partial class MainWindow : Window
    {
    
        /// <summary>
        /// This is a helper class.  It appears that we can't mark the Window as ComVisible
        /// so instead, we'll use this seperate class to be the C# code that gets called.
        /// </summary>
        [ComVisible(true)]
        public class ComVisibleObjectForScripting
        {
            public void ButtonClicked()
            {
                //Do whatever you need to do.  For now, we'll just show a message box
                MessageBox.Show("Button was clicked in web page");
            }
        }
    
        public MainWindow()
        {
            InitializeComponent();
            //Pass an instance of our helper class as the target object for scripting
            webBrowser1.ObjectForScripting = new ComVisibleObjectForScripting();
        }
    
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //Navigate to your page somehow
            webBrowser1.Navigate("http://www.somewhere.com/");
        }
    
        private void webBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
        {
            //Once the document is loaded, we need to inject some custom JavaScript.
    
            //Here is the JavaScript
            var javascript = @"
    //This is the JavaScript method that will forward the click to the WPF app
    function htmlButtonClicked()
    {
        //Do any other procession...here we just always call to the WPF app
        window.external.ButtonClicked();
    }
    
    //Find the button that you want to watch for clicks 
    var searchButton = document.getElementById('theButton');
    
    //Attach an onclick handler that executes our function
    searchButton.attachEvent('onclick',htmlButtonClicked);
    ";
    
            //Grab the current document and cast it to a type we can use
            //NOTE: This interface is defined in the MSHTML COM Component
            //       You need to add a Reference to it in the Add References window
            var doc = (IHTMLDocument2)webBrowser1.Document;
    
            //Once we have the document, execute our JavaScript in it
            doc.parentWindow.execScript(javascript);
        }
    }
    

    部分内容来自http://beensoft.blogspot.com/2010/03/two-way-interaction-with-javascript-in.html

    【讨论】:

    • 我不断抛出异常:HRESULT 0x80020101。
    猜你喜欢
    • 2012-07-01
    • 2015-08-07
    • 2017-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-17
    相关资源
    最近更新 更多