如何捕获WPF形式的网页(在WebBrowser控件中打开)的按钮单击事件?

发布于 2024-11-26 01:19:58 字数 160 浏览 0 评论 0原文

考虑一个场景,我在 WPF 应用程序中有一个 WebBrowser 控件。 网页在 WebBrowser 控件内加载。该网页包含一个按钮。 该网页是 ASP.NET 应用程序。

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

Consider a scenario where I have a WebBrowser Control in WPF application.
A web page is loaded inside WebBrowser Control. The web page contains a button.
The web page is of ASP.NET application.

I want to capture the button click event of the webpage into WPF Form (which hosts WebBrowser Control). Is there any way to achieve this functionality?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

粉红×色少女 2024-12-03 01:19:58

下面的代码应该完全按照您想要的方式进行操作,并带有注释来解释正在发生的事情:

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 MainWindow Parent;

        public void ButtonClicked()
        {
            //Do whatever you need to do.  For now, we'll just show a message box
            Parent.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{ Parent = this };
    }

    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

Here is code that should do exactly what you want with comments to explain what is going on:

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 MainWindow Parent;

        public void ButtonClicked()
        {
            //Do whatever you need to do.  For now, we'll just show a message box
            Parent.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{ Parent = this };
    }

    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);
    }
}

Some of this was taken from http://beensoft.blogspot.com/2010/03/two-way-interaction-with-javascript-in.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文