Widow – Diff between revs 14 and 26

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 14 Rev 26
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 string GetWindowClass(IntPtr hWnd) 51 public static string GetWindowClass(IntPtr hWnd)
52 { 52 {
53 var windowClass = new StringBuilder(256); 53 var windowClass = new StringBuilder(256);
54 Natives.GetClassName(hWnd, windowClass, windowClass.Capacity); 54 Natives.GetClassName(hWnd, windowClass, windowClass.Capacity);
55 return windowClass.ToString(); 55 return windowClass.ToString();
56 } 56 }
57   57  
58 public static string GetWindowTitle(IntPtr hWnd) 58 public static string GetWindowTitle(IntPtr hWnd)
59 { 59 {
60 var length = Natives.GetWindowTextLength(hWnd) + 1; 60 var length = Natives.GetWindowTextLength(hWnd) + 1;
61 var title = new StringBuilder(length); 61 var title = new StringBuilder(length);
62 Natives.GetWindowText(hWnd, title, length); 62 Natives.GetWindowText(hWnd, title, length);
63 return title.ToString(); 63 return title.ToString();
64 } 64 }
65   65  
66 public static string GetProcessName(IntPtr hWnd) 66 public static string GetProcessName(IntPtr hWnd)
67 { 67 {
68 Natives.GetWindowThreadProcessId(hWnd, out var pid); 68 Natives.GetWindowThreadProcessId(hWnd, out var pid);
-   69 try
-   70 {
69 var process = Process.GetProcessById((int) pid); 71 var process = Process.GetProcessById((int) pid);
70 return process.ProcessName; 72 return process.ProcessName;
-   73 }
-   74 catch
-   75 {
-   76 return string.Empty;
-   77 }
71 } 78 }
72   79  
73 #endregion 80 #endregion
74 } 81 }
75 } 82 }
76   83  
77
Generated by GNU Enscript 1.6.5.90.
84
Generated by GNU Enscript 1.6.5.90.
78   85  
79   86  
80   87