1.これまでの以下1、2、3をおさらいして、新しくプロジェクトを作成します。
- WinFormsアプリを作成して、NuGetパッケージを使ってCefSharpを追加する。
- プラットフォーム構成を変更して、AnyCPUで使用できるようにする。
- CefSharpを使って、通常のブラウザを表示する。
2.メニュー「ツール(T)」-「NuGet パッケージ マネージャー(N)」-「ソリューションのNuGetパッケージの管理(N)…」を選択します。
3.左上の「参照」をクリックして、検索欄に「easytabs」と入力して検索を行い、検索結果の中から「EasyTabs」を選択して、現在のプロジェクトにインストールします。
4.下画面で「OK」をクリックします。
5.インストールが終わると、プロジェクトの参照に「EasyTabs」が追加されます。
6.フォーム(Form1.cs)を変更します。
7.フォーム(AppContainer.cs)を追加します。
新規フォームを追加し、継承元クラスを Form から TitleBarTabs に変更します。
このフォームをデザイナーで開くと、以下のように例外エラーになりますが、動作には問題ありませんのでこのまま使用します。
8.ソース(program.cs)を変更します。
プログラムエントリポイントを Form1 から AppContainer に変更します。
9.ここまで出来たら、実行してみます。TABブラウザになっていたらOKです。
尚、本来、タブブラウザを作る際は、以下の考慮も必要です。(ここでは省略します。)
- ブラウザからのNewWindowイベントを検知して、新しいタブを作成する。
- 新しいタブにCookie情報を継承させる。
【フォーム(Form1.cs)】
using System;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
using EasyTabs;
namespace WindowsFormsApp19
{
public partial class Form1 : Form
{
public ChromiumWebBrowser chromeBrowser;
public Form1()
{
InitializeComponent();
// Start the browser after initialize global component
InitializeChromium();
}
private void Form1_Load(object sender, EventArgs e)
{
chromeBrowser.ShowDevTools();
}
public void InitializeChromium()
{
// Cef.Initialize(settings) が1プロセス中に2度呼び出されると例外エラーになるので
if (Cef.IsInitialized == false)
{
// Initialize cef with the provided settings
CefSettings settings = new CefSettings();
settings.Locale = "ja";
settings.AcceptLanguageList = "ja-JP";
Cef.Initialize(settings);
}
// Create a browser component
chromeBrowser = new ChromiumWebBrowser("https://agrimo.jp");
// Add it to the form and fill it to the form window.
this.Controls.Add(chromeBrowser);
chromeBrowser.Dock = DockStyle.Fill;
// Allow the use of local resources in the browser
BrowserSettings browserSettings = new BrowserSettings();
browserSettings.FileAccessFromFileUrls = CefState.Enabled;
browserSettings.UniversalAccessFromFileUrls = CefState.Enabled;
chromeBrowser.BrowserSettings = browserSettings;
}
protected TitleBarTabs ParentTabs
{
get
{
return (ParentForm as TitleBarTabs);
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Cef.Shutdown();
}
}
}
【フォーム(AppContainer.cs)】
using EasyTabs;
namespace WindowsFormsApp19
{
public partial class AppContainer : TitleBarTabs
{
public AppContainer()
{
InitializeComponent();
AeroPeekEnabled = true;
TabRenderer = new ChromeTabRenderer(this);
}
// Handle the method CreateTab that allows the user to create a new Tab
// on your app when clicking
public override TitleBarTab CreateTab()
{
return new TitleBarTab(this)
{
// The content will be an instance of another Form
// In our example, we will create a new instance of the Form1
Content = new Form1
{
Text = "New Tab"
}
};
}
// The rest of the events in your app here if you need to .....
}
}
【ソース(program.cs)】
using System;
using System.Windows.Forms;
using EasyTabs;
namespace WindowsFormsApp19
{
static class Program
{
/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1());
AppContainer container = new AppContainer();
// Add the initial Tab
container.Tabs.Add(
// Our First Tab created by default in the Application will have as content the Form1
new TitleBarTab(container)
{
Content = new Form1
{
Text = "New Tab"
}
}
);
// Set initial tab the first one
container.SelectedTabIndex = 0;
// Create tabs and start application
TitleBarTabsApplicationContext applicationContext = new TitleBarTabsApplicationContext();
applicationContext.Start(container);
Application.Run(applicationContext);
}
}
}












