CEF Xilium.cefglue Open All links in the current window (prevents pop-up windows)

Source: Internet
Author: User

we are using Xilium.cefglue When writing a browser application, the user experience will be poor for embedded Web pages if a link opens in a new window, so we need to modify the program so that all links are opened in the current window.


First referencing the Xilium.CefGlue.dll file, you need to inherit the Ceflifespanhandler class from the Xilium.cefglue


namespace xilium.cefglue{    //Abstract:    //     Implement This interface to handle events related to browser life span. The    //methods of this class would be called on the     UI thread unless otherwise indicated.    Public abstract class Ceflifespanhandler


inherit the Ceflifespanhandler class and override the Onbeforepopup method , implemented as follows:


Internal sealed class Cwblifespanhandler:ceflifespanhandler {private Chromewebbrowser webBrowser;        Public Cwblifespanhandler (Chromewebbrowser browser) {webBrowser = browser; } protected override void Onaftercreated (Cefbrowser browser) {base.            onaftercreated (browser);        webbrowser.oncreated (browser);        } protected override bool Doclose (Cefbrowser browser) {return false; } protected override bool Onbeforepopup (Cefbrowser browser, cefframe frame, string targeturl, String Targetframenam E, cefwindowopendisposition targetdisposition, bool usergesture, Cefpopupfeatures popupfeatures, CefWindowInfo  Windowinfo, ref cefclient client, cefbrowsersettings settings, ref bool nojavascriptaccess) {BOOL res =            False if (!string.                    IsNullOrEmpty (TargetUrl)) {if (webbrowser.selfrequest! = null) {Cefrequest req = Cefrequest.create (); Req.                    Firstpartyforcookies = webBrowser.selfRequest.FirstPartyForCookies; Req.                    Options = webBrowser.selfRequest.Options;                    System.Collections.Specialized.NameValueCollection h = new System.Collections.Specialized.NameValueCollection ();                    H.add ("Content-type", "application/x-www-form-urlencoded"); Req.                    Set (TargetUrl, WebBrowser.selfRequest.Method, NULL, WEBBROWSER.SELFREQUEST.GETHEADERMAP ());                Webbrowser.selfrequest = req;                }//Where to implement open all newly opened Windows Webbrowser.openurl (TargetUrl) with the current page;                res = true;            if (res) return res; } return base. Onbeforepopup (browser, frame, TargetUrl, Targetframename, Targetdisposition, Usergesture, Popupfeatures, WindowInfo,            Ref client, settings, ref nojavascriptaccess);                    return res; }    }

Although the browser's new window processing method has been modified, the browser does not automatically load into our CWBCeflifespanhandler class, Next we need to add our CWBCeflifespanhandler Implementation class to the Clientbrowser class that implements Cefclient:


public sealed class clientbrowser:cefclient{        private readonly cwblifespanhandler _lifespanhandler;        Private Chromewebbrowser WebBrowser;        Public Clientbrowser (Chromewebbrowser browser)        {            webBrowser = browser;            _lifespanhandler = new Cwblifespanhandler (browser);        }        public override Ceflifespanhandler Getlifespanhandler ()        {            return _lifespanhandler;        }}


Our work has been completed and we can run the program directly to test it.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

CEF Xilium.cefglue Open All links in the current window (prevents pop-up windows)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.