如何将事件侦听器添加到用 C# 为 IE BHO 创建的按钮?

发布于 2024-11-25 07:02:40 字数 713 浏览 0 评论 0原文

在我的 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< /a>) 我应该是清楚的。

我还看到了这个线程,但对于我想做的事情来说似乎有点矫枉过正,而且我无法让它工作。

In my IE BHO I create a input button element using:

    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

When I try to call button.addEventListener I get a 'mshtml.IHTMLElement' does not contain a definition for 'addEventListener' error. I find this odd because according to this site (http://help.dottoro.com/ljeuqqoq.php) I should be in the clear.

I also saw this thread but it seems like overkill for what I'm trying to do and I can't get it to work.

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

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

发布评论

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

评论(1

罪#恶を代价 2024-12-02 07:02:40

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

/// 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

Finally got it to work. Had to use similar method to the thread I linked in original question.

/// 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
        {
        }

    }

Inside my OnDocumentComplete method (also in same namespace as above [extra info for novices]):

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

A lot of work just to get a button to click. In firefox it was one line :O

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