-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathHwndUtil.cs
More file actions
197 lines (178 loc) · 7.35 KB
/
HwndUtil.cs
File metadata and controls
197 lines (178 loc) · 7.35 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
namespace wv2util
{
public static class HwndUtil
{
public static int GetWindowProcessId(IntPtr hwnd)
{
int processId = 0;
try
{
PInvoke.User32.GetWindowThreadProcessId(hwnd, out processId);
}
catch (Exception e)
{
Trace.WriteLine("Exception related to GetWindowThreadProcessId " + e);
}
return processId;
}
public static IntPtr GetChildWindow(IntPtr hwnd)
{
IntPtr result = IntPtr.Zero;
try
{
result = PInvoke.User32.GetWindow(hwnd, PInvoke.User32.GetWindowCommands.GW_CHILD);
}
catch (Exception e)
{
Trace.WriteLine("Exception related to GetWindow " + e);
}
return result;
}
public delegate bool HwndFilterCallback(IntPtr hwnd);
public static IEnumerable<IntPtr> GetTopLevelHwnds(HwndFilterCallback filterCallback = null, bool includeHwndMessage = false)
{
IntPtr child = IntPtr.Zero;
var results = GetChildWindows(IntPtr.Zero, filterCallback);
if (includeHwndMessage)
{
// HWND_MESSAGE = -3
results = results.Concat(GetChildWindows((IntPtr)(-3), filterCallback));
}
if (filterCallback != null)
{
results = results.Where(hwnd => filterCallback(hwnd));
}
return results;
}
public static Dictionary<int, List<IntPtr>> CreatePidToHwndsMapFromHwnds(IEnumerable<IntPtr> hwnds)
{
Dictionary<int, List<IntPtr>> pidToHwndMap = new Dictionary<int, List<IntPtr>>();
// Turn the list of hwnds into a dictionary of pid to hwnd list.
foreach (IntPtr hwnd in hwnds)
{
int hwndPid = GetWindowProcessId(hwnd);
if (!pidToHwndMap.TryGetValue(hwndPid, out List<IntPtr> hwndList))
{
hwndList = new List<IntPtr>();
pidToHwndMap.Add(hwndPid, hwndList);
}
hwndList.Add(hwnd);
}
return pidToHwndMap;
}
public static Dictionary<int, List<IntPtr>> GetPidToTopLevelHwndsMap(HwndFilterCallback filterCallback = null)
{
return CreatePidToHwndsMapFromHwnds(GetTopLevelHwnds(filterCallback));
}
public static IEnumerable<IntPtr> GetChildWindows(IntPtr parentHwnd, HwndFilterCallback filterCallback = null)
{
HashSet<IntPtr> hwnds = new HashSet<IntPtr>();
try
{
IntPtr child = IntPtr.Zero;
while ((child = PInvoke.User32.FindWindowEx(parentHwnd, child, null, null)) != IntPtr.Zero)
{
hwnds.Add(child);
}
}
catch (Exception e)
{
// Ignore exceptions trying to find windows.
Trace.WriteLine("FindWindowEx related error: " + e);
}
// EnumChildWindows finds HWNDs that FindWindowEx does not and vice versa.
// So we run both and collect HWNDs from both.
try
{
HwndFilterCallback enumChildrenCallback = childHwnd =>
{
hwnds.Add(childHwnd);
// Always return true to indicate to keep enumerating.
return true;
};
PInvoke.User32.EnumChildWindows(parentHwnd, Marshal.GetFunctionPointerForDelegate(enumChildrenCallback), IntPtr.Zero);
}
catch (Exception e)
{
Trace.WriteLine("EnumChildWindows related exception " + e);
}
IEnumerable<IntPtr> hwndsResult = hwnds;
if (filterCallback != null)
{
hwndsResult = hwndsResult.Where(hwnd => filterCallback(hwnd));
}
return hwndsResult;
}
public static HashSet<IntPtr> GetDescendantWindows(
IntPtr parentHwnd, // Parent HWND for which to find all children and children of children
HwndFilterCallback filterExploreCallback = null, // Delegate return true to explore its children
HwndFilterCallback filterResultCallback = null) // Delegate return true to include in returned list
{
List<IntPtr> borderHwnds = new List<IntPtr>();
HashSet<IntPtr> resultHwnds = new HashSet<IntPtr>();
borderHwnds.Add(parentHwnd);
do
{
// .NET doesn't want us to modify the collection while we enumerate it
// so we have an expandBorderHwnds which contains the next list of border
// HWNDs to examine after we're done with the current list.
List<IntPtr> expandBorderHwnds = new List<IntPtr>();
foreach (IntPtr borderHwnd in borderHwnds)
{
if (filterExploreCallback == null || filterExploreCallback(borderHwnd))
{
expandBorderHwnds.AddRange(GetChildWindows(borderHwnd));
}
if (filterResultCallback == null || filterResultCallback(borderHwnd))
{
resultHwnds.Add(borderHwnd);
}
}
borderHwnds = expandBorderHwnds;
}
while (borderHwnds.Count > 0);
return resultHwnds;
}
public static string GetClassName(IntPtr hwnd)
{
const int bufferSize = 256;
string className = "";
try
{
unsafe
{
char* buffer = stackalloc char[bufferSize];
PInvoke.User32.GetClassName(hwnd, buffer, bufferSize);
className = new string(buffer);
}
}
catch (Exception e)
{
Trace.WriteLine("Exception related to GetClassName " + e);
}
return className;
}
public static System.Windows.Rect ToSystemWindowsRect(this PInvoke.RECT rectAsPInvokeRect)
{
System.Windows.Rect rectAsSystemWindowsRect = new System.Windows.Rect();
rectAsSystemWindowsRect.X = rectAsPInvokeRect.left;
rectAsSystemWindowsRect.Y = rectAsPInvokeRect.top;
rectAsSystemWindowsRect.Width = rectAsPInvokeRect.right - rectAsPInvokeRect.left;
rectAsSystemWindowsRect.Height = rectAsPInvokeRect.bottom - rectAsPInvokeRect.top;
return rectAsSystemWindowsRect;
}
public static System.Windows.Rect GetWindowRect(IntPtr hwnd)
{
if (!PInvoke.User32.GetWindowRect(hwnd, out var rect))
{
throw new PInvoke.Win32Exception(PInvoke.Kernel32.GetLastError());
}
return rect.ToSystemWindowsRect();
}
}
}