DockPanel.SplitterDragHandler.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:DockPanel.cs
  5. * 2.功能描述:类文件
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2014/09/01 1.00 新建
  9. *******************************************************************************/
  10. using System.Drawing;
  11. using System.Windows.Forms;
  12. namespace Dongke.IBOSS.PRD.Basics.DockPanel
  13. {
  14. /// <summary>
  15. /// 类文件
  16. /// </summary>
  17. partial class DockPanel
  18. {
  19. private sealed class SplitterDragHandler : DragHandler
  20. {
  21. private class SplitterOutline
  22. {
  23. public SplitterOutline()
  24. {
  25. m_dragForm = new DragForm();
  26. SetDragForm(Rectangle.Empty);
  27. DragForm.BackColor = Color.Black;
  28. DragForm.Opacity = 0.7;
  29. DragForm.Show(false);
  30. }
  31. DragForm m_dragForm;
  32. private DragForm DragForm
  33. {
  34. get
  35. {
  36. return m_dragForm;
  37. }
  38. }
  39. public void Show(Rectangle rect)
  40. {
  41. SetDragForm(rect);
  42. }
  43. public void Close()
  44. {
  45. DragForm.Close();
  46. }
  47. private void SetDragForm(Rectangle rect)
  48. {
  49. DragForm.Bounds = rect;
  50. if (rect == Rectangle.Empty)
  51. DragForm.Region = new Region(Rectangle.Empty);
  52. else if (DragForm.Region != null)
  53. DragForm.Region = null;
  54. }
  55. }
  56. public SplitterDragHandler(DockPanel dockPanel)
  57. : base(dockPanel)
  58. {
  59. }
  60. public new ISplitterDragSource DragSource
  61. {
  62. get
  63. {
  64. return base.DragSource as ISplitterDragSource;
  65. }
  66. private set
  67. {
  68. base.DragSource = value;
  69. }
  70. }
  71. private SplitterOutline m_outline;
  72. private SplitterOutline Outline
  73. {
  74. get
  75. {
  76. return m_outline;
  77. }
  78. set
  79. {
  80. m_outline = value;
  81. }
  82. }
  83. private Rectangle m_rectSplitter;
  84. private Rectangle RectSplitter
  85. {
  86. get
  87. {
  88. return m_rectSplitter;
  89. }
  90. set
  91. {
  92. m_rectSplitter = value;
  93. }
  94. }
  95. public void BeginDrag(ISplitterDragSource dragSource, Rectangle rectSplitter)
  96. {
  97. DragSource = dragSource;
  98. RectSplitter = rectSplitter;
  99. if (!BeginDrag())
  100. {
  101. DragSource = null;
  102. return;
  103. }
  104. Outline = new SplitterOutline();
  105. Outline.Show(rectSplitter);
  106. DragSource.BeginDrag(rectSplitter);
  107. }
  108. protected override void OnDragging()
  109. {
  110. Outline.Show(GetSplitterOutlineBounds(Control.MousePosition));
  111. }
  112. protected override void OnEndDrag(bool abort)
  113. {
  114. DockPanel.SuspendLayout(true);
  115. Outline.Close();
  116. if (!abort)
  117. DragSource.MoveSplitter(GetMovingOffset(Control.MousePosition));
  118. DragSource.EndDrag();
  119. DockPanel.ResumeLayout(true, true);
  120. }
  121. private int GetMovingOffset(Point ptMouse)
  122. {
  123. Rectangle rect = GetSplitterOutlineBounds(ptMouse);
  124. if (DragSource.IsVertical)
  125. return rect.X - RectSplitter.X;
  126. else
  127. return rect.Y - RectSplitter.Y;
  128. }
  129. private Rectangle GetSplitterOutlineBounds(Point ptMouse)
  130. {
  131. Rectangle rectLimit = DragSource.DragLimitBounds;
  132. Rectangle rect = RectSplitter;
  133. if (rectLimit.Width <= 0 || rectLimit.Height <= 0)
  134. return rect;
  135. if (DragSource.IsVertical)
  136. {
  137. rect.X += ptMouse.X - StartMousePosition.X;
  138. rect.Height = rectLimit.Height;
  139. }
  140. else
  141. {
  142. rect.Y += ptMouse.Y - StartMousePosition.Y;
  143. rect.Width = rectLimit.Width;
  144. }
  145. if (rect.Left < rectLimit.Left)
  146. rect.X = rectLimit.X;
  147. if (rect.Top < rectLimit.Top)
  148. rect.Y = rectLimit.Y;
  149. if (rect.Right > rectLimit.Right)
  150. rect.X -= rect.Right - rectLimit.Right;
  151. if (rect.Bottom > rectLimit.Bottom)
  152. rect.Y -= rect.Bottom - rectLimit.Bottom;
  153. return rect;
  154. }
  155. }
  156. private SplitterDragHandler m_splitterDragHandler = null;
  157. private SplitterDragHandler GetSplitterDragHandler()
  158. {
  159. if (m_splitterDragHandler == null)
  160. m_splitterDragHandler = new SplitterDragHandler(this);
  161. return m_splitterDragHandler;
  162. }
  163. internal void BeginDrag(ISplitterDragSource dragSource, Rectangle rectSplitter)
  164. {
  165. GetSplitterDragHandler().BeginDrag(dragSource, rectSplitter);
  166. }
  167. }
  168. }