Created
July 18, 2019 03:05
-
-
Save jymcheong/9a0c83c6cd2da444728a6864434a80fa to your computer and use it in GitHub Desktop.
.Net EnumWindows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[DllImport("user32.dll", CharSet = CharSet.Unicode)] | |
private static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount); | |
[DllImport("user32.dll", CharSet = CharSet.Unicode)] | |
private static extern int GetWindowTextLength(IntPtr hWnd); | |
[DllImport("user32.dll")] | |
private static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam); | |
// Delegate to filter which windows to include | |
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam); | |
/// <summary> Get the text for the window pointed to by hWnd </summary> | |
public static string GetWindowText(IntPtr hWnd) | |
{ | |
int size = GetWindowTextLength(hWnd); | |
if (size > 0) | |
{ | |
var builder = new StringBuilder(size + 1); | |
GetWindowText(hWnd, builder, builder.Capacity); | |
return builder.ToString(); | |
} | |
return String.Empty; | |
} | |
/// <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(EnumWindowsProc filter) | |
{ | |
IntPtr found = IntPtr.Zero; | |
List<IntPtr> windows = new List<IntPtr>(); | |
EnumWindows(delegate(IntPtr wnd, IntPtr param) | |
{ | |
if (filter(wnd, param)) | |
{ | |
// only add the windows that pass the filter | |
windows.Add(wnd); | |
} | |
// 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(delegate(IntPtr wnd, IntPtr param) | |
{ | |
return GetWindowText(wnd).Contains(titleText); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment