处理Web浏览器控件的click事件 [英] handling click event of web browser control

查看:110
本文介绍了处理Web浏览器控件的click事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加webBrowser控件的click事件。这是我的代码。

I want to add click event of webBrowser control. This is my code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        HtmlDocument htmlDoc; 
        public Form1()
        {
            InitializeComponent();
            OpenFileDialog open = new OpenFileDialog();
            open.ShowDialog();
            this.webBrowser1.Navigate(open.FileName);

        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (webBrowser1.Document != null)
            {
                htmlDoc = webBrowser1.Document;
                htmlDoc.Click += new HtmlElementEventHandler(htmlDoc_Click);
            }
        }
        private void htmlDoc_Click(object sender, HtmlElementEventArgs e)
        {
            MessageBox.Show("Click");
        }

    }
}

我要播放一个.ppt文件。可以显示,但是当我单击webBrowser时,没有显示任何消息框。还有其他解决方案吗?
谢谢

I want it to diplay a .ppt file. It is ok to display but when I click the webBrowser there is no messagebox show up. Is there any other solution? thanks

推荐答案

我正在使用 ObjectForScripting 做这些事情。它允许JavaScript调用C#方法。让JavaScript对事件做出反应,这更加容易,而且您不需要MSHTML。 此处。您需要使用System.Runtime.InteropServices 才能正常工作,以便应用程序知道ComVisible-Annotation。

I'm using ObjectForScripting to do these kind of things. It allows JavaScript to call a C#-Method. Let JavaScript react on events, thats much easier and you don't need MSHTML. It's nicely explained here. Youl'll need using System.Runtime.InteropServices; for it to work, so that the app knows the ComVisible-Annotation.

您不必是JavaScript专业人士即可使用。例如:只需添加一个这样的按钮:

You don't have to be a JavaScript-pro to use this. For example: Just add a button like this:

<button onclick="javascript:window.external.showPpt('test.ppt');">Click me</button>

这将在其中调用名为 showPpt 的方法 ObjectForScripting 。切记:您也可以使用C#创建HTML。这样,您可以将信息存储在文档中。这是一个完整的示例:

which will call a method named showPpt in the ObjectForScripting. Remember: You may create the HTML using C# too. This way you can store information in the document. Here is a full example:

public partial class frmBrowser : Form
{
    HtmlDocument doc;

    [ComVisible(true)]
    public class ScriptManager
    {
        private frmBrowser mForm;

        public ScriptManager(frmBrowser form)
        {
             mForm = form;
        }

        public void recall(string statusID)
        {
             mForm.RestoreStatus(statusID);
        }
    }

    public frmBrowser()
    {
        InitializeComponent();
        this.webBrowser.ObjectForScripting = new ScriptManager(this);
        string url = "file:///" + Application.StartupPath + "\\start\\start.html";
        this.webBrowser.Navigate(url);
    }

    private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        doc = webBrowser.Document;
        showRecent();
    }

    private void showRecent()
    {
        HtmlElement divRecent = doc.GetElementById("recent_cont");
        List<DaoStatus> status = Center.DB.GetUIStatus();
        string html = "";
        foreach (DaoStatus st in status)
        {
             html += "<button onclick=\"javascript:window.external.recall('" + st.ID + "');\">" + st.Name + "</button>";
        }
        divRecent.InnerHtml = html;
    }
}

webBrowser 控件导航到本地文件。如果调用了完全加载的 showRecent(),它将从数据库获取信息,并使用ID为 recent_cont的div元素相应地创建按钮。

The webBrowser control navigates to a local file. If fully loaded showRecent() is called, which gets information from a database and creates buttons accordingly using a div-Element with the id "recent_cont".

这篇关于处理Web浏览器控件的click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