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

问题描述

| 我有WebBrowser控件,我想单击右键时出现上下文菜单以获取该上下文菜单的句柄。 那可能吗?     

解决方法

        是。 您可以参考以下代码。
    //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;
    }
    ,        如果所有您想显示自己的contextMenu。我在这里发布了适用于winforms WebBrowser控件的解决方案: 右键单击winforms WebBrowser控件时,如何覆盖出现的ContextMenu?     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...