Widow

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 1  →  ?path2? @ 11
/trunk/Widow/Helpers.cs
@@ -9,28 +9,52 @@
{
#region Public Methods
 
public static IEnumerable<DesktopWindow> GetDesktopWindows()
/// <summary> Get the text for the window pointed to by hWnd </summary>
public static string GetWindowText(IntPtr hWnd)
{
foreach (var process in Process.GetProcesses())
var size = Natives.GetWindowTextLength(hWnd);
if (size > 0)
{
yield return new DesktopWindow(process.MainWindowHandle, process.MainWindowTitle);
var builder = new StringBuilder(size + 1);
Natives.GetWindowText(hWnd, builder, builder.Capacity);
return builder.ToString();
}
 
return string.Empty;
}
 
public static IntPtr FindWindowByTitle(string name)
/// <summary> Find all windows that match the given filter </summary>
/// <param name="filter">
/// A delegate that returns true for windows
/// that should be returned and false for windows that should
/// not be returned
/// </param>
public static IEnumerable<IntPtr> FindWindows(Natives.EnumWindowsProc filter)
{
var hWnd = IntPtr.Zero;
foreach (var pList in Process.GetProcesses())
var windows = new List<IntPtr>();
 
Natives.EnumWindows(delegate(IntPtr wnd, IntPtr param)
{
if (pList.MainWindowTitle.Contains(name))
if (filter(wnd, param))
{
hWnd = pList.MainWindowHandle;
// only add the windows that pass the filter
windows.Add(wnd);
}
}
 
return hWnd;
// but return true here so that we iterate all windows
return true;
}, IntPtr.Zero);
 
return windows;
}
 
/// <summary> Find all windows that contain the given title text </summary>
/// <param name="titleText"> The text that the window title must contain. </param>
public static IEnumerable<IntPtr> FindWindowsWithText(string titleText)
{
return FindWindows((wnd, param) => GetWindowText(wnd).Contains(titleText));
}
 
public static string GetWindowTitle(IntPtr hWnd)
{
var length = Natives.GetWindowTextLength(hWnd) + 1;
@@ -40,5 +64,13 @@
}
 
#endregion
 
public static string GetProcessName(IntPtr hWnd)
{
Natives.GetWindowThreadProcessId(hWnd, out var pid);
var process = Process.GetProcessById((int)pid);
return process.ProcessName;
 
}
}
}