-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathWindowWatcher.cs
177 lines (143 loc) · 6.83 KB
/
WindowWatcher.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Timers;
namespace TTMulti
{
/// <summary>
/// Global watcher to notify when windows are activated, deactivated, or closed.
/// Windows must be added to the watch list to be notified.
/// TODO: set the synchronizing object to a form
/// </summary>
class WindowWatcher
{
public static WindowWatcher Instance { get; } = new WindowWatcher();
/// <summary>
/// A watched window was activated or deactivated
/// </summary>
public event EventHandler<Events.WindowActivatedEventArgs> ActiveWindowChanged;
/// <summary>
/// A watched window was closed
/// </summary>
public event EventHandler<Events.WindowClosedEventArgs> WindowClosed;
/// <summary>
/// The client area size of a watched window was changed
/// </summary>
public event EventHandler<Events.WindowClientAreaSizeChangedEventArgs> WindowClientAreaSizeChanged;
/// <summary>
/// The client area location of a watched window was moved
/// </summary>
public event EventHandler<Events.WindowClientAreaLocationChangedEventArgs> WindowClientAreaLocationChanged;
/// <summary>
/// The show state (minimized, normal, maximized) of a watched window was changed
/// </summary>
public event EventHandler<Events.WindowShowStateChangedEventArgs> WindowShowStateChanged;
/// <summary>
/// Synchronizing object for event callbacks
/// </summary>
public ISynchronizeInvoke SynchronizingObject
{
get => watchTimer.SynchronizingObject;
set => watchTimer.SynchronizingObject = value;
}
private class WindowInfo
{
public Size ClientAreaSize { get; set; }
public Point ClientAreaScreenLocation { get; set; }
public Win32.ShowWindowCommands ShowState { get; set; }
public WindowInfo(Size clientAreaSize, Point clientAreaScreenLocation, Win32.ShowWindowCommands showState)
{
ClientAreaSize = clientAreaSize;
ClientAreaScreenLocation = clientAreaScreenLocation;
ShowState = showState;
}
}
private List<IntPtr> watchedWindowHandles = new List<IntPtr>();
private Dictionary<IntPtr, WindowInfo> lastWindowInfos = new Dictionary<IntPtr, WindowInfo>();
private IntPtr lastActiveWindowHandle = IntPtr.Zero;
private Timer watchTimer = new Timer(15);
private WindowWatcher()
{
watchTimer.Elapsed += Timer_Elapsed;
watchTimer.AutoReset = false;
watchTimer.Start();
}
/// <summary>
/// Add a window handle to be notified when the window is closed or it becomes active
/// </summary>
public void WatchWindow(IntPtr windowHandle)
{
if (!watchedWindowHandles.Contains(windowHandle))
{
watchedWindowHandles.Add(windowHandle);
}
}
/// <summary>
/// Stop notifications for a window
/// </summary>
public void StopWatchingWindow(IntPtr windowHandle)
{
watchedWindowHandles.Remove(windowHandle);
lastWindowInfos.Remove(windowHandle);
}
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
// Check if active window has changed
IntPtr activeWindowHandle = Win32.GetForegroundWindow();
if (activeWindowHandle != lastActiveWindowHandle)
{
// Only notify on watched windows
if (watchedWindowHandles.Contains(lastActiveWindowHandle) || watchedWindowHandles.Contains(activeWindowHandle))
{
ActiveWindowChanged?.Invoke(this, new Events.WindowActivatedEventArgs(lastActiveWindowHandle, activeWindowHandle));
}
lastActiveWindowHandle = activeWindowHandle;
}
foreach (IntPtr windowHandle in watchedWindowHandles.ToArray())
{
// Check if window has been closed
if (!Win32.IsWindow(windowHandle))
{
WindowClosed?.Invoke(this, new Events.WindowClosedEventArgs(windowHandle));
watchedWindowHandles.Remove(windowHandle);
lastWindowInfos.Remove(windowHandle);
continue;
}
Size clientAreaSize = Win32.GetWindowClientAreaSize(windowHandle);
Point clientAreaLocation = Win32.GetWindowClientAreaLocation(windowHandle);
Win32.ShowWindowCommands showState = Win32.GetWindowShowState(windowHandle);
if (lastWindowInfos.ContainsKey(windowHandle))
{
WindowInfo lastInfo = lastWindowInfos[windowHandle];
if (lastInfo.ShowState != showState)
{
WindowShowStateChanged?.Invoke(this, new Events.WindowShowStateChangedEventArgs(windowHandle, lastInfo.ShowState, showState));
lastInfo.ShowState = showState;
}
if (lastInfo.ClientAreaSize != clientAreaSize)
{
WindowClientAreaSizeChanged?.Invoke(this, new Events.WindowClientAreaSizeChangedEventArgs(windowHandle, lastInfo.ClientAreaSize, clientAreaSize));
lastInfo.ClientAreaSize = clientAreaSize;
}
if (lastInfo.ClientAreaScreenLocation != clientAreaLocation)
{
WindowClientAreaLocationChanged?.Invoke(this, new Events.WindowClientAreaLocationChangedEventArgs(windowHandle, lastInfo.ClientAreaScreenLocation, clientAreaLocation));
lastInfo.ClientAreaScreenLocation = clientAreaLocation;
}
}
else
{
lastWindowInfos.Add(windowHandle, new WindowInfo(clientAreaSize, clientAreaLocation, showState));
WindowShowStateChanged?.Invoke(this, new Events.WindowShowStateChangedEventArgs(windowHandle, Win32.ShowWindowCommands.Hide, showState));
WindowClientAreaSizeChanged?.Invoke(this, new Events.WindowClientAreaSizeChangedEventArgs(windowHandle, Size.Empty, clientAreaSize));
WindowClientAreaLocationChanged?.Invoke(this, new Events.WindowClientAreaLocationChangedEventArgs(windowHandle, Point.Empty, clientAreaLocation));
}
}
watchTimer.Start();
}
}
}