タブコントロール

たぶんブラウザのタブコントロールのソース
2つファイルがあります。

ファイル名:Classes\KAnisTabControl.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace ABCS.Classes
{
    /// TabMouseClickイベントを処理するメソッドを表します。
    public delegate void TabMouseEventHandler(object sender, TabMouseEventArgs e);

    /// タブ部分がクリックされた際に、
    /// クリックされたタブを取得出来るTabMouseClickイベントを備えたTabControl
    public class KAnisTabControl : TabControl
    {
        //リフレクションでデータ取得用する型名(string)を取得。
        //子クラスでオーバーライドすること。
        //(テンプレートメソッド)
        protected virtual string GetThisTabPageType()
        {
            return "System.Windows.Forms.TabPage";
        }

        #region//各種TabMouseEvent定義
        /// クリックされたタブが取得可能なMouseDownイベント
        [Category("アクション"), Description("マウスでタブをクリックした時に発生します。")]
        public event TabMouseEventHandler TabMouseDown;
        /// クリックされたタブが取得可能なMouseClickイベント
        [Category("アクション"), Description("マウスでタブをクリックした時に発生します。")]
        public event TabMouseEventHandler TabMouseClick;
        /// クリックされたタブが取得可能なMouseUpイベント
        [Category("アクション"), Description("マウスでタブをクリックした時に発生します。")]
        public event TabMouseEventHandler TabMouseUp;
        /// クリックされたタブが取得可能なMouseDoubleClickイベント
        [Category("アクション"), Description("マウスでタブをダブルクリックした時に発生します。")]
        public event TabMouseEventHandler TabMouseDoubleClick;
        /// クリックされたタブが取得可能なMouseWheelイベント
        [Category("アクション"), Description("マウスホイールがタブ上で動かされた時に発生します。")]
        public event TabMouseEventHandler TabMouseWheel;
        /// クリックされたタブが取得可能なMouseMoveイベント
        [Category("アクション"), Description("マウスポインタがコントロール上を移動すると発生します。")]
        public event TabMouseEventHandler TabMouseMove;
        #endregion

        /// XMLコメントが無いって警告うざい
        public KAnisTabControl()
        {
            #region//TabMouseEvent関連イベント登録
            this.MouseDown += new MouseEventHandler(AnisTabControl_MouseDown);
            this.MouseClick += new MouseEventHandler(AnisTabControl_MouseClick);
            this.MouseUp += new MouseEventHandler(AnisTabControl_MouseUp);
            this.MouseDoubleClick += new MouseEventHandler(AnisTabControl_MouseDoubleClick);
            this.MouseWheel += new MouseEventHandler(AnisTabControl_MouseWheel);
            this.MouseMove += new MouseEventHandler(AnisTabControl_MouseMove);
            #endregion
        }

        /// Point上のタブコントロールのタブ部分に対応したTabPageのインデックスを取得
        /// インデックスを取得するPoint
        /// Point上にタブが無ければ-1が返ります。。
        int TabIndexFromPoint(Point point)
        {
            for (int i = 0; i <= this.TabPages.Count - 1; i++)
            {
                if (this.GetTabRect(i).Contains(point))
                {
                    return i;
                }
            }
            return -1;
        }

        //#region//Tabマウスイベント
        void AnisTabControl_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                int index = TabIndexFromPoint(e.Location);
                if (index != -1)
                {
                    TabMouseEventArgs E = new TabMouseEventArgs(index, e.Button, e.Clicks, e.X, e.Y, e.Delta);
                    this.TabMouseDown(sender, E);
                }
            }
            catch (NullReferenceException) { }
        }
        void AnisTabControl_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                int index = TabIndexFromPoint(e.Location);
                if (index != -1)
                {
                    TabMouseEventArgs E = new TabMouseEventArgs(index, e.Button, e.Clicks, e.X, e.Y, e.Delta);
                    this.TabMouseClick(sender, E);
                }
            }
            catch (NullReferenceException) { }
        }
        void AnisTabControl_MouseUp(object sender, MouseEventArgs e)
        {
            try
            {
                int index = TabIndexFromPoint(e.Location);
                if (index != -1)
                {
                    TabMouseEventArgs E = new TabMouseEventArgs(index, e.Button, e.Clicks, e.X, e.Y, e.Delta);
                    this.TabMouseUp(sender, E);
                }
            }
            catch (NullReferenceException) { }
        }
        void AnisTabControl_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                int index = TabIndexFromPoint(e.Location);
                if (index != -1)
                {
                    TabMouseEventArgs E = new TabMouseEventArgs(index, e.Button, e.Clicks, e.X, e.Y, e.Delta);
                    this.TabMouseDoubleClick(sender, E);
                }
            }
            catch (NullReferenceException) { }
        }
        void AnisTabControl_MouseWheel(object sender, MouseEventArgs e)
        {
            try
            {
                int index = TabIndexFromPoint(e.Location);
                if (index != -1)
                {
                    TabMouseEventArgs E = new TabMouseEventArgs(index, e.Button, e.Clicks, e.X, e.Y, e.Delta);
                    this.TabMouseWheel(sender, E);
                }
            }
            catch (NullReferenceException) { }
        }
        void AnisTabControl_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                int index = TabIndexFromPoint(e.Location);
                if (index != -1)
                {
                    TabMouseEventArgs E = new TabMouseEventArgs(index, e.Button, e.Clicks, e.X, e.Y, e.Delta);
                    this.TabMouseMove(sender, E);
                }
            }
            catch (NullReferenceException) { }
        }
        //#endregion

        #region//ドラッグ&ドロップ
        Point mouseDownPoint = Point.Empty;
        bool sortTabDragDrop;
        /// trueでドラッグ&ドロップによるタブの並び替えを可能にします。
        [Category("動作"), Description("trueでドラッグ&ドロップによるタブの並び替えを可能にします。")]
        public bool SortTabDragDrop
        {
            set
            {
                if (sortTabDragDrop == value) return;
                sortTabDragDrop = value;
                if (value)
                {
                    this.MouseDown += new MouseEventHandler(AnisTabControl_MouseDown1);
                    this.MouseUp += new MouseEventHandler(AnisTabControl_MouseUp1);
                    this.TabMouseMove += new TabMouseEventHandler(AnisTabControl_TabMouseMove);
                    this.DragEnter += new DragEventHandler(AnisTabControl_DragEnter);
                    this.DragOver += new DragEventHandler(AnisTabControl_DragOver);
                    this.DragDrop += new DragEventHandler(AnisTabControl_DragDrop);
                }
                else
                {
                    this.MouseDown -= new MouseEventHandler(AnisTabControl_MouseDown1);
                    this.MouseUp -= new MouseEventHandler(AnisTabControl_MouseUp1);
                    this.TabMouseMove -= new TabMouseEventHandler(AnisTabControl_TabMouseMove);
                    this.DragEnter -= new DragEventHandler(AnisTabControl_DragEnter);
                    this.DragOver -= new DragEventHandler(AnisTabControl_DragOver);
                    this.DragDrop -= new DragEventHandler(AnisTabControl_DragDrop);
                }
            }
            get
            {
                return sortTabDragDrop;
            }
        }

        void AnisTabControl_MouseDown1(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseDownPoint = e.Location;
            }
            else
            {
                mouseDownPoint = Point.Empty;
            }
        }

        void AnisTabControl_MouseUp1(object sender, MouseEventArgs e)
        {
            mouseDownPoint = Point.Empty;
        }

        void AnisTabControl_TabMouseMove(object sender, TabMouseEventArgs e)
        {
            if (mouseDownPoint != Point.Empty)
            {
                Rectangle mouseMoveRectangle = new Rectangle(
                    mouseDownPoint.X - 5, mouseDownPoint.Y - 5,
                    10, 10);
                if (!mouseMoveRectangle.Contains(e.Location))
                {
                    this.DoDragDrop(this.TabPages[e.TabIndex], DragDropEffects.Move);
                }
            }
        }

        void AnisTabControl_DragOver(object sender, DragEventArgs e)
        {
           mouseDownPoint = Point.Empty; 
        }

        void AnisTabControl_DragEnter(object sender, DragEventArgs e)
        {
            //Type myType = e.Data.GetType();
            TabPage myTabPage = e.Data.GetData(this.GetThisTabPageType(), true) as TabPage;

            if (myTabPage != null &&
                this.TabPages.Contains(myTabPage) &&
                this.TabIndexFromPoint(this.PointToClient(new Point(e.X, e.Y))) != -1)
            {
                e.Effect = DragDropEffects.Move;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        void AnisTabControl_DragDrop(object sender, DragEventArgs e)
        {
            int index = TabIndexFromPoint(this.PointToClient(new Point(e.X, e.Y)));
            //Type myTabType = e.Data.GetType();
            TabPage tabPage = e.Data.GetData(this.GetThisTabPageType(), true) as TabPage;
            //TabPage tabPage = e.Data.GetData("System.Windows.Forms.TabPage") as TabPage;
            if (tabPage != null)
            {
                this.TabPages.Remove(tabPage);
                this.TabPages.Insert(index, tabPage);
                this.SelectTab(index);
            }
        }
        #endregion
    }


    /// TabMouseClickイベントのデータ
    public class TabMouseEventArgs : MouseEventArgs
    {
        private int tabIndex_;
        /// クリックされたタブに対応したタブのインデックス
        public TabMouseEventArgs(int tabIndex, MouseButtons button, int clicks, int x, int y, int delta)
            : base(button, clicks, x, y, delta)
        {
            tabIndex_ = tabIndex;
        }
        /// クリックされたタブに対応したタブページ
        public int TabIndex
        {
            get
            {
                return tabIndex_;
            }
        }
    }
}

