.NET WebBrowser 控件 - 获取活动上下文菜单

发布于 2024-11-16 03:58:01 字数 68 浏览 4 评论 0原文

我有 WebBrowser 控件,我希望当单击右键并且上下文菜单出现时获取该上下文菜单的句柄。

这可能吗?

I have WebBrowser control and i want when right button is clicked and context menu appear to get the handle of that context menu.

is that possible?

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

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

发布评论

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

评论(2

浅忆流年 2024-11-23 03:58:01

是的。

您可以参考以下代码。

    //this code assumes WebBrowser object(_webBrowser) is already initiated
    //in class scope.

    //this method is needed to execute when form is loaded.
    //Register it to load event
    private void Loaded(object sender, RoutedEventArgs e)
    {
        _webBrowser.LoadCompleted += _webBrowser_LoadCompleted;
    }

    private HTMLDocumentEvents2_Event _docEvent;

    private void _webBrowser_LoadCompleted(object sender, NavigationEventArgs e)
    {
        if (_docEvent != null)
        {
            _docEvent.oncontextmenu -= new HTMLDocumentEvents2_oncontextmenuEventHandler(_docEvent_oncontextmenu);
        }
        if (_webBrowser.Document != null)
        {
            _docEvent = (HTMLDocumentEvents2_Event)_webBrowser.Document;
            _docEvent.oncontextmenu += new HTMLDocumentEvents2_oncontextmenuEventHandler(_docEvent_oncontextmenu);
        }
    }

    bool _docEvent_oncontextmenu(IHTMLEventObj pEvtObj)
    {
        //do something and determine you want whether context menu shows or not
        //if you want to shows context menu, you'll need to return true.
        return true;
    }

Yes.

You could reference the following code.

    //this code assumes WebBrowser object(_webBrowser) is already initiated
    //in class scope.

    //this method is needed to execute when form is loaded.
    //Register it to load event
    private void Loaded(object sender, RoutedEventArgs e)
    {
        _webBrowser.LoadCompleted += _webBrowser_LoadCompleted;
    }

    private HTMLDocumentEvents2_Event _docEvent;

    private void _webBrowser_LoadCompleted(object sender, NavigationEventArgs e)
    {
        if (_docEvent != null)
        {
            _docEvent.oncontextmenu -= new HTMLDocumentEvents2_oncontextmenuEventHandler(_docEvent_oncontextmenu);
        }
        if (_webBrowser.Document != null)
        {
            _docEvent = (HTMLDocumentEvents2_Event)_webBrowser.Document;
            _docEvent.oncontextmenu += new HTMLDocumentEvents2_oncontextmenuEventHandler(_docEvent_oncontextmenu);
        }
    }

    bool _docEvent_oncontextmenu(IHTMLEventObj pEvtObj)
    {
        //do something and determine you want whether context menu shows or not
        //if you want to shows context menu, you'll need to return true.
        return true;
    }
羁绊已千年 2024-11-23 03:58:01

如果您只想显示自己的 contextMenu。我在这里发布了一个适用于 winforms WebBrowser 控件的解决方案:

如何覆盖右键单击 winforms WebBrowser 时出现的上下文菜单控制?

If all you want to display your own contextMenu instead. I posted a solution here that works for a winforms WebBrowser control:

How do you override the ContextMenu that appears when right clicking on winforms WebBrowser Control?

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