Files
cefsharp/CefSharp.WinForms.Example/BrowserTabUserControl.cs
T
2014-07-01 12:03:58 +10:00

150 lines
4.5 KiB
C#

using System.Threading.Tasks;
using CefSharp.Example;
using CefSharp.WinForms.Example.Controls;
using System;
using System.Windows.Forms;
namespace CefSharp.WinForms.Example
{
public partial class BrowserTabUserControl : UserControl
{
private readonly ChromiumWebBrowser browser;
public BrowserTabUserControl()
{
InitializeComponent();
browser = new ChromiumWebBrowser(CefExample.DefaultUrl)
{
Dock = DockStyle.Fill,
};
toolStripContainer.ContentPanel.Controls.Add(browser);
browser.MenuHandler = new MenuHandler();
browser.NavStateChanged += OnBrowserNavStateChanged;
browser.ConsoleMessage += OnBrowserConsoleMessage;
browser.TitleChanged += OnBrowserTitleChanged;
browser.AddressChanged += OnBrowserAddressChanged;
var version = String.Format("Chromium: {0}, CEF: {1}, CefSharp: {2}", Cef.ChromiumVersion, Cef.CefVersion, Cef.CefSharpVersion);
DisplayOutput(version);
}
private void OnBrowserConsoleMessage(object sender, ConsoleMessageEventArgs args)
{
DisplayOutput(string.Format("Line: {0}, Source: {1}, Message: {2}", args.Line, args.Source, args.Message));
}
private void OnBrowserNavStateChanged(object sender, NavStateChangedEventArgs args)
{
SetCanGoBack(args.CanGoBack);
SetCanGoForward(args.CanGoForward);
this.InvokeOnUiThreadIfRequired(() => SetIsLoading(!args.CanReload));
}
private void OnBrowserTitleChanged(object sender, TitleChangedEventArgs args)
{
this.InvokeOnUiThreadIfRequired(() => Parent.Text = args.Title);
}
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 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);
}
}
public void CopySourceToClipBoardAsync()
{
var task = browser.GetSourceAsync();
task.ContinueWith(t =>
{
if (!t.IsFaulted)
{
Clipboard.SetText(t.Result);
DisplayOutput("HTML Source copied to clipboard");
}
},
TaskScheduler.FromCurrentSynchronizationContext());
}
}
}