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

C#编程

开发平台:

C#

  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing;
  4. using System.Runtime.InteropServices;
  5. using System.ComponentModel;
  6. namespace WeifenLuo.WinFormsUI.Docking
  7. {
  8. [ToolboxItem(false)]
  9. public partial class DockWindow : Panel, INestedPanesContainer, ISplitterDragSource
  10. {
  11. private DockPanel m_dockPanel;
  12. private DockState m_dockState;
  13. private SplitterControl m_splitter;
  14. private NestedPaneCollection m_nestedPanes;
  15. internal DockWindow(DockPanel dockPanel, DockState dockState)
  16. {
  17. m_nestedPanes = new NestedPaneCollection(this);
  18. m_dockPanel = dockPanel;
  19. m_dockState = dockState;
  20. Visible = false;
  21. SuspendLayout();
  22. if (DockState == DockState.DockLeft || DockState == DockState.DockRight ||
  23. DockState == DockState.DockTop || DockState == DockState.DockBottom)
  24. {
  25. m_splitter = new SplitterControl();
  26. Controls.Add(m_splitter);
  27. }
  28. if (DockState == DockState.DockLeft)
  29. {
  30. Dock = DockStyle.Left;
  31. m_splitter.Dock = DockStyle.Right;
  32. }
  33. else if (DockState == DockState.DockRight)
  34. {
  35. Dock = DockStyle.Right;
  36. m_splitter.Dock = DockStyle.Left;
  37. }
  38. else if (DockState == DockState.DockTop)
  39. {
  40. Dock = DockStyle.Top;
  41. m_splitter.Dock = DockStyle.Bottom;
  42. }
  43. else if (DockState == DockState.DockBottom)
  44. {
  45. Dock = DockStyle.Bottom;
  46. m_splitter.Dock = DockStyle.Top;
  47. }
  48. else if (DockState == DockState.Document)
  49. {
  50. Dock = DockStyle.Fill;
  51. }
  52. ResumeLayout();
  53. }
  54. public VisibleNestedPaneCollection VisibleNestedPanes
  55. {
  56. get { return NestedPanes.VisibleNestedPanes; }
  57. }
  58. public NestedPaneCollection NestedPanes
  59. {
  60. get { return m_nestedPanes; }
  61. }
  62. public DockPanel DockPanel
  63. {
  64. get { return m_dockPanel; }
  65. }
  66. public DockState DockState
  67. {
  68. get { return m_dockState; }
  69. }
  70. public bool IsFloat
  71. {
  72. get { return DockState == DockState.Float; }
  73. }
  74. internal DockPane DefaultPane
  75. {
  76. get { return VisibleNestedPanes.Count == 0 ? null : VisibleNestedPanes[0]; }
  77. }
  78. public virtual Rectangle DisplayingRectangle
  79. {
  80. get
  81. {
  82. Rectangle rect = ClientRectangle;
  83. // if DockWindow is document, exclude the border
  84. if (DockState == DockState.Document)
  85. {
  86. rect.X += 1;
  87. rect.Y += 1;
  88. rect.Width -= 2;
  89. rect.Height -= 2;
  90. }
  91. // exclude the splitter
  92. else if (DockState == DockState.DockLeft)
  93. rect.Width -= Measures.SplitterSize;
  94. else if (DockState == DockState.DockRight)
  95. {
  96. rect.X += Measures.SplitterSize;
  97. rect.Width -= Measures.SplitterSize;
  98. }
  99. else if (DockState == DockState.DockTop)
  100. rect.Height -= Measures.SplitterSize;
  101. else if (DockState == DockState.DockBottom)
  102. {
  103. rect.Y += Measures.SplitterSize;
  104. rect.Height -= Measures.SplitterSize;
  105. }
  106. return rect;
  107. }
  108. }
  109. protected override void OnPaint(PaintEventArgs e)
  110. {
  111. // if DockWindow is document, draw the border
  112.             if (DockState == DockState.Document)
  113.                 e.Graphics.DrawRectangle(SystemPens.ControlDark, ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width - 1, ClientRectangle.Height - 1);
  114. base.OnPaint(e);
  115. }
  116. protected override void OnLayout(LayoutEventArgs levent)
  117. {
  118. VisibleNestedPanes.Refresh();
  119. if (VisibleNestedPanes.Count == 0)
  120. {
  121.                 if (Visible)
  122.                     Visible = false;
  123. }
  124. else if (!Visible)
  125. {
  126. Visible = true;
  127. VisibleNestedPanes.Refresh();
  128. }
  129. base.OnLayout (levent);
  130. }
  131.         #region ISplitterDragSource Members
  132.         void ISplitterDragSource.BeginDrag(Rectangle rectSplitter)
  133.         {
  134.         }
  135.         void ISplitterDragSource.EndDrag()
  136.         {
  137.         }
  138.         bool ISplitterDragSource.IsVertical
  139.         {
  140.             get { return (DockState == DockState.DockLeft || DockState == DockState.DockRight); }
  141.         }
  142.         Rectangle ISplitterDragSource.DragLimitBounds
  143.         {
  144.             get
  145.             {
  146.                 Rectangle rectLimit = DockPanel.DockArea;
  147.                 Point location;
  148.                 if ((Control.ModifierKeys & Keys.Shift) == 0)
  149.                     location = Location;
  150.                 else
  151.                     location = DockPanel.DockArea.Location;
  152.                 if (((ISplitterDragSource)this).IsVertical)
  153.                 {
  154.                     rectLimit.X += MeasurePane.MinSize;
  155.                     rectLimit.Width -= 2 * MeasurePane.MinSize;
  156.                     rectLimit.Y = location.Y;
  157.                     if ((Control.ModifierKeys & Keys.Shift) == 0)
  158.                         rectLimit.Height = Height;
  159.                 }
  160.                 else
  161.                 {
  162.                     rectLimit.Y += MeasurePane.MinSize;
  163.                     rectLimit.Height -= 2 * MeasurePane.MinSize;
  164.                     rectLimit.X = location.X;
  165.                     if ((Control.ModifierKeys & Keys.Shift) == 0)
  166.                         rectLimit.Width = Width;
  167.                 }
  168.                 return DockPanel.RectangleToScreen(rectLimit);
  169.             }
  170.         }
  171.         void ISplitterDragSource.MoveSplitter(int offset)
  172.         {
  173.             if ((Control.ModifierKeys & Keys.Shift) != 0)
  174.                 SendToBack();
  175.             Rectangle rectDockArea = DockPanel.DockArea;
  176.             if (DockState == DockState.DockLeft && rectDockArea.Width > 0)
  177.             {
  178.                 if (DockPanel.DockLeftPortion > 1)
  179.                     DockPanel.DockLeftPortion = Width + offset;
  180.                 else
  181.                     DockPanel.DockLeftPortion += ((double)offset) / (double)rectDockArea.Width;
  182.             }
  183.             else if (DockState == DockState.DockRight && rectDockArea.Width > 0)
  184.             {
  185.                 if (DockPanel.DockRightPortion > 1)
  186.                     DockPanel.DockRightPortion = Width - offset;
  187.                 else
  188.                     DockPanel.DockRightPortion -= ((double)offset) / (double)rectDockArea.Width;
  189.             }
  190.             else if (DockState == DockState.DockBottom && rectDockArea.Height > 0)
  191.             {
  192.                 if (DockPanel.DockBottomPortion > 1)
  193.                     DockPanel.DockBottomPortion = Height - offset;
  194.                 else
  195.                     DockPanel.DockBottomPortion -= ((double)offset) / (double)rectDockArea.Height;
  196.             }
  197.             else if (DockState == DockState.DockTop && rectDockArea.Height > 0)
  198.             {
  199.                 if (DockPanel.DockTopPortion > 1)
  200.                     DockPanel.DockTopPortion = Height + offset;
  201.                 else
  202.                     DockPanel.DockTopPortion += ((double)offset) / (double)rectDockArea.Height;
  203.             }
  204.         }
  205.         #region IDragSource Members
  206.         Control IDragSource.DragControl
  207.         {
  208.             get { return this; }
  209.         }
  210.         #endregion
  211.         #endregion
  212.     }
  213. }