ファイル名:Classes\MyAnisTabControl.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace ABCS.Classes
{
    public class MyAnisTabControl : KAnisTabControl
    {
        //protected string ThisTabPageType = "ABCS.Classes.MyTabPageWithWeb";
        protected override string GetThisTabPageType()
        {
            return "ABCS.Classes.MyTabPageWithWeb";
        }

        public MyAnisTabControl()
            : base()
        {
            base.TabMouseDoubleClick += this.MyAnisTabControl_MouseDoubleClick;
            base.TabMouseClick += this.MyAnisTabControl_MouseClick;
        }
         

        void MyAnisTabControl_MouseDoubleClick(object sender, TabMouseEventArgs e)
        {
            
            TabControl tabCntl = sender as TabControl;

            if (tabCntl == null)
            {
                return;
            }
            else
            {
                int index = e.TabIndex;
                tabCntl.TabPages.Remove(tabCntl.TabPages[index]);
            }
        }

        void MyAnisTabControl_MouseClick(object sender, TabMouseEventArgs e)
        {
            TabControl tabCntl = sender as TabControl;

            if (tabCntl == null)
            {
                return;
            }
            else
            {
                int index = e.TabIndex;
                MyTabPageWithWeb tab = tabCntl.TabPages[index] as MyTabPageWithWeb;

                if( tab != null )
                {
                    tab.ChildWebBrowser.SetTitle2Form();
                }
            }
        }
    }
}

