DockPaneStripBase.cs
上传用户:szlfmled
上传日期:2020-11-22
资源大小:978k
文件大小:7k
源码类别:

C#编程

开发平台:

C#

  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Security.Permissions;
  8. using System.Diagnostics.CodeAnalysis;
  9. namespace WeifenLuo.WinFormsUI.Docking
  10. {
  11. public abstract class DockPaneStripBase : Control
  12. {
  13.         [SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]        
  14.         protected internal class Tab : IDisposable
  15.         {
  16.             private IDockContent m_content;
  17.             public Tab(IDockContent content)
  18.             {
  19.                 m_content = content;
  20.             }
  21.             ~Tab()
  22.             {
  23.                 Dispose(false);
  24.             }
  25.             public IDockContent Content
  26.             {
  27.                 get { return m_content; }
  28.             }
  29.             public Form ContentForm
  30.             {
  31.                 get { return m_content as Form; }
  32.             }
  33.             public void Dispose()
  34.             {
  35.                 Dispose(true);
  36.                 GC.SuppressFinalize(this);
  37.             }
  38.             protected virtual void Dispose(bool disposing)
  39.             {
  40.             }
  41.         }
  42.         [SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]        
  43.         protected sealed class TabCollection : IEnumerable<Tab>
  44.         {
  45.             #region IEnumerable Members
  46.             IEnumerator<Tab> IEnumerable<Tab>.GetEnumerator()
  47.             {
  48.                 for (int i = 0; i < Count; i++)
  49.                     yield return this[i];
  50.             }
  51.             IEnumerator IEnumerable.GetEnumerator()
  52.             {
  53.                 for (int i = 0; i < Count; i++)
  54.                     yield return this[i];
  55.             }
  56.             #endregion
  57.             internal TabCollection(DockPane pane)
  58.             {
  59.                 m_dockPane = pane;
  60.             }
  61.             private DockPane m_dockPane;
  62.             public DockPane DockPane
  63.             {
  64.                 get { return m_dockPane; }
  65.             }
  66.             public int Count
  67.             {
  68.                 get { return DockPane.DisplayingContents.Count; }
  69.             }
  70.             public Tab this[int index]
  71.             {
  72.                 get
  73.                 {
  74.                     IDockContent content = DockPane.DisplayingContents[index];
  75.                     if (content == null)
  76.                         throw (new ArgumentOutOfRangeException("index"));
  77.                     return content.DockHandler.GetTab(DockPane.TabStripControl);
  78.                 }
  79.             }
  80.             public bool Contains(Tab tab)
  81.             {
  82.                 return (IndexOf(tab) != -1);
  83.             }
  84.             public bool Contains(IDockContent content)
  85.             {
  86.                 return (IndexOf(content) != -1);
  87.             }
  88.             public int IndexOf(Tab tab)
  89.             {
  90.                 if (tab == null)
  91.                     return -1;
  92.                 return DockPane.DisplayingContents.IndexOf(tab.Content);
  93.             }
  94.             public int IndexOf(IDockContent content)
  95.             {
  96.                 return DockPane.DisplayingContents.IndexOf(content);
  97.             }
  98.         }
  99. protected DockPaneStripBase(DockPane pane)
  100. {
  101. m_dockPane = pane;
  102. SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  103. SetStyle(ControlStyles.Selectable, false);
  104.             AllowDrop = true;
  105. }
  106. private DockPane m_dockPane;
  107. protected DockPane DockPane
  108. {
  109. get { return m_dockPane; }
  110. }
  111. protected DockPane.AppearanceStyle Appearance
  112. {
  113. get { return DockPane.Appearance; }
  114. }
  115.         private TabCollection m_tabs = null;
  116. protected TabCollection Tabs
  117. {
  118. get
  119.             {
  120.                 if (m_tabs == null)
  121.                     m_tabs = new TabCollection(DockPane);
  122.                 return m_tabs;
  123.             }
  124. }
  125. internal void RefreshChanges()
  126. {
  127.             if (IsDisposed)
  128.                 return;
  129. OnRefreshChanges();
  130. }
  131. protected virtual void OnRefreshChanges()
  132. {
  133. }
  134. protected internal abstract int MeasureHeight();
  135. protected internal abstract void EnsureTabVisible(IDockContent content);
  136. protected int HitTest()
  137. {
  138. return HitTest(PointToClient(Control.MousePosition));
  139. }
  140. protected internal abstract int HitTest(Point point);
  141. protected internal abstract GraphicsPath GetOutline(int index);
  142.         protected internal virtual Tab CreateTab(IDockContent content)
  143.         {
  144.             return new Tab(content);
  145.         }
  146.         protected override void OnMouseDown(MouseEventArgs e)
  147.         {
  148.             base.OnMouseDown(e);
  149.             int index = HitTest();
  150.             if (index != -1)
  151.             {
  152.                 IDockContent content = Tabs[index].Content;
  153.                 if (DockPane.ActiveContent != content)
  154.                     DockPane.ActiveContent = content;
  155.             }
  156.             if (e.Button == MouseButtons.Left)
  157.             {
  158.                 if (DockPane.DockPanel.AllowEndUserDocking && DockPane.AllowDockDragAndDrop && DockPane.ActiveContent.DockHandler.AllowEndUserDocking)
  159.                     DockPane.DockPanel.BeginDrag(DockPane.ActiveContent.DockHandler);
  160.             }
  161.         }
  162.         protected bool HasTabPageContextMenu
  163.         {
  164.             get { return DockPane.HasTabPageContextMenu; }
  165.         }
  166.         protected void ShowTabPageContextMenu(Point position)
  167.         {
  168.             DockPane.ShowTabPageContextMenu(this, position);
  169.         }
  170.         protected override void OnMouseUp(MouseEventArgs e)
  171.         {
  172.             base.OnMouseUp(e);
  173.             if (e.Button == MouseButtons.Right)
  174.                 ShowTabPageContextMenu(new Point(e.X, e.Y));
  175.         }
  176.         [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  177. protected override void WndProc(ref Message m)
  178. {
  179. if (m.Msg == (int)Win32.Msgs.WM_LBUTTONDBLCLK)
  180. {
  181. base.WndProc(ref m);
  182. int index = HitTest();
  183. if (DockPane.DockPanel.AllowEndUserDocking && index != -1)
  184. {
  185. IDockContent content = Tabs[index].Content;
  186.                     if (content.DockHandler.CheckDockState(!content.DockHandler.IsFloat) != DockState.Unknown)
  187.     content.DockHandler.IsFloat = !content.DockHandler.IsFloat;
  188. }
  189. return;
  190. }
  191. base.WndProc(ref m);
  192. return;
  193. }
  194.         protected override void OnDragOver(DragEventArgs drgevent)
  195.         {
  196.             base.OnDragOver(drgevent);
  197.             int index = HitTest();
  198.             if (index != -1)
  199.             {
  200.                 IDockContent content = Tabs[index].Content;
  201.                 if (DockPane.ActiveContent != content)
  202.                     DockPane.ActiveContent = content;
  203.             }
  204.         }
  205. }
  206. }