Widow – Blame information for rev 26

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