WPF webbrowser control vs winforms

可紊 提交于 2019-12-01 05:54:54
A P

The way I have done this is...

Download HTML text of the page you want to render using HTTPRequest. Inject java script using HTML agility pack in HTML text. If you want to use jQuery then you have to jQuerify your page first and then bind event with your dom elements. You can also call your c# function from within the script and other way around. No messing around with dynamic types and hence no exception.

You can also suppress script error in your WC using extension method on this link.

This and this may help.

Use the following namespace that way you can get to all element properties and eventhandler properties:

    using mshtml;

    private mshtml.HTMLDocumentEvents2_Event documentEvents;
    private mshtml.IHTMLDocument2 documentText;

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)
    {
        documentText = (IHTMLDocument2)webBrowserChat.Document; //this will access the document properties as needed
        documentEvents = (HTMLDocumentEvents2_Event)webBrowserChat.Document; // this will access the events properties as needed
        documentEvents.onkeydown += webBrowserChat_MouseDown;
        documentEvents.oncontextmenu += webBrowserChat_ContextMenuOpening;
    }

    private void webBrowser_MouseDown(IHTMLEventObj pEvtObj)
    {
         pEvtObj.returnValue = false; // Stops key down
         pEvtObj.returnValue = true; // Return value as pressed to be true;
    }

    private bool webBrowserChat_ContextMenuOpening(IHTMLEventObj pEvtObj)
    {
        return false; // ContextMenu wont open
        // return true;  ContextMenu will open
        // Here you can create your custom contextmenu or whatever you want
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!