WebBrowserコンポーネント

WebBrowserコンポーネントは2種類あります。

ファイル名:Classes\MySnapWebBrowser.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Security.Permissions;

namespace ABCS.Classes
{
    public class MySnapWebBrowser : WebBrowserEx
    {
        private string beforeurl_ = null;
        private Form parentForm_ = null;
        private TabControl parentTabControl__ = null;

        public bool isSnap = false;
        public string snapUrl = null;

        public MySnapWebBrowser(TabControl parentTabControl, Form parentForm)
            : base()
        {
            this.parentTabControl__ = parentTabControl;
            this.NewWindow2 += new WebBrowserNewWindow2EventHandler(webBrowser_NewWindow2);
            //this.BeforeNavigate += new EventHandler(OnBeforeNavigate);
            this.BeforeNewWindow += new EventHandler(OnMyBeforeNewWindow);
            this.parentForm_ = parentForm;
        }

        

        protected void OnMyBeforeNewWindow(object sender, EventArgs e)
        {
            WebBrowserExtendedNavigatingEventArgs we = e as WebBrowserExtendedNavigatingEventArgs;
            if (we != null)
            {
                this.beforeurl_ = we.Url;
            }
        }
        /*
        protected void OnBeforeNavigate(object sender, EventArgs e)
        {
            WebBrowserExtendedNavigatingEventArgs we = e as WebBrowserExtendedNavigatingEventArgs;
            //cancel = false;
            if( we != null )
            {
                this.beforeurl_ = we.Url;
            }
        }
        */

        protected override void OnNavigating(WebBrowserNavigatingEventArgs e)
        {
            if (this.isSnap == true || this.snapUrl == e.Url.ToString() || string.IsNullOrEmpty(e.Url.ToString()))
            {
                base.OnNavigating(e);
                return;
            }
            MyTabPageWithWeb tab = new MyTabPageWithWeb(this.parentForm_, this.parentTabControl__, e.Url.ToString());

            this.parentTabControl__.TabPages.Add(tab);

            this.parentTabControl__.SelectTab(tab);

            e.Cancel = true;
        }

        // 新しくウィンドウを開かれようとするときに発生する
        void webBrowser_NewWindow2(object sender, WebBrowserNewWindow2EventArgs e)
        {
            MyTabPageWithWeb tab = new MyTabPageWithWeb(this.parentForm_, this.parentTabControl__, this.beforeurl_);

            
            // 新しい WebBrowser の初期化
            //WebBrowserEx newBrowser = new WebBrowserEx();
            //newBrowser.Dock = DockStyle.Fill;

            // 新しい WebBrowser のコンテナ(下記はタブの場合)
            //var tabPage = new TabPage();
            //tab.Controls.Add(newBrowser);
            this.parentTabControl__.TabPages.Add(tab);

            // 新しい WebBrowser に表示させる設定
            //e.ppDisp = newBrowser.Application;
            //newBrowser.RegisterAsBrowser = true;
            e.ppDisp = tab.ChildWebBrowser.Application;
            tab.ChildWebBrowser.RegisterAsBrowser = true;

            // 新しい WebBrowser からさらにウィンドウを開かれるときも同じようにする
            //newBrowser.NewWindow2 += webBrowser_NewWindow2;
            tab.ChildWebBrowser.NewWindow2 += webBrowser_NewWindow2;

            //newBrowser.Navigate(this.beforeurl_);
            //tab.ChildWebBrowser.Navigate(this.beforeurl_);

            this.parentTabControl__.SelectTab(tab);

            e.Cancel = true;
        }

        protected override void OnDocumentCompleted(WebBrowserDocumentCompletedEventArgs e)
        {
            base.OnDocumentCompleted(e);
            this.isSnap = false;

        }
    }

    public class WebBrowserEx : ExtendedWebBrowser
    {
#region NewWindow2 イベント関連

        private AxHost.ConnectionPointCookie cookie;
        private WebBrowser2EventHelper helper;

