/// <summary> /// Initializes a new instance of the <see cref="CefGlueBrowser"/> class. /// </summary> /// <param name="browserConfig"> /// The browser config. /// </param> public CefGlueBrowser(CefBrowserConfig browserConfig) { this.mBrowserConfig = browserConfig; this.StartUrl = string.IsNullOrEmpty(browserConfig.StartUrl) ? "about:blank" : browserConfig.StartUrl; this.CreateBrowser(); }
/// <summary> /// The on create. /// </summary> /// <param name="packet"> /// The packet. /// </param> /// <exception cref="Exception"> /// Rethrown exception on Cef load failure. /// </exception> protected override void OnCreate(ref CreateWindowPacket packet) { // Will throw exception if Cef load fails. CefRuntime.Load(); var mainArgs = new CefMainArgs(this.HostConfig.AppArgs); var app = new CefGlueApp(this.HostConfig); var exitCode = CefRuntime.ExecuteProcess(mainArgs, app, IntPtr.Zero); if (exitCode != -1) { return; } var codeBase = Assembly.GetExecutingAssembly().CodeBase; var localFolder = Path.GetDirectoryName(new Uri(codeBase).LocalPath); var localesDirPath = Path.Combine(localFolder, "locales"); var settings = new CefSettings { LocalesDirPath = localesDirPath, Locale = this.HostConfig.Locale, SingleProcess = false, MultiThreadedMessageLoop = true, LogSeverity = (CefLogSeverity)this.HostConfig.LogSeverity, LogFile = this.HostConfig.LogFile, ResourcesDirPath = Path.GetDirectoryName(new Uri(Assembly.GetEntryAssembly().CodeBase).LocalPath), NoSandbox = true }; // Update configuration settings settings.Update(this.HostConfig.CustomSettings); CefRuntime.Initialize(mainArgs, settings, app, IntPtr.Zero); this.RegisterSchemeHandlers(); this.RegisterMessageRouters(); var browserConfig = new CefBrowserConfig { StartUrl = this.HostConfig.StartUrl, ParentHandle = this.Handle, AppArgs = this.HostConfig.AppArgs, CefRectangle = new CefRectangle { X = 0, Y = 0, Width = this.HostConfig.HostWidth, Height = this.HostConfig.HostHeight } }; this.mBrowser = new CefGlueBrowser(browserConfig); base.OnCreate(ref packet); Log.Info("Cef browser successfully created."); }
protected override void OnCreate(ref CreateWindowPacket packet) { try { CefRuntime.Load(); } catch (Exception ex) { throw ex; } var mainArgs = new CefMainArgs(HostConfig.CefAppArgs); var app = new CefWebApp(); var exitCode = CefRuntime.ExecuteProcess(mainArgs, app, IntPtr.Zero); if (exitCode != -1) { return; } var codeBase = Assembly.GetExecutingAssembly().CodeBase; var localFolder = Path.GetDirectoryName(new Uri(codeBase).LocalPath); var localesDirPath = Path.Combine(localFolder, "locales"); var settings = new CefSettings { LocalesDirPath = localFolder, Locale = HostConfig.CefLocale, SingleProcess = false, MultiThreadedMessageLoop = true, LogSeverity = (CefLogSeverity)HostConfig.CefLogSeverity, LogFile = HostConfig.CefLogFile }; CefRuntime.Initialize(mainArgs, settings, app, IntPtr.Zero); RegisterSchemeHandlers(); RegisterMessageRouters(); CefBrowserConfig browserConfig = new CefBrowserConfig(); browserConfig.StartUrl = HostConfig.CefStartUrl; browserConfig.ParentHandle = Handle; browserConfig.AppArgs = HostConfig.CefAppArgs; browserConfig.CefRectangle = new CefRectangle { X = 0, Y = 0, Width = HostConfig.CefHostWidth, Height = HostConfig.CefHostHeight }; m_browser = new CefWebBrowser(browserConfig); base.OnCreate(ref packet); Log.Info("Cef browser successfully created."); }
/// <summary> /// Initializes a new instance of the <see cref="Window"/> class. /// </summary> /// <param name="application"> /// The application. /// </param> /// <param name="hostConfig"> /// The host config. /// </param> public Window(HostBase application, CefConfiguration hostConfig) { CefGlueClient.window = this; this.Text = application.HostConfig.HostTitle; this.Size = new Size(application.HostConfig.HostWidth, application.HostConfig.HostHeight); this.StartPosition = FormStartPosition.CenterScreen; if (File.Exists(Path.Combine(application.HostConfig.HostIconFile))) { this.Icon = Icon.FromHandle((new Bitmap(Path.Combine(application.HostConfig.HostIconFile))).GetHicon()); } this.AutoScaleMode = AutoScaleMode.Dpi; this.AutoScroll = false; this.BackColor = Color.FromArgb(32, 31, 41); this.mHostConfig = hostConfig; this.mApplication = application; var browserConfig = new CefBrowserConfig { StartUrl = application.HostConfig.StartUrl, ParentHandle = this.Handle, AppArgs = application.HostConfig.AppArgs, CefRectangle = new CefRectangle { X = 0, Y = 0, Width = this.ClientSize.Width, Height = this.ClientSize.Height } }; this.FormClosing += (s, args) => { this.mApplication.Quit(); }; this.SizeChanged += (s, args) => { if (this.mBrowserWindowHandle != IntPtr.Zero) { NativeMethods.SetWindowPos(this.mBrowserWindowHandle, IntPtr.Zero, 0, 0, this.ClientSize.Width, this.ClientSize.Height, Core.Host.WinapiConstants.NoZOrder); } }; #region Browser this.mCore = new CefGlueBrowser(browserConfig); this.mCore.AddressChanged += (s, e) => { this.InvokeIfRequired(() => { }); }; this.mCore.BeforeClose += (s, e) => { this.InvokeIfRequired(() => { }); }; this.mCore.BeforePopup += (s, e) => { this.InvokeIfRequired(() => { }); }; this.mCore.BrowserCreated += (s, args) => { this.mBrowserWindowHandle = this.mCore.Browser.GetHost().GetWindowHandle(); }; this.mCore.ConsoleMessage += (s, e) => { this.InvokeIfRequired(() => { Log.Trace(e.Source, e.Line, e.Message); }); }; this.mCore.LoadEnd += (s, e) => { this.InvokeIfRequired(() => { }); }; this.mCore.LoadError += (s, e) => { this.InvokeIfRequired(() => { }); }; this.mCore.LoadingStateChange += (s, e) => { this.InvokeIfRequired(() => { }); }; this.mCore.LoadStarted += (s, e) => { this.InvokeIfRequired(() => { }); }; this.mCore.PluginCrashed += (s, e) => { this.InvokeIfRequired(() => { }); }; this.mCore.RenderProcessTerminated += (s, e) => { this.InvokeIfRequired(() => { if (this.mCore != null) { Log.Critial("Render Process Terminated: reloading page"); if (this.mCore.Browser != null) { this.mCore.Browser.Reload(); } } }); }; this.mCore.StatusMessage += (s, e) => { this.InvokeIfRequired(() => { }); }; this.mCore.PluginCrashed += (s, e) => { this.InvokeIfRequired(() => { }); }; this.mCore.TitleChanged += (s, e) => { this.InvokeIfRequired(() => { if (!string.IsNullOrEmpty(e.Title)) { Text = e.Title; } }); }; this.mCore.Tooltip += (s, e) => { this.InvokeIfRequired(() => { }); }; #endregion this.Show(); }