DockWindow.cs 8.4 KB

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