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