Widow – Diff between revs 11 and 12
?pathlinks?
Rev 11 | Rev 12 | |||
---|---|---|---|---|
1 | using System; |
1 | using System; |
|
2 | using System.Collections.Generic; |
2 | using System.Collections.Generic; |
|
3 | using System.Diagnostics; |
3 | using System.Diagnostics; |
|
4 | using System.Text; |
4 | using System.Text; |
|
5 | |
5 | |
|
6 | namespace Widow |
6 | namespace Widow |
|
7 | { |
7 | { |
|
8 | public static class Helpers |
8 | public static class Helpers |
|
9 | { |
9 | { |
|
10 | #region Public Methods |
10 | #region Public Methods |
|
11 | |
11 | |
|
12 | /// <summary> Get the text for the window pointed to by hWnd </summary> |
12 | /// <summary> Get the text for the window pointed to by hWnd </summary> |
|
13 | public static string GetWindowText(IntPtr hWnd) |
13 | public static string GetWindowText(IntPtr hWnd) |
|
14 | { |
14 | { |
|
15 | var size = Natives.GetWindowTextLength(hWnd); |
15 | var size = Natives.GetWindowTextLength(hWnd); |
|
16 | if (size > 0) |
16 | if (size > 0) |
|
17 | { |
17 | { |
|
18 | var builder = new StringBuilder(size + 1); |
18 | var builder = new StringBuilder(size + 1); |
|
19 | Natives.GetWindowText(hWnd, builder, builder.Capacity); |
19 | Natives.GetWindowText(hWnd, builder, builder.Capacity); |
|
20 | return builder.ToString(); |
20 | return builder.ToString(); |
|
21 | } |
21 | } |
|
22 | |
22 | |
|
23 | return string.Empty; |
23 | return string.Empty; |
|
24 | } |
24 | } |
|
25 | |
25 | |
|
26 | /// <summary> Find all windows that match the given filter </summary> |
26 | /// <summary> Find all windows that match the given filter </summary> |
|
27 | /// <param name="filter"> |
27 | /// <param name="filter"> |
|
28 | /// A delegate that returns true for windows |
28 | /// A delegate that returns true for windows |
|
29 | /// that should be returned and false for windows that should |
29 | /// that should be returned and false for windows that should |
|
30 | /// not be returned |
30 | /// not be returned |
|
31 | /// </param> |
31 | /// </param> |
|
32 | public static IEnumerable<IntPtr> FindWindows(Natives.EnumWindowsProc filter) |
32 | public static IEnumerable<IntPtr> FindWindows(Natives.EnumWindowsProc filter) |
|
33 | { |
33 | { |
|
34 | var windows = new List<IntPtr>(); |
34 | var windows = new List<IntPtr>(); |
|
35 | |
35 | |
|
36 | Natives.EnumWindows(delegate(IntPtr wnd, IntPtr param) |
36 | Natives.EnumWindows(delegate(IntPtr wnd, IntPtr param) |
|
37 | { |
37 | { |
|
38 | if (filter(wnd, param)) |
38 | if (filter(wnd, param)) |
|
39 | { |
39 | { |
|
40 | // only add the windows that pass the filter |
40 | // only add the windows that pass the filter |
|
41 | windows.Add(wnd); |
41 | windows.Add(wnd); |
|
42 | } |
42 | } |
|
43 | |
43 | |
|
44 | // but return true here so that we iterate all windows |
44 | // but return true here so that we iterate all windows |
|
45 | return true; |
45 | return true; |
|
46 | }, IntPtr.Zero); |
46 | }, IntPtr.Zero); |
|
47 | |
47 | |
|
48 | return windows; |
48 | return windows; |
|
49 | } |
49 | } |
|
- | 50 | |
||
- | 51 | public static IEnumerable<IntPtr> EnumerateWindows() |
||
- | 52 | { |
||
- | 53 | var windows = new List<IntPtr>(); |
||
- | 54 | |
||
- | 55 | Natives.EnumWindows(delegate(IntPtr wnd, IntPtr param) |
||
- | 56 | { |
||
- | 57 | windows.Add(wnd); |
||
- | 58 | return true; |
||
- | 59 | }, IntPtr.Zero); |
||
- | 60 | |
||
- | 61 | return windows; |
||
- | 62 | } |
||
50 | |
63 | |
|
51 | /// <summary> Find all windows that contain the given title text </summary> |
64 | /// <summary> Find all windows that contain the given title text </summary> |
|
52 | /// <param name="titleText"> The text that the window title must contain. </param> |
65 | /// <param name="titleText"> The text that the window title must contain. </param> |
|
53 | public static IEnumerable<IntPtr> FindWindowsWithText(string titleText) |
66 | public static IEnumerable<IntPtr> FindWindowsWithText(string titleText) |
|
54 | { |
67 | { |
|
55 | return FindWindows((wnd, param) => GetWindowText(wnd).Contains(titleText)); |
68 | return FindWindows((wnd, param) => GetWindowText(wnd).Contains(titleText)); |
|
56 | } |
69 | } |
|
57 | |
70 | |
|
58 | public static string GetWindowTitle(IntPtr hWnd) |
71 | public static string GetWindowTitle(IntPtr hWnd) |
|
59 | { |
72 | { |
|
60 | var length = Natives.GetWindowTextLength(hWnd) + 1; |
73 | var length = Natives.GetWindowTextLength(hWnd) + 1; |
|
61 | var title = new StringBuilder(length); |
74 | var title = new StringBuilder(length); |
|
62 | Natives.GetWindowText(hWnd, title, length); |
75 | Natives.GetWindowText(hWnd, title, length); |
|
63 | return title.ToString(); |
76 | return title.ToString(); |
|
64 | } |
77 | } |
|
65 | |
- | ||
66 | #endregion |
- | ||
67 | |
78 | |
|
68 | public static string GetProcessName(IntPtr hWnd) |
79 | public static string GetProcessName(IntPtr hWnd) |
|
69 | { |
80 | { |
|
70 | Natives.GetWindowThreadProcessId(hWnd, out var pid); |
81 | Natives.GetWindowThreadProcessId(hWnd, out var pid); |
|
71 | var process = Process.GetProcessById((int)pid); |
82 | var process = Process.GetProcessById((int) pid); |
|
72 | return process.ProcessName; |
83 | return process.ProcessName; |
|
73 | |
- | ||
74 | } |
84 | } |
|
- | 85 | |
||
- | 86 | #endregion |
||
75 | } |
87 | } |
|
76 | } |
88 | } |
|
77 | |
89 | |
|
78 |
|
90 |
|
|
79 | |
91 | |
|
80 | |
92 | |
|
81 | |
93 | |