2015-06-15 16:33:22 +08:00
|
|
|
|
// Copyright © 2010-2015 The CefSharp Authors. All rights reserved.
|
2014-10-07 09:03:00 +08:00
|
|
|
|
//
|
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
using CefSharp.MinimalExample.WinForms.Controls;
|
|
|
|
|
using CefSharp.WinForms;
|
2019-07-27 20:24:56 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Windows.Forms;
|
2014-10-07 09:03:00 +08:00
|
|
|
|
|
|
|
|
|
namespace CefSharp.MinimalExample.WinForms
|
|
|
|
|
{
|
|
|
|
|
public partial class BrowserForm : Form
|
|
|
|
|
{
|
2021-09-21 07:27:03 +08:00
|
|
|
|
#if DEBUG
|
|
|
|
|
private const string Build = "Debug";
|
|
|
|
|
#else
|
|
|
|
|
private const string Build = "Release";
|
|
|
|
|
#endif
|
|
|
|
|
private string title = "CefSharp.MinimalExample.WinForms (" + Build + ")";
|
2014-10-07 09:03:00 +08:00
|
|
|
|
private readonly ChromiumWebBrowser browser;
|
|
|
|
|
|
|
|
|
|
public BrowserForm()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
2021-09-21 07:27:03 +08:00
|
|
|
|
Text = title;
|
2014-10-07 09:03:00 +08:00
|
|
|
|
WindowState = FormWindowState.Maximized;
|
|
|
|
|
|
2019-12-14 09:52:53 +08:00
|
|
|
|
browser = new ChromiumWebBrowser("www.google.com");
|
2014-10-07 09:03:00 +08:00
|
|
|
|
toolStripContainer.ContentPanel.Controls.Add(browser);
|
|
|
|
|
|
2018-07-02 17:43:42 +08:00
|
|
|
|
browser.IsBrowserInitializedChanged += OnIsBrowserInitializedChanged;
|
2015-08-20 09:54:49 +08:00
|
|
|
|
browser.LoadingStateChanged += OnLoadingStateChanged;
|
2014-10-07 09:03:00 +08:00
|
|
|
|
browser.ConsoleMessage += OnBrowserConsoleMessage;
|
|
|
|
|
browser.StatusMessage += OnBrowserStatusMessage;
|
|
|
|
|
browser.TitleChanged += OnBrowserTitleChanged;
|
|
|
|
|
browser.AddressChanged += OnBrowserAddressChanged;
|
|
|
|
|
|
2019-09-10 16:20:34 +08:00
|
|
|
|
var version = string.Format("Chromium: {0}, CEF: {1}, CefSharp: {2}",
|
|
|
|
|
Cef.ChromiumVersion, Cef.CefVersion, Cef.CefSharpVersion);
|
|
|
|
|
|
|
|
|
|
#if NETCOREAPP
|
|
|
|
|
// .NET Core
|
|
|
|
|
var environment = string.Format("Environment: {0}, Runtime: {1}",
|
|
|
|
|
System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture.ToString().ToLowerInvariant(),
|
|
|
|
|
System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription);
|
|
|
|
|
#else
|
|
|
|
|
// .NET Framework
|
2014-10-07 09:03:00 +08:00
|
|
|
|
var bitness = Environment.Is64BitProcess ? "x64" : "x86";
|
2019-12-14 09:52:53 +08:00
|
|
|
|
var environment = String.Format("Environment: {0}", bitness);
|
2019-09-10 16:20:34 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
DisplayOutput(string.Format("{0}, {1}", version, environment));
|
2014-10-07 09:03:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-27 20:24:56 +08:00
|
|
|
|
private void OnIsBrowserInitializedChanged(object sender, EventArgs e)
|
2018-07-02 17:43:42 +08:00
|
|
|
|
{
|
2019-07-27 20:24:56 +08:00
|
|
|
|
var b = ((ChromiumWebBrowser)sender);
|
2018-07-02 17:43:42 +08:00
|
|
|
|
|
2019-07-27 20:24:56 +08:00
|
|
|
|
this.InvokeOnUiThreadIfRequired(() => b.Focus());
|
2018-07-02 17:43:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-07 09:03:00 +08:00
|
|
|
|
private void OnBrowserConsoleMessage(object sender, ConsoleMessageEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
DisplayOutput(string.Format("Line: {0}, Source: {1}, Message: {2}", args.Line, args.Source, args.Message));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnBrowserStatusMessage(object sender, StatusMessageEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
this.InvokeOnUiThreadIfRequired(() => statusLabel.Text = args.Value);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-20 09:54:49 +08:00
|
|
|
|
private void OnLoadingStateChanged(object sender, LoadingStateChangedEventArgs args)
|
2014-10-07 09:03:00 +08:00
|
|
|
|
{
|
|
|
|
|
SetCanGoBack(args.CanGoBack);
|
|
|
|
|
SetCanGoForward(args.CanGoForward);
|
|
|
|
|
|
|
|
|
|
this.InvokeOnUiThreadIfRequired(() => SetIsLoading(!args.CanReload));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnBrowserTitleChanged(object sender, TitleChangedEventArgs args)
|
|
|
|
|
{
|
2021-09-21 07:27:03 +08:00
|
|
|
|
this.InvokeOnUiThreadIfRequired(() => Text = title + " - " + args.Title);
|
2014-10-07 09:03:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnBrowserAddressChanged(object sender, AddressChangedEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
this.InvokeOnUiThreadIfRequired(() => urlTextBox.Text = args.Address);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetCanGoBack(bool canGoBack)
|
|
|
|
|
{
|
|
|
|
|
this.InvokeOnUiThreadIfRequired(() => backButton.Enabled = canGoBack);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetCanGoForward(bool canGoForward)
|
|
|
|
|
{
|
|
|
|
|
this.InvokeOnUiThreadIfRequired(() => forwardButton.Enabled = canGoForward);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetIsLoading(bool isLoading)
|
|
|
|
|
{
|
|
|
|
|
goButton.Text = isLoading ?
|
|
|
|
|
"Stop" :
|
|
|
|
|
"Go";
|
|
|
|
|
goButton.Image = isLoading ?
|
|
|
|
|
Properties.Resources.nav_plain_red :
|
|
|
|
|
Properties.Resources.nav_plain_green;
|
|
|
|
|
|
|
|
|
|
HandleToolStripLayout();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DisplayOutput(string output)
|
|
|
|
|
{
|
|
|
|
|
this.InvokeOnUiThreadIfRequired(() => outputLabel.Text = output);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HandleToolStripLayout(object sender, LayoutEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
HandleToolStripLayout();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HandleToolStripLayout()
|
|
|
|
|
{
|
|
|
|
|
var width = toolStrip1.Width;
|
|
|
|
|
foreach (ToolStripItem item in toolStrip1.Items)
|
|
|
|
|
{
|
|
|
|
|
if (item != urlTextBox)
|
|
|
|
|
{
|
|
|
|
|
width -= item.Width - item.Margin.Horizontal;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
urlTextBox.Width = Math.Max(0, width - urlTextBox.Margin.Horizontal - 18);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ExitMenuItemClick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
browser.Dispose();
|
|
|
|
|
Cef.Shutdown();
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void GoButtonClick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
LoadUrl(urlTextBox.Text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void BackButtonClick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
browser.Back();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ForwardButtonClick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
browser.Forward();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UrlTextBoxKeyUp(object sender, KeyEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.KeyCode != Keys.Enter)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LoadUrl(urlTextBox.Text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LoadUrl(string url)
|
|
|
|
|
{
|
|
|
|
|
if (Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
|
|
|
|
|
{
|
|
|
|
|
browser.Load(url);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-06 08:35:31 +08:00
|
|
|
|
|
|
|
|
|
private void ShowDevToolsMenuItemClick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
browser.ShowDevTools();
|
|
|
|
|
}
|
2014-10-07 09:03:00 +08:00
|
|
|
|
}
|
|
|
|
|
}
|