Custom context menu for WPF WebBrowser Control

后端 未结 4 544
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-21 15:02

Hi I need to create a custom context menu for a web browser control in wpf. Here is my xaml code which is not working:



        
4条回答
  •  鱼传尺愫
    2021-01-21 15:24

    The XAML as follows:

    
        
            
                
                    
                
            
    

    Code as follows:

    using mshtml;
    
    private mshtml.HTMLDocumentEvents2_Event documentEvents;
    

    in constructor or xaml set your LoadComplete event:

    webBrowser.LoadCompleted += webBrowser_LoadCompleted;
    

    then in that method create your new webbrowser document object and view the available properties and create new events as follows:

    private void webBrowser_LoadCompleted(object sender, NavigationEventArgs e)
    {
        documentEvents = (HTMLDocumentEvents2_Event)webBrowserChat.Document; // this will access the events properties as needed
        documentEvents.oncontextmenu += webBrowserChat_ContextMenuOpening;
    }
    
    private bool webBrowserChat_ContextMenuOpening(IHTMLEventObj pEvtObj)
    {
        wbContextMenu.PlacementTarget = pEvtObj as ContextMenu; // Creates target spot where contextmenu will appear
        wbContextMenu.IsOpen = true; // Opens contextmneu
        return false; // ContextMenu wont open
        // return true;  ContextMenu will open
        // Here you can create your custom contextmenu or whatever you want
    }
    

提交回复
热议问题