        [DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
        [DispIdAttribute(200)]
        public object Application
        {
            get
            {
                if (this.ActiveXInstance == null)
                {
                    throw new AxHost.InvalidActiveXStateException("Application", AxHost.ActiveXInvokeKind.PropertyGet);
                }
                return (this.ActiveXInstance as IWebBrowser2).Application;
            }
        }

        [DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
        [DispIdAttribute(552)]
        public bool RegisterAsBrowser
        {
            get
            {
                if (this.ActiveXInstance == null)
                {
                    throw new AxHost.InvalidActiveXStateException("RegisterAsBrowser", AxHost.ActiveXInvokeKind.PropertyGet);
                }
                return (this.ActiveXInstance as IWebBrowser2).RegisterAsBrowser;
            }
            set
            {
                if (this.ActiveXInstance == null)
                {
                    throw new AxHost.InvalidActiveXStateException("RegisterAsBrowser", AxHost.ActiveXInvokeKind.PropertyGet);
                }
                (this.ActiveXInstance as IWebBrowser2).RegisterAsBrowser = value;
            }
        }

        [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
        protected override void CreateSink()
        {
            base.CreateSink();
            helper = new WebBrowser2EventHelper(this);
            cookie = new AxHost.ConnectionPointCookie(this.ActiveXInstance, helper, typeof(DWebBrowserEvents2));
        }

        [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
        protected override void DetachSink()
        {
            if (cookie != null)
            {
                cookie.Disconnect();
                cookie = null;
            }
            base.DetachSink();
        }

        public event WebBrowserNewWindow2EventHandler NewWindow2;

        protected virtual void OnNewWindow2(WebBrowserNewWindow2EventArgs e)
        {
            NewWindow2(this, e);
        }

        private class WebBrowser2EventHelper : StandardOleMarshalObject, DWebBrowserEvents2
        {
            private WebBrowserEx parent;

            public WebBrowser2EventHelper(WebBrowserEx parent)
            {
                this.parent = parent;
            }

            public void NewWindow2(ref object ppDisp, ref bool cancel)
            {
                WebBrowserNewWindow2EventArgs e = new WebBrowserNewWindow2EventArgs(ppDisp);
                this.parent.OnNewWindow2(e);
                ppDisp = e.ppDisp;
                cancel = e.Cancel;
            }

            
            void ABCS.Classes.ExtendedWebBrowser.DWebBrowserEvents2.NewWindow3(object ppDisp , ref bool cancel , ref object a2, ref object a3, ref object a4)
            {
                WebBrowserNewWindow2EventArgs e = new WebBrowserNewWindow2EventArgs(ppDisp);
                this.parent.OnNewWindow2(e);
                ppDisp = e.ppDisp;
                cancel = e.Cancel;
            }

            void ABCS.Classes.ExtendedWebBrowser.DWebBrowserEvents2.BeforeNavigate2(object sender, ref object a1, ref object a2, ref object a3, ref object a4, ref object a5, ref bool a6)
            {
            }
             

        }
        #endregion

    }

    public delegate void WebBrowserNewWindow2EventHandler(object sender, WebBrowserNewWindow2EventArgs e);

    public class WebBrowserNewWindow2EventArgs : CancelEventArgs
    {
        private object ppdisp_ = null;
        public object ppDisp {
            get
            {
                return this.ppdisp_;
            }
            set
            {
                this.ppdisp_ = value;
            }
        }

        public WebBrowserNewWindow2EventArgs(object ppDisp)
        {
            this.ppDisp = ppDisp;
        }
    }

    [ComImport, Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D")]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    [TypeLibType(TypeLibTypeFlags.FHidden)]
    public interface DWebBrowserEvents2
    {
        [DispId(251)]
        void NewWindow2(
        [InAttribute(), OutAttribute(), MarshalAs(UnmanagedType.IDispatch)] ref object ppDisp,
        [InAttribute(), OutAttribute()] ref bool cancel);
    }

    [ComImport, Guid("D30C1661-CDAF-11D0-8A3E-00C04FC9E26E")]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface IWebBrowser2
    {
        object Application { get; }
        bool RegisterAsBrowser { get; set; }
    }




    public class WebBrowserExtendedNavigatingEventArgs : CancelEventArgs
    {
        private string _Url;
        public string Url
        {
            get { return _Url; }
        }

        private string _Frame;
        public string Frame
        {
            get { return _Frame; }
        }

        public WebBrowserExtendedNavigatingEventArgs(string url, string frame)
            : base()
        {
            _Url = url;
            _Frame = frame;
        }
    }

    public class ExtendedWebBrowser : System.Windows.Forms.WebBrowser
    {
        protected string url_ = null;

        System.Windows.Forms.AxHost.ConnectionPointCookie cookie;
        WebBrowserExtendedEvents events;

        //This method will be called to give you a chance to create your own event sink
        protected override void CreateSink()
        {
            //MAKE SURE TO CALL THE BASE or the normal events won't fire
            base.CreateSink();
            events = new WebBrowserExtendedEvents(this);
            cookie = new System.Windows.Forms.AxHost.ConnectionPointCookie(this.ActiveXInstance, events, typeof(DWebBrowserEvents2));
        }

        protected override void DetachSink()
        {
            if (null != cookie)
            {
                cookie.Disconnect();
                cookie = null;
            }
            base.DetachSink();
        }

        //This new event will fire when the page is navigating
        public event EventHandler BeforeNavigate;
        public event EventHandler BeforeNewWindow;

        protected void OnBeforeNewWindow(string url, out bool cancel)
        {
            EventHandler h = BeforeNewWindow;
            WebBrowserExtendedNavigatingEventArgs args = new WebBrowserExtendedNavigatingEventArgs(url, null);
            if (null != h)
            {
                h(this, args);
            }
            cancel = args.Cancel;
        }

        protected void OnBeforeNavigate(string url, string frame, out bool cancel)
        {
            EventHandler h = BeforeNavigate;
            WebBrowserExtendedNavigatingEventArgs args = new WebBrowserExtendedNavigatingEventArgs(url, frame);
            if (null != h)
            {
                h(this, args);
            }
            //Pass the cancellation chosen back out to the events
            cancel = args.Cancel;
        }

        //This class will capture events from the WebBrowser
        class WebBrowserExtendedEvents : System.Runtime.InteropServices.StandardOleMarshalObject, DWebBrowserEvents2
        {
            ExtendedWebBrowser _Browser;
            public WebBrowserExtendedEvents(ExtendedWebBrowser browser) { _Browser = browser; }

            //Implement whichever events you wish
            public void BeforeNavigate2(object pDisp, ref object URL, ref object flags, ref object targetFrameName, ref object postData, ref object headers, ref bool cancel)
            {
                _Browser.OnBeforeNavigate((string)URL, (string)targetFrameName, out cancel);
                _Browser.url_ = URL.ToString();
            }

            public void NewWindow3(object pDisp, ref bool cancel, ref object flags, ref object URLContext, ref object URL)
            {
                _Browser.OnBeforeNewWindow((string)URL, out cancel);
            }

        }

        [System.Runtime.InteropServices.ComImport(), System.Runtime.InteropServices.Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"),
        System.Runtime.InteropServices.InterfaceTypeAttribute(System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch),
        System.Runtime.InteropServices.TypeLibType(System.Runtime.InteropServices.TypeLibTypeFlags.FHidden)]
        public interface DWebBrowserEvents2
        {

            [System.Runtime.InteropServices.DispId(250)]
            void BeforeNavigate2(
                [System.Runtime.InteropServices.In,
                System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.IDispatch)] object pDisp,
                [System.Runtime.InteropServices.In] ref object URL,
                [System.Runtime.InteropServices.In] ref object flags,
                [System.Runtime.InteropServices.In] ref object targetFrameName, [System.Runtime.InteropServices.In] ref object postData,
                [System.Runtime.InteropServices.In] ref object headers,
                [System.Runtime.InteropServices.In,
                System.Runtime.InteropServices.Out] ref bool cancel);
            [System.Runtime.InteropServices.DispId(273)]
            void NewWindow3(
                [System.Runtime.InteropServices.In,
                System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.IDispatch)] object pDisp,
                [System.Runtime.InteropServices.In, System.Runtime.InteropServices.Out] ref bool cancel,
                [System.Runtime.InteropServices.In] ref object flags,
                [System.Runtime.InteropServices.In] ref object URLContext,
                [System.Runtime.InteropServices.In] ref object URL);
        }
    }

}


ファイル名:Classes\MyTabWebBrowser.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace ABCS.Classes
{
    public class MyTabWebBrowser : WebBrowserEx
    {
        private bool isDocumentComplete_ = false;
        private TabPage tab_ = null;
        private int titleMaxLength_ = 5;
        private Form parentForm_ = null;
        private string title_ = "";
        private string beforeurl_ = null;
        private TabControl parentTabControl_ = null;

        public string SiteTitle
        {
            get
            {
                return this.title_;
            }
        }

        public void SetTitle2Form()
        {
            this.parentForm_.Text = this.title_;
        }


        public bool IsDocumentCompleted
        {
            get
            {
                return this.isDocumentComplete_;
            }
        }
        public MyTabWebBrowser(Form parentForm, TabControl parentTabControl ,TabPage tab, int titleMaxLength)
            : base()
        {
            //this.DocumentCompleted += this.evDocumentCompleted;
            this.parentTabControl_ = parentTabControl;
            this.tab_ = tab;
            this.titleMaxLength_ = titleMaxLength;
            this.parentForm_ = parentForm;
            this.NewWindow2 += new WebBrowserNewWindow2EventHandler(webBrowser_NewWindow2);
            this.BeforeNewWindow += new EventHandler(OnMyBeforeNewWindow);

        }
        protected override void OnDocumentCompleted(WebBrowserDocumentCompletedEventArgs e)
        {
            base.OnDocumentCompleted(e);
            isDocumentComplete_ = true;

            this.title_ = this.DocumentTitle.Trim();

            this.parentForm_.Text = this.title_;
            this.tab_.Text = this.title_.Substring(0,(this.titleMaxLength_< this.title_.Length ) ? this.titleMaxLength_ : this.title_.Length);
        }

        protected override void OnNavigating(WebBrowserNavigatingEventArgs e)
        {
            base.OnNavigating(e);
            isDocumentComplete_ = false;
        }

        protected void OnMyBeforeNewWindow(object sender, EventArgs e)
        {
            WebBrowserExtendedNavigatingEventArgs we = e as WebBrowserExtendedNavigatingEventArgs;
            if (we != null)
            {
                this.beforeurl_ = we.Url;
            }
        }

        // 新しくウィンドウを開かれようとするときに発生する
        void webBrowser_NewWindow2(object sender, WebBrowserNewWindow2EventArgs e)
        {
            MyTabPageWithWeb tab = new MyTabPageWithWeb(this.parentForm_,this.parentTabControl_, this.beforeurl_);


            // 新しい WebBrowser の初期化
            //WebBrowserEx newBrowser = new WebBrowserEx();
            //newBrowser.Dock = DockStyle.Fill;

            // 新しい WebBrowser のコンテナ(下記はタブの場合)
            //var tabPage = new TabPage();
            //tab.Controls.Add(newBrowser);
            this.parentTabControl_.TabPages.Add(tab);

            // 新しい WebBrowser に表示させる設定
            //e.ppDisp = newBrowser.Application;
            //newBrowser.RegisterAsBrowser = true;
            e.ppDisp = tab.ChildWebBrowser.Application;
            tab.ChildWebBrowser.RegisterAsBrowser = true;

            // 新しい WebBrowser からさらにウィンドウを開かれるときも同じようにする
            //newBrowser.NewWindow2 += webBrowser_NewWindow2;
            tab.ChildWebBrowser.NewWindow2 += webBrowser_NewWindow2;

            //newBrowser.Navigate(this.beforeurl_);
            //tab.ChildWebBrowser.Navigate(this.beforeurl_);

            this.parentTabControl_.SelectTab(tab);

            e.Cancel = true;
        }

    }
}

タブページ

ファイル名:Classes\MyTabPageWithWeb.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using ABCS.Interfaces;

namespace ABCS.Classes
{
    public class MyTabPageWithWeb : TabPage
    {
        private MyTabWebBrowser web_ = null;
        private MyTabControlWidthRadio childTabControl_ = null;
        private TabControl parentTabControl_ = null;

        public MyTabWebBrowser ChildWebBrowser
        {
            get
            {
                return this.web_;
            }

        }

        public MyTabControlWidthRadio ChildTabControlWithRadio
        {
            get
            {
                return this.childTabControl_;
            }
        }

        public MyTabPageWithWeb(Form parentForm, TabControl parentTabControl_ , string url)
            : base()
        {
            //this.AllowDrop = true;
            this.web_ = new MyTabWebBrowser(parentForm, parentTabControl_, this, 10);

            this.childTabControl_ = new MyTabControlWidthRadio();


            /* //①Dock は後続の方が優先される(スタック形式?)
            ctabctrl.Dock = DockStyle.Top;
            tab.Controls.Add(ctabctrl);
            */

            //Web表示
            this.web_.Dock = DockStyle.Fill;
            this.Controls.Add(this.web_);

            // //②Dock は後続の方が優先される(スタック形式?)
            // //そのため、ctabctrlのaddをwebのaddより後方に移した。
            // //(①を②に移した)
            this.childTabControl_.Dock = DockStyle.Top;
            this.Controls.Add(this.childTabControl_);

            //web.DocumentCompleted += webDocumentCompleted;

            this.web_.Navigate(url);

        }
    }
}

スライドボタン

ファイル名:Classes\SlidingButton.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace ABCS.Classes
{
    public delegate void OnResizeHook4Form();

    public class SlidingButton : Button
    {
        private Point mouseDownPoint = Point.Empty;
        private Point originalPoint = Point.Empty;
        public OnResizeHook4Form resizeHook = null;


        public int avgX
        {
            get
            {
                return this.Left + this.Width / 2;
            }
        }

        public SlidingButton()
            : base()
        {
            this.MouseDown += new MouseEventHandler(SlidingButton_MouseDown1);
            //this.MouseClick += new MouseEventHandler(AnisTabControl_MouseClick);
            this.MouseUp += new MouseEventHandler(SlidingButton_MouseUp1);
            //this.MouseDoubleClick += new MouseEventHandler(AnisTabControl_MouseDoubleClick);
            //this.MouseWheel += new MouseEventHandler(AnisTabControl_MouseWheel);
            this.MouseMove += new MouseEventHandler(SlidingButton_MouseMove);

        }
        void SlidingButton_MouseDown1(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseDownPoint = e.Location;
                originalPoint = new Point(this.Top, this.Left);
            }
            else
            {
                mouseDownPoint = Point.Empty;
            }
        }

        void SlidingButton_MouseUp1(object sender, MouseEventArgs e)
        {
            mouseDownPoint = Point.Empty;
        }

        void SlidingButton_MouseMove(object sender, MouseEventArgs e)
        {
            if (mouseDownPoint != Point.Empty)
            {
                Rectangle mouseMoveRectangle = new Rectangle(
                    mouseDownPoint.X - 5, mouseDownPoint.Y - 5,
                    10, 10);
                Rectangle r = new Rectangle(new Point(0,0), this.Size);
                if (r.Contains(e.Location))
                {

                    Point p = new Point(mouseDownPoint.X - e.Location.X, mouseDownPoint.Y - e.Location.Y);
                    int x = 0;
                    if( p.X > 0 ) 
                    {
                        x = 2;
                    }
                    else 
                    {
                        x = -2;
                    }
                    this.Left -= x;
                    //this.OnMouseUp(e);

                    if (this.resizeHook != null)
                    {
                        this.resizeHook();
                    }

                    this.Refresh();
                }
            }
        }

    }
}

お気に入りを取り込むツリービュー

ファイル名:Classes\Util.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
using System.Xml;
using System.Windows.Forms;

namespace ABCS.Classes
{
    class Util
    {
    }
    public class MyFiles {
        private String rootPath_= String.Empty;
        private IList dirs = new ArrayList();

