Widow – Diff between revs 1 and 11

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 1 Rev 11
Line 7... Line 7...
7 { 7 {
8 public static class Helpers 8 public static class Helpers
9 { 9 {
10 #region Public Methods 10 #region Public Methods
Line -... Line 11...
-   11  
11   12 /// <summary> Get the text for the window pointed to by hWnd </summary>
12 public static IEnumerable<DesktopWindow> GetDesktopWindows() 13 public static string GetWindowText(IntPtr hWnd)
13 { 14 {
-   15 var size = Natives.GetWindowTextLength(hWnd);
14 foreach (var process in Process.GetProcesses()) 16 if (size > 0)
-   17 {
15 { 18 var builder = new StringBuilder(size + 1);
-   19 Natives.GetWindowText(hWnd, builder, builder.Capacity);
16 yield return new DesktopWindow(process.MainWindowHandle, process.MainWindowTitle); 20 return builder.ToString();
-   21 }
-   22  
17 } 23 return string.Empty;
Line -... Line 24...
-   24 }
-   25  
-   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
18 } 30 /// not be returned
19   31 /// </param>
20 public static IntPtr FindWindowByTitle(string name) 32 public static IEnumerable<IntPtr> FindWindows(Natives.EnumWindowsProc filter)
-   33 {
21 { 34 var windows = new List<IntPtr>();
22 var hWnd = IntPtr.Zero; 35  
23 foreach (var pList in Process.GetProcesses()) 36 Natives.EnumWindows(delegate(IntPtr wnd, IntPtr param)
24 { 37 {
-   38 if (filter(wnd, param))
25 if (pList.MainWindowTitle.Contains(name)) 39 {
26 { 40 // only add the windows that pass the filter
27 hWnd = pList.MainWindowHandle; -  
Line -... Line 41...
-   41 windows.Add(wnd);
-   42 }
-   43  
-   44 // but return true here so that we iterate all windows
28 } 45 return true;
-   46 }, IntPtr.Zero);
-   47  
-   48 return windows;
-   49 }
-   50  
-   51 /// <summary> Find all windows that contain the given title text </summary>
-   52 /// <param name="titleText"> The text that the window title must contain. </param>
29 } 53 public static IEnumerable<IntPtr> FindWindowsWithText(string titleText)
Line 30... Line 54...
30   54 {
31 return hWnd; 55 return FindWindows((wnd, param) => GetWindowText(wnd).Contains(titleText));
32 } 56 }
Line 38... Line 62...
38 Natives.GetWindowText(hWnd, title, length); 62 Natives.GetWindowText(hWnd, title, length);
39 return title.ToString(); 63 return title.ToString();
40 } 64 }
Line 41... Line 65...
41   65  
-   66 #endregion
-   67  
-   68 public static string GetProcessName(IntPtr hWnd)
-   69 {
-   70 Natives.GetWindowThreadProcessId(hWnd, out var pid);
-   71 var process = Process.GetProcessById((int)pid);
-   72 return process.ProcessName;
-   73  
42 #endregion 74 }
43 } 75 }
44 } 76 }