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 AddTreeView(TreeView treeView)
\r
43 var handler = new TreeViewHandler(treeView);
\r
44 _filter.MouseDown += handler.MouseDown;
\r
45 _filter.MouseMove += handler.MouseMove;
\r
46 _filter.MouseUp += handler.MouseUp;
\r
49 private class MouseFilter : IMessageFilter
\r
51 public delegate void MouseHandler(IntPtr handle, ref bool handled);
\r
53 public event MouseHandler MouseMove , MouseDown , MouseUp;
\r
55 // ReSharper disable InconsistentNaming
\r
56 private const int WM_MOUSEMOVE = 0x0200;
\r
57 private const int WM_LBUTTONDOWN = 0x0201;
\r
58 private const int WM_LBUTTONUP = 0x0202;
\r
59 // ReSharper restore InconsistentNaming
\r
61 public bool PreFilterMessage(ref Message m)
\r
63 var handled = false;
\r
66 case WM_LBUTTONDOWN:
\r
67 MouseDown?.Invoke(m.HWnd, ref handled);
\r
70 MouseMove?.Invoke(m.HWnd, ref handled);
\r
73 MouseUp?.Invoke(m.HWnd, ref handled);
\r
80 private class PanelHandler
\r
82 private readonly Panel _panel;
\r
83 private bool _touch;
\r
84 private Point _mouseStart;
\r
85 private Point _panelStart;
\r
86 private Point _scrollStart;
\r
87 private const int ScrollCount = 6;
\r
89 public PanelHandler(Panel panel)
\r
94 public void MouseDown(IntPtr handle, ref bool handled)
\r
96 if (!_mouseStart.IsEmpty)
\r
98 if (!_panel.RectangleToScreen(_panel.ClientRectangle).Contains(Control.MousePosition))
\r
101 for (var control = Control.FromHandle(handle); control != null; control = control.Parent)
\r
103 if (control != _panel)
\r
110 _mouseStart = _scrollStart = Control.MousePosition;
\r
111 _panelStart = _panel.AutoScrollPosition;
\r
114 public void MouseMove(IntPtr handle, ref bool handled)
\r
116 if (_mouseStart.IsEmpty)
\r
118 var cur = Control.MousePosition;
\r
119 var dx = cur.X - _mouseStart.X;
\r
120 var dy = cur.Y - _mouseStart.Y;
\r
123 if (!(Abs(dx) > ScrollCount || Abs(dy) > ScrollCount))
\r
127 if (Abs(_scrollStart.X - cur.X) > ScrollCount || Abs(_scrollStart.Y - cur.Y) > ScrollCount)
\r
129 _panel.AutoScrollPosition = new Point(-_panelStart.X - dx, -_panelStart.Y - dy);
\r
130 _scrollStart = cur;
\r
134 public void MouseUp(IntPtr handle, ref bool handled)
\r
136 if (_touch && !_panelStart.IsEmpty && _panelStart != _panel.AutoScrollPosition)
\r
139 _mouseStart = _panelStart = Point.Empty;
\r
143 private class TreeViewHandler
\r
145 private readonly TreeView _treeView;
\r
146 private bool _touch;
\r
147 private Point _mouseStart;
\r
148 private Point _panelStart;
\r
150 [DllImport("user32.dll")]
\r
151 private static extern int GetScrollPos(IntPtr hWnd, int nBar);
\r
153 [DllImport("user32.dll")]
\r
154 private static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);
\r
156 // ReSharper disable InconsistentNaming
\r
157 private const int GWL_STYLE = -16;
\r
158 private const int WS_HSCROLL = 0x00100000;
\r
159 private const int WS_VSCROLL = 0x00200000;
\r
160 // ReSharper restore InconsistentNaming
\r
162 [DllImport("user32.dll")]
\r
163 private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
\r
165 public TreeViewHandler(TreeView treeView)
\r
167 _treeView = treeView;
\r
170 public void MouseDown(IntPtr handle, ref bool handled)
\r
172 if (!_mouseStart.IsEmpty)
\r
174 var control = Control.FromHandle(handle);
\r
175 if (control == null || control != _treeView)
\r
177 if (!_treeView.RectangleToScreen(_treeView.ClientRectangle).Contains(Control.MousePosition))
\r
179 var loc = _treeView.HitTest(_treeView.PointToClient(Control.MousePosition)).Location;
\r
180 // アイテムをクリックするとWM_LBUTTONUPが来ないので避ける
\r
181 if (loc == TreeViewHitTestLocations.Label || loc == TreeViewHitTestLocations.Image)
\r
183 _mouseStart = Control.MousePosition;
\r
184 _panelStart = ScrollPosition();
\r
187 public void MouseMove(IntPtr handle, ref bool handled)
\r
189 if (_mouseStart.IsEmpty)
\r
191 if (!_treeView.RectangleToScreen(_treeView.ClientRectangle).Contains(Control.MousePosition))
\r
193 // TreeViewではうまく動かないので外に出たら止める
\r
195 _mouseStart = _panelStart = Point.Empty;
\r
198 var cur = Control.MousePosition;
\r
199 var dx = cur.X - _mouseStart.X;
\r
200 var dy = cur.Y - _mouseStart.Y;
\r
201 var style = GetWindowLong(_treeView.Handle, GWL_STYLE);
\r
204 _treeView.BeginUpdate();
\r
205 if ((style & WS_HSCROLL) != 0)
\r
206 SetScrollPos(_treeView.Handle, 0, _panelStart.X - dx, true);
\r
207 if ((style & WS_VSCROLL) != 0)
\r
208 SetScrollPos(_treeView.Handle, 1, _panelStart.Y - dy / _treeView.ItemHeight, true);
\r
209 _treeView.EndUpdate();
\r
212 else if (Abs(dx) > 5 || Abs(dy) > 5)
\r
216 public void MouseUp(IntPtr handle, ref bool handled)
\r
218 if (_touch && !_panelStart.IsEmpty && _panelStart != ScrollPosition())
\r
221 _mouseStart = _panelStart = Point.Empty;
\r
224 private Point ScrollPosition()
\r
226 return new Point(GetScrollPos(_treeView.Handle, 0), GetScrollPos(_treeView.Handle, 1));
\r