如何将鼠标事件侦听器附加到 C++ 中的嵌入式 nsIWebBrowser

发布于 2024-08-03 09:27:58 字数 675 浏览 4 评论 0原文

我已在我的应用程序中嵌入了 nsIWebBrowser。因为我只是动态生成 HTML,所以我使用 OpenStream、AppendToStream 和 CloseStream 来添加内容。我需要的是为鼠标在网络浏览器上移动以及鼠标单击添加事件侦听器。我已经阅读了文档并尝试了很多不同的方法,但我所尝试的都不起作用。例如,下面的代码似乎做了正确的事情,但它什么也没做:

    nsCOMPtr<nsIDOMWindow> domWindow;
    mWebBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));

    if (!mEventTarget) {
    mEventTarget = do_QueryInterface(domWindow);

    if (mEventTarget)
        mEventTarget->AddEventListener(NS_LITERAL_STRING("mouseover"), (nsIDOMEventListener *)mEventListener, PR_FALSE);
}

也许它不起作用,因为它是在初始化期间运行的,但在实际添加任何内容之前运行。但是,如果我在 AppendStream 或 CloseStream 期间添加它,则会出现段错误。

请告诉我一个简单的方法来做到这一点。

I've embedded an nsIWebBrowser in my application. Because I'm just generating HTML for it on the fly, I'm using OpenStream, AppendToStream, and CloseStream to add content. What I need is to add event listeners for mouse movement over the web browser as well as mouse clicks. I've read documentation and tried lots of different things, but nothing I have tried has worked. For instance, the code below would seem to do the right thing, but it does nothing:

    nsCOMPtr<nsIDOMWindow> domWindow;
    mWebBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));

    if (!mEventTarget) {
    mEventTarget = do_QueryInterface(domWindow);

    if (mEventTarget)
        mEventTarget->AddEventListener(NS_LITERAL_STRING("mouseover"), (nsIDOMEventListener *)mEventListener, PR_FALSE);
}

Perhaps it isn't working because this is run during initialization, but before any content is actually added. However, if I add it during AppendStream, or CloseStream, it segfaults.

Please tell me a straightforward way to do this.

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

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

发布评论

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

评论(1

故人如初 2024-08-10 09:27:58

好吧,答案如下:

nsCOMPtr<nsIDOMEventTarget> cpEventTarget;
nsCOMPtr<nsIDOMWindow> cpDomWin;
m_pWebBrowser->GetContentDOMWindow (getter_AddRefs(cpDomWin));
nsCOMPtr<nsIDOMWindow2> cpDomWin2 (do_QueryInterface (cpDomWin));
cpDomWin2->GetWindowRoot(getter_AddRefs(cpEventTarget));

rv = cpEventTarget->AddEventListener(NS_LITERAL_STRING("mousedown"),
                m_pBrowserImpl, PR_FALSE); 

Well, here's the answer:

nsCOMPtr<nsIDOMEventTarget> cpEventTarget;
nsCOMPtr<nsIDOMWindow> cpDomWin;
m_pWebBrowser->GetContentDOMWindow (getter_AddRefs(cpDomWin));
nsCOMPtr<nsIDOMWindow2> cpDomWin2 (do_QueryInterface (cpDomWin));
cpDomWin2->GetWindowRoot(getter_AddRefs(cpEventTarget));

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