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