1 // Copyright (C) 2015 Kazuhiro Fujieda <fujieda@users.osdn.me>
\r
3 // Licensed under the Apache License, Version 2.0 (the "License");
\r
4 // you may not use this file except in compliance with the License.
\r
5 // You may obtain a copy of the License at
\r
7 // http://www.apache.org/licenses/LICENSE-2.0
\r
9 // Unless required by applicable law or agreed to in writing, software
\r
10 // distributed under the License is distributed on an "AS IS" BASIS,
\r
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
12 // See the License for the specific language governing permissions and
\r
13 // limitations under the License.
\r
16 using System.Drawing;
\r
17 using System.Runtime.InteropServices;
\r
18 using System.Windows.Forms;
\r
19 using static System.Math;
\r
21 namespace KancolleSniffer
\r
23 public class SwipeScrollify
\r
25 private readonly MouseFilter _filter;
\r
27 public SwipeScrollify()
\r
29 _filter = new MouseFilter();
\r
30 Application.AddMessageFilter(_filter);
\r
33 public void AddPanel(Panel panel)
\r
35 var handler = new PanelHandler(panel);
\r
36 _filter.MouseDown += handler.MouseDown;
\r
37 _filter.MouseMove += handler.MouseMove;
\r
38 _filter.MouseUp += handler.MouseUp;
\r
41 public void AddShipListPanel(ShipListPanel panel)
\r
43 var handler = new ShipListPanelHandler(panel);
\r
44 _filter.MouseDown += handler.MouseDown;
\r
45 _filter.MouseMove += handler.MouseMove;
\r
46 _filter.MouseUp += handler.MouseUp;
\r
49 public void AddTreeView(TreeView treeView)
\r
51 var handler = new TreeViewHandler(treeView);
\r
52 _filter.MouseDown += handler.MouseDown;
\r
53 _filter.MouseMove += handler.MouseMove;
\r
54 _filter.MouseUp += handler.MouseUp;
\r
57 private class MouseFilter : IMessageFilter
\r
59 public delegate void MouseHandler(IntPtr handle, ref bool handled);
\r
61 public event MouseHandler MouseMove , MouseDown , MouseUp;
\r
63 // ReSharper disable InconsistentNaming
\r
64 private const int WM_MOUSEMOVE = 0x0200;
\r
65 private const int WM_LBUTTONDOWN = 0x0201;
\r
66 private const int WM_LBUTTONUP = 0x0202;
\r
67 // ReSharper restore InconsistentNaming
\r
69 public bool PreFilterMessage(ref Message m)
\r
71 var handled = false;
\r
74 case WM_LBUTTONDOWN:
\r
75 MouseDown?.Invoke(m.HWnd, ref handled);
\r
78 MouseMove?.Invoke(m.HWnd, ref handled);
\r
81 MouseUp?.Invoke(m.HWnd, ref handled);
\r
88 private class PanelHandler
\r
90 private readonly Panel _panel;
\r
91 private bool _touch;
\r
92 private Point _mouseStart;
\r
93 private Point _panelStart;
\r
94 private Point _scrollStart;
\r
95 private const int ScrollCount = 6;
\r
97 public PanelHandler(Panel panel)
\r
102 public void MouseDown(IntPtr handle, ref bool handled)
\r
104 if (!_mouseStart.IsEmpty)
\r
106 if (!_panel.RectangleToScreen(_panel.ClientRectangle).Contains(Control.MousePosition))
\r
109 for (var control = Control.FromHandle(handle); control != null; control = control.Parent)
\r
111 if (control != _panel)
\r
118 _mouseStart = _scrollStart = Control.MousePosition;
\r
119 _panelStart = _panel.AutoScrollPosition;
\r
122 public void MouseMove(IntPtr handle, ref bool handled)
\r
124 if (_mouseStart.IsEmpty)
\r
126 var cur = Control.MousePosition;
\r
127 var dx = cur.X - _mouseStart.X;
\r
128 var dy = cur.Y - _mouseStart.Y;
\r
131 if (!(Abs(dx) > ScrollCount || Abs(dy) > ScrollCount))
\r
135 if (Abs(_scrollStart.X - cur.X) > ScrollCount || Abs(_scrollStart.Y - cur.Y) > ScrollCount)
\r
137 _panel.AutoScrollPosition = new Point(-_panelStart.X - dx, -_panelStart.Y - dy);
\r
138 _scrollStart = cur;
\r
142 public void MouseUp(IntPtr handle, ref bool handled)
\r
145 _mouseStart = _panelStart = Point.Empty;
\r
148 private class ShipListPanelHandler
\r
150 private readonly ShipListPanel _panel;
\r
151 private bool _touch;
\r
152 private Point _mouseStart;
\r
153 private int _barStart = -1;
\r
154 private Point _scrollStart;
\r
155 private const int ScrollCount = ShipListPanel.LineHeight;
\r
157 public ShipListPanelHandler(ShipListPanel panel)
\r
162 public void MouseDown(IntPtr handle, ref bool handled)
\r
164 if (!_mouseStart.IsEmpty)
\r
166 if (!_panel.RectangleToScreen(_panel.ClientRectangle).Contains(Control.MousePosition) ||
\r
167 _panel.ScrollBar.RectangleToScreen(_panel.ScrollBar.ClientRectangle).Contains(Control.MousePosition))
\r
170 for (var control = Control.FromHandle(handle); control != null; control = control.Parent)
\r
172 if (control != _panel)
\r
179 _mouseStart = _scrollStart = Control.MousePosition;
\r
180 _barStart = _panel.ScrollBar.Value;
\r
183 public void MouseMove(IntPtr handle, ref bool handled)
\r
185 if (_mouseStart.IsEmpty)
\r
187 var cur = Control.MousePosition;
\r
188 var dy = cur.Y - _mouseStart.Y;
\r
191 if (Abs(dy) <= ScrollCount)
\r
195 if (Abs(_scrollStart.Y - cur.Y) > ScrollCount)
\r
197 var bar = _panel.ScrollBar;
\r
198 bar.Value = Max(0, Min(bar.Maximum - bar.LargeChange + 1, _barStart - dy / ScrollCount));
\r
199 _scrollStart = cur;
\r
203 public void MouseUp(IntPtr handle, ref bool handled)
\r
207 _mouseStart = Point.Empty;
\r
211 private class TreeViewHandler
\r
213 private readonly TreeView _treeView;
\r
214 private bool _touch;
\r
215 private Point _mouseStart;
\r
216 private Point _panelStart;
\r
218 [DllImport("user32.dll")]
\r
219 private static extern int GetScrollPos(IntPtr hWnd, int nBar);
\r
221 [DllImport("user32.dll")]
\r
222 private static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);
\r
224 // ReSharper disable InconsistentNaming
\r
225 private const int GWL_STYLE = -16;
\r
226 private const int WS_HSCROLL = 0x00100000;
\r
227 private const int WS_VSCROLL = 0x00200000;
\r
228 // ReSharper restore InconsistentNaming
\r
230 [DllImport("user32.dll")]
\r
231 private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
\r
233 public TreeViewHandler(TreeView treeView)
\r
235 _treeView = treeView;
\r
238 public void MouseDown(IntPtr handle, ref bool handled)
\r
240 if (!_mouseStart.IsEmpty)
\r
242 var control = Control.FromHandle(handle);
\r
243 if (control == null || control != _treeView)
\r
245 if (!_treeView.RectangleToScreen(_treeView.ClientRectangle).Contains(Control.MousePosition))
\r
247 var loc = _treeView.HitTest(_treeView.PointToClient(Control.MousePosition)).Location;
\r
248 // アイテムをクリックするとWM_LBUTTONUPが来ないので避ける
\r
249 if (loc == TreeViewHitTestLocations.Label || loc == TreeViewHitTestLocations.Image)
\r
251 _mouseStart = Control.MousePosition;
\r
252 _panelStart = ScrollPosition();
\r
255 public void MouseMove(IntPtr handle, ref bool handled)
\r
257 if (_mouseStart.IsEmpty)
\r
259 if (!_treeView.RectangleToScreen(_treeView.ClientRectangle).Contains(Control.MousePosition))
\r
261 // TreeViewではうまく動かないので外に出たら止める
\r
263 _mouseStart = _panelStart = Point.Empty;
\r
266 var cur = Control.MousePosition;
\r
267 var dx = cur.X - _mouseStart.X;
\r
268 var dy = cur.Y - _mouseStart.Y;
\r
269 var style = GetWindowLong(_treeView.Handle, GWL_STYLE);
\r
272 _treeView.BeginUpdate();
\r
273 if ((style & WS_HSCROLL) != 0)
\r
274 SetScrollPos(_treeView.Handle, 0, _panelStart.X - dx, true);
\r
275 if ((style & WS_VSCROLL) != 0)
\r
276 SetScrollPos(_treeView.Handle, 1, _panelStart.Y - dy / _treeView.ItemHeight, true);
\r
277 _treeView.EndUpdate();
\r
280 else if (Abs(dx) > 5 || Abs(dy) > 5)
\r
284 public void MouseUp(IntPtr handle, ref bool handled)
\r
286 if (_touch && !_panelStart.IsEmpty && _panelStart != ScrollPosition())
\r
289 _mouseStart = _panelStart = Point.Empty;
\r
292 private Point ScrollPosition()
\r
294 return new Point(GetScrollPos(_treeView.Handle, 0), GetScrollPos(_treeView.Handle, 1));
\r