        public MyFiles( String rootPath ){
            this.rootPath_ = rootPath;
            try
            {
                foreach (string sub in Directory.GetDirectories(this.rootPath_))
                {
                    this.dirs.Add(new MyFiles(sub));
                }
            }
            catch (Exception excp)
            {
            }
        }

        public string nodeName
        {
            get
            {
                return this.rootPath_;
            }

        }

        public IList files {
            get 
            {
                IList ret = null;
                try
                {
                    ret = Directory.GetFiles(rootPath_);
                }
                catch (Exception excp)
                {
                    return (IList) ( new List() );
                }

                return ret;
            }
        }

        public IList MyDirs
        {
            get
            {
                return (IList)this.dirs;
            }
        }

        private string getFileName(string fullName)
        {
            char sep = { '\\','/' };

            string ary = fullName.Split(sep, StringSplitOptions.RemoveEmptyEntries);
            return ary[ary.Length - 1];
        }
        public TreeNode CreateNode()
        {
            TreeNode rootNode = new TreeNode(this.getFileName(this.nodeName));
            TreeNodeCollection nodes = rootNode.Nodes;

            foreach (string file in this.files)
            {
                nodes.Add(new TreeNode(this.getFileName(file)));
            }
            foreach (MyFiles sub in this.dirs)
            {
                nodes.Add(sub.CreateNode());
            }

            return rootNode;
        }
    }

}

ラジオボタンとグループ

ファイル名:MyGroup.cs

using System;
using System.Windows.Forms;
using System.ComponentModel;

/// 
/// Summary description for Class1
/// 
public class MyGroup : GroupBox
{
    private RadioButton[] rbtns = new RadioButton[5];
	public MyGroup( System.EventHandler clickEvent ) : base()
	{
		//
		// TODO: Add constructor logic here
		//
        for (int i = 0; i < 5; i++)
        {
            this.rbtns[i] = new RadioButton();
            this.rbtns[i].Dock = DockStyle.Right;
            this.rbtns[i].Click += clickEvent;
            int  num = i + 1;
            this.rbtns[i].Text = num.ToString(); 
        }
        Controls.AddRange( this.rbtns);
	}
    public RadioButton getRadioButton(int i)
    {
        if (i >= 5 || i < 0)
        {
            throw new InvalidEnumArgumentException();
        }
        return this.rbtns[i];
    }

}

ファイル名:MyTabControlWithRadio.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace ABCS
{
    public class MyTabControlWidthRadio : TabControl
    {
        public MyTabControlWidthRadio()
            : base()
        {
            //子タブ追加
            for (int i = 0; i < 5; i++)
            {
                TabPage ctab = new TabPage();
                GroupBox gbox = new MyGroup(this.OnRadioBtnClick);
                gbox.Dock = DockStyle.Top;
                ctab.Controls.Add(gbox);
                this.TabPages.Add(ctab);
            }
            TabPage ctabend = new TabPage();
            Button recbtn = new Button();
            recbtn.Text = "記録";
            recbtn.Dock = DockStyle.Right;
            ctabend.Controls.Add(recbtn);
            this.TabPages.Add(ctabend);

        }

        public void OnRadioBtnClick(object sender, System.EventArgs e)
        {
            int i = base.SelectedIndex;
            
            if (i == 5) i = 0;
            else i++;

            base.SelectedIndex = i;
        }

    }
}

フォームクラス

ファイル名:Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using ABCS.Classes;
using Microsoft.VisualBasic;
using System.Text.RegularExpressions;

namespace ABCS
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private String boomarkDir_ = null;
        private MyFiles myFiles_ = null;
        private TreeView trace_ = null;
        private TreeView recent_ = null;
        private string ez = "Provider=MSDAORA; User ID=ABCS; password=ABCS; Data Source=lsl.atnifty.com:1521/bbc";
        private string xmlurl = Application.StartupPath + "\\" + "urls.xml";
        private DataSet dataSet_ = null;

