1 using System;
2 using System.Diagnostics;
3 using System.Drawing;
4 using System.Drawing.Drawing2D;
5 using System.Windows.Forms;
6 
7 namespace WeifenLuo.WinFormsUI.Docking
8 {
9     internal class VS2012SplitterControl : DockPane.SplitterControlBase
10     {
11         private readonly SolidBrush _horizontalBrush;
12         private readonly SolidBrush _backgroundBrush;
13         private PathGradientBrush _foregroundBrush;
14         private readonly Color[] _verticalSurroundColors;
15         private int SplitterSize { get; }
16 
VS2012SplitterControl(DockPane pane)17         public VS2012SplitterControl(DockPane pane)
18             : base(pane)
19         {
20             _horizontalBrush = pane.DockPanel.Theme.PaintingService.GetBrush(pane.DockPanel.Theme.ColorPalette.TabSelectedInactive.Background);
21             _backgroundBrush = pane.DockPanel.Theme.PaintingService.GetBrush(pane.DockPanel.Theme.ColorPalette.MainWindowActive.Background);
22             _verticalSurroundColors = new[]
23             {
24                 pane.DockPanel.Theme.ColorPalette.MainWindowActive.Background
25             };
26             SplitterSize = pane.DockPanel.Theme.Measures.SplitterSize;
27         }
28 
OnSizeChanged(EventArgs e)29         protected override void OnSizeChanged(EventArgs e)
30         {
31             base.OnSizeChanged(e);
32             Rectangle rect = ClientRectangle;
33             if (rect.Width <= 0 || rect.Height <= 0)
34                 return;
35 
36             if (Alignment != DockAlignment.Left && Alignment != DockAlignment.Right)
37                 return;
38 
39             MakeForegroundBrush(rect);
40         }
41 
MakeForegroundBrush(Rectangle rect)42         private void MakeForegroundBrush(Rectangle rect)
43         {
44             _foregroundBrush?.Dispose();
45             using (var path = new GraphicsPath())
46             {
47                 path.AddRectangle(rect);
48                 _foregroundBrush = new PathGradientBrush(path)
49                 {
50                     CenterColor = _horizontalBrush.Color,
51                     SurroundColors = _verticalSurroundColors
52                 };
53             }
54         }
55 
Dispose(bool disposing)56         protected override void Dispose(bool disposing)
57         {
58             if (!IsDisposed && disposing)
59             {
60                 _foregroundBrush?.Dispose();
61             }
62 
63             base.Dispose(disposing);
64         }
65 
OnPaint(PaintEventArgs e)66         protected override void OnPaint(PaintEventArgs e)
67         {
68             base.OnPaint(e);
69 
70             Rectangle rect = ClientRectangle;
71             if (rect.Width <= 0 || rect.Height <= 0)
72                 return;
73 
74             switch (Alignment)
75             {
76                 case DockAlignment.Right:
77                 case DockAlignment.Left:
78                     {
79                         Debug.Assert(SplitterSize == rect.Width);
80 
81                         if (_foregroundBrush == null)
82                             MakeForegroundBrush(rect);
83 
84                         e.Graphics.FillRectangle(_backgroundBrush, rect);
85                         e.Graphics.FillRectangle(_foregroundBrush, rect.X + SplitterSize / 2 - 1, rect.Y,
86                                                     SplitterSize / 3, rect.Height);
87                     }
88                     break;
89                 case DockAlignment.Bottom:
90                 case DockAlignment.Top:
91                     {
92                         Debug.Assert(SplitterSize == rect.Height);
93                         e.Graphics.FillRectangle(_horizontalBrush, rect);
94                     }
95                     break;
96             }
97         }
98     }
99 }