如何在C#for IE BHO中创建的按钮中添加事件监听器?

时间:2011-07-19 22:39:06

标签: c# internet-explorer bho mshtml

在我的IE BHO中,我使用:

创建一个输入按钮元素
    var button = doc.createElement("input");
    button.setAttribute("value", "myButton"); //next line gets an error
    button.addEventListener("click", openFunction, false); //openFunction is a function inside of the same class

当我尝试调用button.addEventListener时,我得到'mshtml.IHTMLElement'不包含'addEventListener'错误的定义。我发现这很奇怪,因为根据这个网站(http://help.dottoro.com/ljeuqqoq.php)我应该清楚。

我也看到了this线程,但是对于我正在尝试做的事情似乎有些过分,我无法让它发挥作用。

1 个答案:

答案 0 :(得分:2)

终于开始工作了。不得不使用类似于我在原始问题中链接的线程的方法。

/// Inside the BHO.cs file and in my namespace
/// Generic HTML DOM Event method handler.
///
public delegate void DHTMLEvent(IHTMLEventObj e);

///
/// Generic Event handler for HTML DOM objects.
/// Handles a basic event object which receives an IHTMLEventObj which
/// applies to all document events raised.
///
public class DHTMLEventHandler
{
    public DHTMLEvent Handler;
    HTMLDocument Document;

    public DHTMLEventHandler(HTMLDocument doc)
    {
        this.Document = doc;
    }
    [DispId(0)]
    public void Call()
    {
        Handler(Document.parentWindow.@event);
    }
}
    public void BrowserEventHandler(IHTMLEventObj e)
    {
        try
        {
            if (e.type == "click" && e.srcElement.id == "IDOfmyButton")
            {
                // do something.
                something();
            }
        }
        catch
        {
        }

    }

在我的OnDocumentComplete方法中(也在上面相同的命名空间中[新手的额外信息]):

            DHTMLEventHandler myHandler = new DHTMLEventHandler(framedoc);
            myHandler.Handler += new DHTMLEvent(this.BrowserEventHandler);
            button.onclick = myHandler;

很多工作只是为了点击按钮。在Firefox中它是一行:O