        private MySnapWebBrowser Snapped2UPweb = null;


        private void Form1_Load(object sender, EventArgs e)
        {
            #region
            #endregion
            //Tab 初期化
            {
                if (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("ABCS_SITE")))
                {
                    string site = Environment.GetEnvironmentVariable("ABCS_SITE").ToString();
                    this.ez = "Provider=MSDAORA; User ID=ABCS; password=ABCS; Data Source=" + site + ":1521/bbc";
                }
                this.WindowState = FormWindowState.Maximized;

                //MyAnisTabControl初期化
                this.tabControl.TabPages.Clear();
                this.tabView.TabPages.Clear();

                //ウインドウサイズ
                int xsize = this.Size.Width;
                int ysize = this.Size.Height;
                int ybias = 64;

                /*
                 * //古い
                //myTabPageAdmin
                {
                    myTabPageAdmin = new MyTabPageAdmin(this.tabControl);
                }
                 */

                /*
                 * //AnisTabControl, MyAnisTabControlに移行し、不要になった。
                //MouseEventHandler
                {
                    this.tabControl.MouseDoubleClick += new MouseEventHandler(this.tabControl_DoubleClick);
                }
                */

                //SlidingButton ※ReiszeWebTabUrlTab()の前であること。
                {
                    this.slidingButton4Half.Top = ysize - ybias + 2;
                    this.slidingButton4Half.Left = xsize * 50 / 100;

                    this.slidingButton4Half.resizeHook = new OnResizeHook4Form(this.ResizeWebTabUrlTab);
                }


                //ウインドウサイズに合わせて、WebTabとUrlTabをリサイズ
                this.ResizeWebTabUrlTab();


                //AnisTabControl ドラッグ&ドロップ
                {
                    this.tabControl.SortTabDragDrop = true;
                    this.tabControl.AllowDrop = true;
                }

                //Favarate
                {
                    TabPage tab = new TabPage();
                    this.treeFaverate.Dock = DockStyle.Fill;
                    tab.Controls.Add(this.treeFaverate);
                    tab.Text = "お気に入り";
                    this.tabView.TabPages.Add(tab);
                }

                //2UP
                {
                    TabPage tab = new TabPage();
                    MySnapWebBrowser web = new MySnapWebBrowser(this.tabControl, this);
                    web.Dock = DockStyle.Fill;
                    tab.Controls.Add(web);
                    tab.Text = "スナップWeb";
                    this.tabView.TabPages.Add(tab);
                    this.Snapped2UPweb = web;
                    //web.OnNewWindowSnap += this.SnapNewWindow;
                }

                //Trace
                {
                    this.trace_ = new TreeView();

                    TabPage tab = new TabPage();
                    this.trace_.Dock = DockStyle.Fill;
                    tab.Controls.Add(this.trace_);
                    tab.Text = "トレース";
                    this.tabView.TabPages.Add(tab);
                }

                //Recent
                {
                    this.recent_ = new TreeView();

                    TabPage tab = new TabPage();
                    this.recent_.Dock = DockStyle.Fill;
                    tab.Controls.Add(this.recent_);
                    tab.Text = "履歴";
                    this.tabView.TabPages.Add(tab);
                }


            }

