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)