tangxu
2024-10-22 7a0d1efa4784d176a32f182ecae671711e98ca92
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Docking.Crown;
using DPumpHydr.WinFrmUI.RLT.Enum.Crown;
using System;
using System.Drawing;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Native
{
    #region DockResizeFilterNative
 
    public class DockResizeFilter : IMessageFilter
    {
        #region Field Region
 
        private readonly CrownDockPanel _dockPanel;
 
        private readonly Timer _dragTimer;
        private bool _isDragging;
        private Point _initialContact;
        private CrownDockSplitter _activeSplitter;
 
        #endregion
 
        #region Constructor Region
 
        public DockResizeFilter(CrownDockPanel dockPanel)
        {
            _dockPanel = dockPanel;
 
            _dragTimer = new Timer
            {
                Interval = 1
            };
            _dragTimer.Tick += DragTimer_Tick;
        }
 
        #endregion
 
        #region IMessageFilter Region
 
        public bool PreFilterMessage(ref Message m)
        {
            // We only care about mouse events
            if (m.Msg is not (((int)WM.MOUSEMOVE) or ((int)WM.LBUTTONDOWN) or ((int)WM.LBUTTONUP) or ((int)WM.LBUTTONDBLCLK) or ((int)WM.RBUTTONDOWN) or ((int)WM.RBUTTONUP) or ((int)WM.RBUTTONDBLCLK)))
            {
                return false;
            }
 
            // Stop drag.
            if (m.Msg == (int)WM.LBUTTONUP)
            {
                if (_isDragging)
                {
                    StopDrag();
                    return true;
                }
            }
 
            // Exit out early if we're simply releasing a non-splitter drag over the area
            if (m.Msg == (int)WM.LBUTTONUP && !_isDragging)
            {
                return false;
            }
 
            // Force cursor if already dragging.
            if (_isDragging)
            {
                Cursor.Current = _activeSplitter.ResizeCursor;
            }
 
            // Return out early if we're dragging something that's not a splitter.
            if (m.Msg == (int)WM.MOUSEMOVE && !_isDragging && _dockPanel.MouseButtonState != MouseButtons.None)
            {
                return false;
            }
 
            // Try and create a control from the message handle.
            Control control = Control.FromHandle(m.HWnd);
 
            // Exit out if we didn't manage to create a control.
            if (control == null)
            {
                return false;
            }
 
            // Exit out if the control is not the dock panel or a child control.
            if (!(control == _dockPanel || _dockPanel.Contains(control)))
            {
                return false;
            }
 
            // Update the mouse cursor
            CheckCursor();
 
            // Start drag.
            if (m.Msg == (int)WM.LBUTTONDOWN)
            {
                CrownDockSplitter hotSplitter = HotSplitter();
                if (hotSplitter != null)
                {
                    StartDrag(hotSplitter);
                    return true;
                }
            }
 
            // Stop events passing through if we're hovering over a splitter
            if (HotSplitter() != null)
            {
                return true;
            }
 
            // Stop all events from going through if we're dragging a splitter.
            if (_isDragging)
            {
                return true;
            }
 
            return false;
        }
 
        #endregion
 
        #region Event Handler Region
 
        private void DragTimer_Tick(object sender, EventArgs e)
        {
            if (_dockPanel.MouseButtonState != MouseButtons.Left)
            {
                StopDrag();
                return;
            }
 
            Point difference = new(_initialContact.X - Cursor.Position.X, _initialContact.Y - Cursor.Position.Y);
            _activeSplitter.UpdateOverlay(difference);
        }
 
        #endregion
 
        #region Method Region
 
        private void StartDrag(CrownDockSplitter splitter)
        {
            _activeSplitter = splitter;
            Cursor.Current = _activeSplitter.ResizeCursor;
 
            _initialContact = Cursor.Position;
            _isDragging = true;
 
            _activeSplitter.ShowOverlay();
            _dragTimer.Start();
        }
 
        private void StopDrag()
        {
            _dragTimer.Stop();
            _activeSplitter.HideOverlay();
 
            Point difference = new(_initialContact.X - Cursor.Position.X, _initialContact.Y - Cursor.Position.Y);
            _activeSplitter.Move(difference);
 
            _isDragging = false;
        }
 
        private CrownDockSplitter HotSplitter()
        {
            foreach (CrownDockSplitter splitter in _dockPanel.Splitters)
            {
                if (splitter.Bounds.Contains(Cursor.Position))
                {
                    return splitter;
                }
            }
 
            return null;
        }
 
        private void CheckCursor()
        {
            if (_isDragging)
            {
                return;
            }
 
            CrownDockSplitter hotSplitter = HotSplitter();
            if (hotSplitter != null)
            {
                Cursor.Current = hotSplitter.ResizeCursor;
            }
        }
 
        private void ResetCursor()
        {
            Cursor.Current = Cursors.Default;
            CheckCursor();
        }
 
        #endregion
    }
 
    #endregion
}