NoBorderTabControl.cs
上传用户:hbhltzc
上传日期:2022-06-04
资源大小:1925k
文件大小:8k
源码类别:

xml/soap/webservice

开发平台:

Visual C++

  1. using System;
  2. using System.Collections;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.Drawing;
  6. namespace XmlNotepad {
  7.     public class NoBorderTabControlEventArgs {
  8.         NoBorderTabPage page;
  9.         public NoBorderTabControlEventArgs(NoBorderTabPage page){
  10.             this.page = page;
  11.         }
  12.         public NoBorderTabPage TabPage {
  13.             get { return this.page; }
  14.         }
  15.     }
  16.     public delegate void NoBorderTabControlEventHandler(object sender, NoBorderTabControlEventArgs args);
  17.     public class NoBorderTabControl : UserControl {
  18.         TabControl tabs;
  19.         TabPageCollection pages;
  20.         public delegate void PageEventHandler(object sender, PageEventArgs args);
  21.         public event NoBorderTabControlEventHandler Selected;
  22.         public NoBorderTabControl() {
  23.             pages = new TabPageCollection();
  24.             tabs = new TabControl();
  25.             
  26.             this.Controls.Add(tabs);
  27.             pages.PageAdded += new PageEventHandler(OnPageAdded);
  28.             pages.PageRemoved += new PageEventHandler(OnPageRemoved);
  29.             tabs.SelectedIndexChanged += new EventHandler(OnTabsSelectedIndexChanged);
  30.         }
  31.         public int SelectedIndex {
  32.             get { return tabs.SelectedIndex; }
  33.             set { tabs.SelectedIndex = value; }
  34.         }
  35.         public NoBorderTabPage SelectedTab {
  36.             get { 
  37.                 return (NoBorderTabPage)pages[tabs.SelectedIndex];
  38.             }
  39.             set {
  40.                 foreach (NoBorderTabPage p in pages) {
  41.                     if (p == value) {
  42.                         this.tabs.SelectedTab = p.Page;
  43.                         break;
  44.                     }
  45.                 }
  46.             }
  47.         }
  48.         void OnTabsSelectedIndexChanged(object sender, EventArgs e) {
  49.             TabPage page = tabs.SelectedTab;
  50.             foreach (NoBorderTabPage p in pages) {
  51.                 if (p.Page == page) {
  52.                     if (Selected != null) {
  53.                         Selected(this, new NoBorderTabControlEventArgs(p));
  54.                     }
  55.                     this.Controls.SetChildIndex(p, 0); // put it on top!
  56.                     break;
  57.                 }
  58.             }
  59.         }
  60.         void OnPageRemoved(object sender, PageEventArgs e) {
  61.             NoBorderTabPage page = e.Page;
  62.             tabs.TabPages.Remove(page.Page);
  63.             if (this.Controls.Contains(page)) {
  64.                 this.Controls.Remove(page);
  65.             }
  66.         }
  67.         void OnPageAdded(object sender, PageEventArgs e) {
  68.             NoBorderTabPage page = e.Page;
  69.             if (e.Index >= tabs.TabPages.Count) {
  70.                 tabs.TabPages.Add(page.Page);
  71.             } else {
  72.                 tabs.TabPages.Insert(e.Index, page.Page);
  73.             }
  74.             if (!this.Controls.Contains(page)) {
  75.                 this.Controls.Add(page);
  76.                 this.Controls.SetChildIndex(page, this.TabPages.IndexOf(page));
  77.             }
  78.         }
  79.         protected override void OnControlAdded(ControlEventArgs e) {
  80.             base.OnControlAdded(e);
  81.             NoBorderTabPage page = e.Control as NoBorderTabPage;
  82.             if (page != null && !tabs.TabPages.Contains(page.Page)) {
  83.                 pages.Add(page);
  84.             }
  85.         }
  86.         protected override void OnControlRemoved(ControlEventArgs e) {
  87.             base.OnControlRemoved(e);
  88.             NoBorderTabPage page = e.Control as NoBorderTabPage;
  89.             if (page != null && tabs.TabPages.Contains(page.Page)) {
  90.                 pages.Remove(page);
  91.             }
  92.         }
  93.         protected override void OnLayout(LayoutEventArgs e) {
  94.             tabs.MinimumSize = new Size(10, 10);
  95.             Size s = tabs.GetPreferredSize(new Size(this.Width, 20));
  96.             int height = tabs.ItemSize.Height + tabs.Padding.Y;
  97.             tabs.Bounds = new Rectangle(0, 0, this.Width, height);
  98.             foreach (NoBorderTabPage p in this.TabPages) {
  99.                 p.Bounds = new Rectangle(0, height, this.Width, this.Height - height);
  100.             }
  101.         }
  102.         public TabPageCollection TabPages { 
  103.             get {
  104.                 return pages;
  105.             }
  106.         }
  107.         public class PageEventArgs : EventArgs {
  108.             int index;
  109.             NoBorderTabPage page;
  110.             public PageEventArgs(NoBorderTabPage page, int index) {
  111.                 this.page = page;
  112.                 this.index = index;
  113.             }
  114.             public NoBorderTabPage Page {
  115.                 get {
  116.                     return this.page;
  117.                 }
  118.                 set {
  119.                     this.page = value;
  120.                 }
  121.             }
  122.             public int Index {
  123.                 get {
  124.                     return this.index;
  125.                 }
  126.                 set {
  127.                     this.index = value;
  128.                 }
  129.             }
  130.         }
  131.         public class TabPageCollection : IList {
  132.             ArrayList list = new ArrayList();
  133.             public event PageEventHandler PageAdded;
  134.             public event PageEventHandler PageRemoved;
  135.             void OnPageAdded(NoBorderTabPage page, int index) {
  136.                 PageAdded(this, new PageEventArgs(page, index));
  137.             }
  138.             void OnPageRemoved(NoBorderTabPage page) {
  139.                 PageRemoved(this, new PageEventArgs(page, 0));
  140.             }
  141.             #region IList Members
  142.             public int Add(object value) {
  143.                 int index = list.Count;
  144.                 list.Add(value);
  145.                 OnPageAdded((NoBorderTabPage)value, index);
  146.                 return index;
  147.             }
  148.             public void Clear() {
  149.                 foreach (NoBorderTabPage page in list) {
  150.                     OnPageRemoved(page);
  151.                 }
  152.                 list.Clear();
  153.             }
  154.             public bool Contains(object value) {
  155.                 return list.Contains(value);
  156.             }
  157.             public int IndexOf(object value) {
  158.                 return list.IndexOf(value);
  159.             }
  160.             public void Insert(int index, object value) {
  161.                 list.Insert(index, value);
  162.                 OnPageAdded((NoBorderTabPage)value, index);
  163.             }
  164.             public bool IsFixedSize {
  165.                 get { return false; }
  166.             }
  167.             public bool IsReadOnly {
  168.                 get { return false; }
  169.             }
  170.             public void Remove(object value) {
  171.                 OnPageRemoved((NoBorderTabPage)value); 
  172.                 list.Remove(value);
  173.             }
  174.             public void RemoveAt(int index) {
  175.                 if (index >= 0 && index < list.Count) {
  176.                     OnPageRemoved((NoBorderTabPage)list[index]);
  177.                     list.RemoveAt(index);
  178.                 }
  179.             }
  180.             public object this[int index] {
  181.                 get {
  182.                     return list[index];
  183.                 }
  184.                 set {
  185.                     RemoveAt(index);
  186.                     if (value != null) {
  187.                         Insert(index, value);
  188.                     }
  189.                 }
  190.             }
  191.             #endregion
  192.             #region ICollection Members
  193.             public void CopyTo(Array array, int index) {
  194.                 list.CopyTo(array, index);
  195.             }
  196.             public int Count {
  197.                 get { return list.Count;  }
  198.             }
  199.             public bool IsSynchronized {
  200.                 get { return list.IsSynchronized; }
  201.             }
  202.             public object SyncRoot {
  203.                 get { return list.SyncRoot; }
  204.             }
  205.             #endregion
  206.             #region IEnumerable Members
  207.             public IEnumerator GetEnumerator() {
  208.                 return list.GetEnumerator();
  209.             }
  210.             #endregion
  211.         }
  212.     }
  213.     public class NoBorderTabPage : Panel {
  214.         TabPage page = new TabPage();
  215.         public NoBorderTabPage() {            
  216.         }
  217.         [System.ComponentModel.Browsable(false)]
  218.         internal TabPage Page {
  219.             get { return this.page; }
  220.         }
  221.         public override string Text { 
  222.             get {
  223.                 return this.page.Text;
  224.             }
  225.             set {
  226.                 this.page.Text = value;
  227.             }
  228.         }
  229.     }
  230. }