            //お気に入りツリー 初期化
            {
                TreeNodeCollection nodes = this.treeFaverate.Nodes;

                this.boomarkDir_ = Environment.GetFolderPath(Environment.SpecialFolder.Favorites);

                this.myFiles_ = new MyFiles(this.boomarkDir_);

                this.treeFaverate.Nodes.Add(this.myFiles_.CreateNode());

            }

            //履歴ツリー 初期化
            {
                TreeNodeCollection nodes = this.recent_.Nodes;

                string recentDir = Environment.GetFolderPath(Environment.SpecialFolder.History);

                MyFiles files = new MyFiles(recentDir);

                this.recent_.Nodes.Add(files.CreateNode());

            }

        }

        #region oldbutton
        /*
        private void btnNewTab_Click(object sender, EventArgs e)
        {
            TabPage tab = new TabPage();
            WebBrowser web = new WebBrowser();
            web.Dock = DockStyle.Fill;
            tab.Controls.Add(web);

            this.tabControl.Controls.Add(tab);
            this.tabControl.SelectTab(tab);
        }
        */
        #endregion

        private string getFavarateOffset(string bookmarkDir)
        {
            Regex reg = new Regex("[\\\\][^\\\\]*$");
            return reg.Replace(bookmarkDir, ""); 
        }

