Last active
June 22, 2018 21:26
-
-
Save vwmberry95/01d96cdee92a82fb052631509997ef3c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Windows.Forms; | |
using CefSharp; | |
using CefSharp.WinForms; | |
namespace WindowsFormsApp1 | |
{ | |
public partial class Form1 : Form | |
{ | |
private TabControl _tabControl = null; | |
public Form1() | |
{ | |
InitializeComponent(); | |
if (!Cef.IsInitialized) | |
{ | |
CefSettings settings = new CefSettings(); | |
// Register the "local" scheme | |
settings.RegisterScheme(new CefCustomScheme | |
{ | |
SchemeName = "local", | |
SchemeHandlerFactory = new LocalSchemeHandlerFactory(), | |
IsSecure = true | |
}); | |
Cef.Initialize(settings); | |
} | |
// Create the TabControl | |
_tabControl = new TabControl | |
{ | |
Dock = DockStyle.Fill | |
}; | |
this.Controls.Add(_tabControl); | |
// Add the default tabs | |
this.AddNewTab("https://www.google.com"); | |
this.AddNewTab("https://www.w3schools.com/jsref/met_win_close.asp"); // Test window.open and window.close | |
this.AddNewTab("http://www.thispagedoesnotexist.com"); // Test load error handling | |
} | |
public void AddNewTab(string url) | |
{ | |
// Create a new browser | |
ChromiumWebBrowser browser = new ChromiumWebBrowser(url) | |
{ | |
Dock = DockStyle.Fill, | |
LifeSpanHandler = new LifeSpanHandler(this), | |
LoadHandler = new LoadHandler() | |
}; | |
// Create a new tab | |
TabPage tab = new TabPage(url); | |
tab.Controls.Add(browser); | |
// Add it to the tab control | |
_tabControl.Controls.Add(tab); | |
} | |
public Control AddNewPopup(string url) | |
{ | |
// Create a new host control | |
Control host = new Control | |
{ | |
Dock = DockStyle.Fill | |
}; | |
// Create a new tab | |
TabPage tab = new TabPage(url); | |
tab.Controls.Add(host); | |
_tabControl.Invoke(new Action(() => | |
{ | |
// Add it to the tab control | |
_tabControl.Controls.Add(tab); | |
})); | |
return host; | |
} | |
public void CloseTab(TabPage tab) | |
{ | |
_tabControl.Invoke(new Action(() => | |
{ | |
// Remove this tab | |
_tabControl.Controls.Remove(tab); | |
})); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment