OSDN Git Service

サムネイル画像にフォーカスがない状態でも MouseWheel イベントを無理矢理発生させる
[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 using System;
23 using System.Collections.Generic;
24 using System.Drawing;
25 using System.Linq;
26 using System.Reflection;
27 using System.Text;
28 using System.Threading.Tasks;
29 using System.Windows.Forms;
30
31 namespace OpenTween
32 {
33     /// <summary>
34     /// コントロールにフォーカスが当たってなくても無理矢理 MouseWheel イベントを発生させるクラス
35     /// </summary>
36     public class MouseWheelMessageFilter : IMessageFilter
37     {
38         private readonly List<Control> controls = new List<Control>();
39
40         public MouseWheelMessageFilter()
41         {
42             Application.AddMessageFilter(this);
43         }
44
45         public void Register(Control target)
46         {
47             this.controls.Add(target);
48         }
49
50         public void Unregister(Control target)
51         {
52             this.controls.Remove(target);
53         }
54
55         public bool PreFilterMessage(ref Message m)
56         {
57             const int WM_MOUSEWHEEL = 0x020A;
58
59             if (m.Msg == WM_MOUSEWHEEL)
60             {
61                 foreach (var control in this.controls)
62                 {
63                     // フォーカスが当たっている時は何もしなくても MouseWheel イベントが発生するので無視
64                     if (control.Focused)
65                         return false;
66
67                     var screenLocation = new Point((int)m.LParam);
68                     var controlRectangle = control.Parent.RectangleToScreen(control.DisplayRectangle);
69                     if (controlRectangle.Contains(screenLocation))
70                     {
71                         var clientLocation = control.PointToClient(screenLocation);
72                         var wheelDelta = (int)m.WParam >> 16;
73
74                         var ev = new HandledMouseEventArgs(MouseButtons.None, 0, clientLocation.X, clientLocation.Y, wheelDelta);
75                         this.RaiseMouseWheelEvent(control, ev);
76
77                         return true;
78                     }
79                 }
80             }
81
82             return false;
83         }
84
85         public void RaiseMouseWheelEvent(Control control, MouseEventArgs e)
86         {
87             var method = typeof(Control).GetMethod("OnMouseWheel", BindingFlags.Instance | BindingFlags.NonPublic);
88             method.Invoke(control, new[] { e });
89         }
90     }
91 }