Widow – Diff between revs 12 and 14
?pathlinks?
Rev 12 | Rev 14 | |||
---|---|---|---|---|
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 | |
50 | |
|
51 | public static IEnumerable<IntPtr> EnumerateWindows() |
51 | public static string GetWindowClass(IntPtr hWnd) |
|
52 | { |
52 | { |
|
53 | var windows = new List<IntPtr>(); |
- | ||
54 | |
53 | var windowClass = new StringBuilder(256); |
|
55 | Natives.EnumWindows(delegate(IntPtr wnd, IntPtr param) |
- | ||
56 | { |
- | ||
57 | windows.Add(wnd); |
- | ||
58 | return true; |
- | ||
59 | }, IntPtr.Zero); |
- | ||
60 | |
54 | Natives.GetClassName(hWnd, windowClass, windowClass.Capacity); |
|
61 | return windows; |
- | ||
62 | } |
- | ||
63 | |
- | ||
64 | /// <summary> Find all windows that contain the given title text </summary> |
- | ||
65 | /// <param name="titleText"> The text that the window title must contain. </param> |
- | ||
66 | public static IEnumerable<IntPtr> FindWindowsWithText(string titleText) |
- | ||
67 | { |
- | ||
68 | return FindWindows((wnd, param) => GetWindowText(wnd).Contains(titleText)); |
55 | return windowClass.ToString(); |
|
69 | } |
56 | } |
|
70 | |
57 | |
|
71 | public static string GetWindowTitle(IntPtr hWnd) |
58 | public static string GetWindowTitle(IntPtr hWnd) |
|
72 | { |
59 | { |
|
73 | var length = Natives.GetWindowTextLength(hWnd) + 1; |
60 | var length = Natives.GetWindowTextLength(hWnd) + 1; |
|
74 | var title = new StringBuilder(length); |
61 | var title = new StringBuilder(length); |
|
75 | Natives.GetWindowText(hWnd, title, length); |
62 | Natives.GetWindowText(hWnd, title, length); |
|
76 | return title.ToString(); |
63 | return title.ToString(); |
|
77 | } |
64 | } |
|
78 | |
65 | |
|
79 | public static string GetProcessName(IntPtr hWnd) |
66 | public static string GetProcessName(IntPtr hWnd) |
|
80 | { |
67 | { |
|
81 | Natives.GetWindowThreadProcessId(hWnd, out var pid); |
68 | Natives.GetWindowThreadProcessId(hWnd, out var pid); |
|
82 | var process = Process.GetProcessById((int) pid); |
69 | var process = Process.GetProcessById((int) pid); |
|
83 | return process.ProcessName; |
70 | return process.ProcessName; |
|
84 | } |
71 | } |
|
85 | |
72 | |
|
86 | #endregion |
73 | #endregion |
|
87 | } |
74 | } |
|
88 | } |
75 | } |
|
89 | |
76 | |
|
90 |
|
77 |
|
|
91 | |
78 | |
|
92 | |
79 | |
|
93 | |
80 | |