        private void treeFaverate_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode node = e.Node;
            String nodefile = this.getFavarateOffset(this.boomarkDir_) + "\\" + node.FullPath;
            //MessageBox.Show(nodefile);
            openPage(nodefile);
        }

        private void openPage(string path)
        {
            if (File.Exists(path))
            {
                StreamReader reader = new StreamReader(path, Encoding.Default);
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();
                    if (line.IndexOf("URL=") == 0)
                    {
                        string url = line.Replace("URL=", "");
                        openNewTab(url);
                        break;
                    }
                }

            }
        }


        private void openNewTab(string url)
        {
            MyTabPageWithWeb tab = new MyTabPageWithWeb(this, this.tabControl,url);
            tab.AllowDrop = true;
            //ContextMenuStrip topmenu = new ContextMenuStrip();
            //ContextMenuStrip menu = web.ContextMenuStrip;

            //web.ContextMenuStrip = topmenu;

            this.trace_.Nodes.Add(new TreeNode(url));

            this.tabControl.Controls.Add(tab);
            this.tabControl.SelectTab(tab);

            //タブ・子タブ・子Webへのポインタ管理
            //this.myTabPageAdmin.Add(tab, web, ctabctrl);
            //tab.DoubleClick += this.tabPageDoubleClick;   //いらない
        }

        private void aaaToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }


        private void frmMain_Resize(object sender, EventArgs e)
        {
            ResizeWebTabUrlTab();

        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            this.slidingButton4Half.Left = this.Width / 2;
        } 
        private void ResizeWebTabUrlTab()
        {
            //ウインドウサイズ
            int xsize = this.Size.Width;
            int ysize = this.Size.Height;
            int ybias = 0;

            int topPanelY = this.pnlTopPanel.Height;

            if (this.WindowState == FormWindowState.Maximized)
            {
                ybias = 64;
            }
            else
            {
                ybias = 64;

            }


            int pct = this.slidingButton4Half.avgX * 100 / xsize;

            //TabWeb
            {
                int pnlXsize = xsize * (100 - pct) / 100;
                int pnlYsize = ysize - ybias - topPanelY;

                this.pnlPanelForTab.Size = new Size(pnlXsize, pnlYsize);
                this.pnlPanelForTab.Top = topPanelY;
                this.pnlPanelForTab.Left = xsize - pnlXsize;

                this.tabControl.AllowDrop = true;
                this.tabControl.SortTabDragDrop = true;

                //this.tabControl.Refresh();
                //this.pnlPanelForTab.Refresh();
            }

            int tabViewHeight = ysize * 90 / 100 - ybias - topPanelY;
            int tabViewWidth = xsize * pct / 100;

            //pnlButtons
            {
                this.pnlButtons.Size = new Size(xsize * pct / 100, ysize - tabViewHeight - ybias - topPanelY);
                this.pnlButtons.Top = topPanelY;
                this.pnlButtons.Left = 0;
            }

            //TabView
            {
                this.tabView.Size = new Size(tabViewWidth, tabViewHeight);
                this.tabView.Top = this.pnlButtons.Top + this.pnlButtons.Height;
                this.tabView.Left = 0;
                //this.tabView.Controls.Clear();
                //this.tabView.Refresh();
            }



            //sliderButton
            {
                this.slidingButton4Half.Top = ysize - ybias + 2;
            }

        }

        private void btnBackWeb_Click(object sender, EventArgs e)
        {
            MyTabPageWithWeb tab = this.tabControl.SelectedTab as MyTabPageWithWeb;

            if (tab != null)
            {
                tab.ChildWebBrowser.GoBack();
            }
        }

        private void btnForwardWeb_Click(object sender, EventArgs e)
        {
            MyTabPageWithWeb tab = this.tabControl.SelectedTab as MyTabPageWithWeb;

            if (tab != null)
            {
                tab.ChildWebBrowser.GoForward();
            }
        }

        private void btnSnap_Click(object sender, EventArgs e)
        {
            MyTabPageWithWeb tab = this.tabControl.SelectedTab as MyTabPageWithWeb;

            WebBrowser fromweb = tab.ChildWebBrowser;

            MySnapWebBrowser toweb = this.Snapped2UPweb;

            if (tab != null)
            {
                toweb.isSnap = true;
                toweb.Navigate(fromweb.Url);

                foreach (TabPage ctab in this.tabView.TabPages)
                {
                    if (ctab.Text == "スナップWeb")
                    {
                        this.tabView.SelectTab(ctab);
                        return;
                    }
                }


            }

        }

        private void frmMain_SizeChanged(object sender, EventArgs e)
        {
            this.ResizeWebTabUrlTab();
        }



    }
    
}