SplitContainer.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System.ComponentModel;
  2. using System.Drawing;
  3. namespace System.Windows.Forms
  4. {
  5. public class SplitContainer : ContainerControl, ISupportInitialize
  6. {
  7. private Splitter Splitter { get; }
  8. public SplitterPanel Panel1 { get; }
  9. public SplitterPanel Panel2 { get; }
  10. public int Panel1MinSize { get; set; } // TODO
  11. public int Panel2MinSize { get; set; } // TODO
  12. private Orientation orientation;
  13. public Orientation Orientation
  14. {
  15. get => orientation;
  16. set
  17. {
  18. if (orientation != value)
  19. {
  20. orientation = value;
  21. SuspendLayout();
  22. if (value == Orientation.Vertical)
  23. {
  24. if (FixedPanel == FixedPanel.Panel2)
  25. {
  26. Panel1.Dock = DockStyle.Fill;
  27. Splitter.Dock = DockStyle.Right;
  28. Panel2.Dock = DockStyle.Right;
  29. }
  30. else
  31. {
  32. Panel1.Dock = DockStyle.Left;
  33. Splitter.Dock = DockStyle.Left;
  34. Panel2.Dock = DockStyle.Fill;
  35. }
  36. Splitter.Width = 3;
  37. }
  38. else
  39. {
  40. if (FixedPanel == FixedPanel.Panel2)
  41. {
  42. Panel1.Dock = DockStyle.Fill;
  43. Splitter.Dock = DockStyle.Bottom;
  44. Panel2.Dock = DockStyle.Bottom;
  45. }
  46. else
  47. {
  48. Panel1.Dock = DockStyle.Top;
  49. Splitter.Dock = DockStyle.Top;
  50. Panel2.Dock = DockStyle.Fill;
  51. }
  52. Splitter.Height = 3;
  53. }
  54. ResumeLayout();
  55. }
  56. }
  57. }
  58. public int SplitterDistance
  59. {
  60. get => Orientation == Orientation.Vertical ? Panel1.Width : Panel1.Height;
  61. set
  62. {
  63. if (Orientation == Orientation.Vertical)
  64. {
  65. if (FixedPanel == FixedPanel.Panel1)
  66. Panel1.Width = value;
  67. else
  68. Panel2.Width = Width - value - Splitter.Width;
  69. }
  70. else
  71. {
  72. if (FixedPanel == FixedPanel.Panel1)
  73. Panel1.Height = value;
  74. else
  75. Panel2.Height = Height - value - Splitter.Height;
  76. }
  77. }
  78. }
  79. public FixedPanel FixedPanel
  80. {
  81. get => Panel1.Dock != DockStyle.Fill ? FixedPanel.Panel1 : FixedPanel.Panel2;
  82. set
  83. {
  84. if (value == FixedPanel.Panel1)
  85. {
  86. Panel1.Dock = Orientation == Orientation.Vertical ? DockStyle.Left : DockStyle.Top;
  87. Splitter.Dock = Panel1.Dock;
  88. Panel2.Dock = DockStyle.Fill;
  89. Controls.SetChildIndex(Panel1, 2);
  90. Controls.SetChildIndex(Panel2, 0);
  91. }
  92. else if (value == FixedPanel.Panel2)
  93. {
  94. Panel2.Dock = Orientation == Orientation.Vertical ? DockStyle.Right : DockStyle.Bottom;
  95. Splitter.Dock = Panel2.Dock;
  96. Panel1.Dock = DockStyle.Fill;
  97. Controls.SetChildIndex(Panel1, 0);
  98. Controls.SetChildIndex(Panel2, 2);
  99. }
  100. }
  101. }
  102. public bool Panel1Collapsed
  103. {
  104. get => !Panel1.Visible;
  105. set
  106. {
  107. Panel1.Visible = !value;
  108. Splitter.Visible = !value;
  109. }
  110. }
  111. public bool Panel2Collapsed
  112. {
  113. get => !Panel2.Visible;
  114. set
  115. {
  116. Panel2.Visible = !value;
  117. Splitter.Visible = !value;
  118. }
  119. }
  120. public int SplitterWidth
  121. {
  122. get => Orientation == Orientation.Vertical ? Splitter.Width : Splitter.Height;
  123. set
  124. {
  125. if (Orientation == Orientation.Vertical)
  126. Splitter.Width = value;
  127. else
  128. Splitter.Height = value;
  129. }
  130. }
  131. public override Color BackColor
  132. {
  133. get => base.BackColor;
  134. set
  135. {
  136. base.BackColor = value;
  137. Splitter.BackColor = value;
  138. }
  139. }
  140. public event SplitterEventHandler SplitterMoved;
  141. protected virtual void OnSplitterMoved(SplitterEventArgs e) => SplitterMoved?.Invoke(this, e);
  142. public void BeginInit() { }
  143. public void EndInit() { }
  144. public SplitContainer()
  145. {
  146. SetContentControl(new Windows.Controls.ContentControl());
  147. Panel1 = new SplitterPanel();
  148. Splitter = new Splitter();
  149. Splitter.Width = 3;
  150. Panel2 = new SplitterPanel();
  151. Controls.AddRange(new Control[] { Panel2, Splitter, Panel1 });
  152. Orientation = Orientation.Vertical;
  153. FixedPanel = FixedPanel.Panel1;
  154. Splitter.SplitterMoved += (s, e) => OnSplitterMoved(e);
  155. }
  156. }
  157. }