using System.ComponentModel; using System.Drawing; namespace System.Windows.Forms { public class SplitContainer : ContainerControl, ISupportInitialize { private Splitter Splitter { get; } public SplitterPanel Panel1 { get; } public SplitterPanel Panel2 { get; } public int Panel1MinSize { get; set; } // TODO public int Panel2MinSize { get; set; } // TODO private Orientation orientation; public Orientation Orientation { get => orientation; set { if (orientation != value) { orientation = value; SuspendLayout(); if (value == Orientation.Vertical) { if (FixedPanel == FixedPanel.Panel2) { Panel1.Dock = DockStyle.Fill; Splitter.Dock = DockStyle.Right; Panel2.Dock = DockStyle.Right; } else { Panel1.Dock = DockStyle.Left; Splitter.Dock = DockStyle.Left; Panel2.Dock = DockStyle.Fill; } Splitter.Width = 3; } else { if (FixedPanel == FixedPanel.Panel2) { Panel1.Dock = DockStyle.Fill; Splitter.Dock = DockStyle.Bottom; Panel2.Dock = DockStyle.Bottom; } else { Panel1.Dock = DockStyle.Top; Splitter.Dock = DockStyle.Top; Panel2.Dock = DockStyle.Fill; } Splitter.Height = 3; } ResumeLayout(); } } } public int SplitterDistance { get => Orientation == Orientation.Vertical ? Panel1.Width : Panel1.Height; set { if (Orientation == Orientation.Vertical) { if (FixedPanel == FixedPanel.Panel1) Panel1.Width = value; else Panel2.Width = Width - value - Splitter.Width; } else { if (FixedPanel == FixedPanel.Panel1) Panel1.Height = value; else Panel2.Height = Height - value - Splitter.Height; } } } public FixedPanel FixedPanel { get => Panel1.Dock != DockStyle.Fill ? FixedPanel.Panel1 : FixedPanel.Panel2; set { if (value == FixedPanel.Panel1) { Panel1.Dock = Orientation == Orientation.Vertical ? DockStyle.Left : DockStyle.Top; Splitter.Dock = Panel1.Dock; Panel2.Dock = DockStyle.Fill; Controls.SetChildIndex(Panel1, 2); Controls.SetChildIndex(Panel2, 0); } else if (value == FixedPanel.Panel2) { Panel2.Dock = Orientation == Orientation.Vertical ? DockStyle.Right : DockStyle.Bottom; Splitter.Dock = Panel2.Dock; Panel1.Dock = DockStyle.Fill; Controls.SetChildIndex(Panel1, 0); Controls.SetChildIndex(Panel2, 2); } } } public bool Panel1Collapsed { get => !Panel1.Visible; set { Panel1.Visible = !value; Splitter.Visible = !value; } } public bool Panel2Collapsed { get => !Panel2.Visible; set { Panel2.Visible = !value; Splitter.Visible = !value; } } public int SplitterWidth { get => Orientation == Orientation.Vertical ? Splitter.Width : Splitter.Height; set { if (Orientation == Orientation.Vertical) Splitter.Width = value; else Splitter.Height = value; } } public override Color BackColor { get => base.BackColor; set { base.BackColor = value; Splitter.BackColor = value; } } public event SplitterEventHandler SplitterMoved; protected virtual void OnSplitterMoved(SplitterEventArgs e) => SplitterMoved?.Invoke(this, e); public void BeginInit() { } public void EndInit() { } public SplitContainer() { SetContentControl(new Windows.Controls.ContentControl()); Panel1 = new SplitterPanel(); Splitter = new Splitter(); Splitter.Width = 3; Panel2 = new SplitterPanel(); Controls.AddRange(new Control[] { Panel2, Splitter, Panel1 }); Orientation = Orientation.Vertical; FixedPanel = FixedPanel.Panel1; Splitter.SplitterMoved += (s, e) => OnSplitterMoved(e); } } }