OSDN Git Service

Merge pull request #76 from upsilon/csharp8
[opentween/open-tween.git] / OpenTween / MouseWheelMessageFilter.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2015 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
10 // any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 // for more details.
16 //
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
21
22 #nullable enable
23
24 using System;
25 using System.Collections.Generic;
26 using System.Drawing;
27 using System.Linq;
28 using System.Reflection;
29 using System.Text;
30 using System.Threading.Tasks;
31 using System.Windows.Forms;
32
33 namespace OpenTween
34 {
35     /// <summary>
36     /// コントロールにフォーカスが当たってなくても無理矢理 MouseWheel イベントを発生させるクラス
37     /// </summary>
38     public class MouseWheelMessageFilter : IMessageFilter
39     {
40         private readonly List<Control> controls = new List<Control>();
41
42         public MouseWheelMessageFilter()
43             => Application.AddMessageFilter(this);
44
45         public void Register(Control target)
46             => this.controls.Add(target);
47
48         public void Unregister(Control target)
49             => this.controls.Remove(target);
50
51         public bool PreFilterMessage(ref Message m)
52         {
53             const int WM_MOUSEWHEEL = 0x020A;
54
55             if (m.Msg == WM_MOUSEWHEEL)
56             {
57                 foreach (var control in this.controls)
58                 {
59                     var details = ParseMessage(m);
60                     var controlRectangle = control.Parent.RectangleToScreen(control.DisplayRectangle);
61                     if (controlRectangle.Contains(details.ScreenLocation))
62                     {
63                         var clientLocation = control.PointToClient(details.ScreenLocation);
64
65                         var ev = new HandledMouseEventArgs(MouseButtons.None, 0, clientLocation.X, clientLocation.Y, details.WheelDelta);
66                         this.RaiseMouseWheelEvent(control, ev);
67
68                         // フォーカスが当たっているか否かに関わらず OnMouseWheel イベントを発生させているため、
69                         // 二重にイベントが発生することを防ぐために標準のメッセージ処理は抑制する
70                         return true;
71                     }
72                 }
73             }
74
75             return false;
76         }
77
78         internal class MouseEvent
79         {
80             public Point ScreenLocation { get; }
81             public int WheelDelta { get; }
82
83             public MouseEvent(Point location, int delta)
84             {
85                 this.ScreenLocation = location;
86                 this.WheelDelta = delta;
87             }
88         }
89
90         internal static MouseEvent ParseMessage(Message m)
91         {
92             var screenLocation = new Point((int)(m.LParam.ToInt64() & 0xffffffff));
93             var wheelDelta = (int)(m.WParam.ToInt64() & 0xffff0000) >> 16;
94
95             return new MouseEvent(screenLocation, wheelDelta);
96         }
97
98         public void RaiseMouseWheelEvent(Control control, MouseEventArgs e)
99         {
100             var method = typeof(Control).GetMethod("OnMouseWheel", BindingFlags.Instance | BindingFlags.NonPublic);
101             method.Invoke(control, new[] { e });
102         }
103     }
104 }