Create a gist now

Instantly share code, notes, and snippets.

using System;
using System.ComponentModel;
using System.Windows.Forms;
using mshtml;
namespace MyBrowser
{
class BrowserTab : TabPage
{
public event CancelEventHandler NewWindow;
private WebBrowser wb = new WebBrowser();
#region Constructor
public BrowserTab() {
wb.DocumentTitleChanged += wb_DocumentTitleChanged;
wb.NewWindow += wb_NewWindow;
wb.Url = new Uri("http://www.google.com");
wb.Dock = DockStyle.Fill;
Controls.Add(wb);
}
public BrowserTab(Uri url) : this()
{
wb.Url =url;
}
#endregion
#region Property
public WebBrowser Wb { get { return wb; } }
public HtmlDocument Document { get { return wb.Document; } }
#endregion
#region EventHandler
private void wb_DocumentTitleChanged(object sender, EventArgs e)
{
Text = wb.DocumentTitle;
}
private void wb_NewWindow(object sender, CancelEventArgs e)
{
NewWindow(this, e);
}
#endregion
}
}
using System;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
namespace MyBrowser
{
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public partial class MyBrowser : Form
{
#region Const Field
private const string REG_KEY_PATH = @"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION";
private const int REG_VALUE_IE11 = 11001; //IE11 = 11000, IE11(Edge) = 11001
private string registry_name = Path.GetFileName(Assembly.GetExecutingAssembly().Location);
#endregion
#region Constructor
public MyBrowser()
{
InitializeComponent();
SetRegistry();
SetTabControlStyle();
BrowserTab bt = new BrowserTab();
tc.Controls.Add(bt);
bt.NewWindow += BrowserTab_NewWindow;
bt.Wb.ObjectForScripting = this;
}
#endregion
private void SetRegistry()
{
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(REG_KEY_PATH);
object regValue = regKey.GetValue(registry_name);
if (regValue == null)
{
regKey.SetValue(registry_name, REG_VALUE_IE11);
regKey.Close();
return;
}
if ((int)regValue != REG_VALUE_IE11)
{
regKey.SetValue(registry_name, REG_VALUE_IE11);
}
regKey.Close();
}
private void SetTabControlStyle()
{
tc.TabClosing += Tc_TabClosing;
tc.DisplayStyle = TabStyle.Chrome;
}
#region Event Handler
private void BrowserTab_NewWindow(object sender, CancelEventArgs e)
{
e.Cancel = true;
HtmlWindow window = ((BrowserTab)sender).Document.Window;
HtmlElement element = ((BrowserTab)sender).Document.ActiveElement;
// iframeの入れ子を掘り下げていく
while (element.TagName.Equals("iframe", StringComparison.InvariantCultureIgnoreCase))
{
HtmlWindow iframe = window.Frames[0];
element = iframe.Document.ActiveElement;
window = iframe;
}
string targetUrl = element.GetAttribute("href");
BrowserTab newTab = new BrowserTab(new Uri(targetUrl));
newTab.NewWindow += BrowserTab_NewWindow;
tc.Controls.Add(newTab);
}
private void Tc_TabClosing(object sender, TabControlCancelEventArgs e)
{
if (tc.Controls.Count == 1)
{
BrowserTab bt = new BrowserTab();
tc.Controls.Add(bt);
bt.NewWindow += BrowserTab_NewWindow;
}
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment