1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| class ClassEW
{
private IntPtr _hwdn;
private string _name;
private static List<ClassEW> _windows = new List<ClassEW>();
public IntPtr HWND { get { return this._hwdn; } }
public string Name { get { return this._name; } }
public ClassEW(IntPtr hwnd, string name)
{
this._hwdn = hwnd;
this._name = name;
}
public override string ToString()
{
return this._name;
}
private static bool EnumWindows(IntPtr hwnd, int lparam)
{
if (!string.IsNullOrEmpty(NativeMethods.GetWindowText()))
{
ClassEW window = new ClassEW(hwnd, NativeMethods.GetWindowText(hwnd));
_windows.Add(window);
}
return true;
}
public static List<ClassEW> GetWindowsList()
{
NativeMethods.EnumWindows(new NativeMethods.EnumWindowsCallBack(EnumWindows), 0);
return _windows;
}
internal static class NativeMethods
{
public delegate bool EnumWindowsCallBack(IntPtr hwnd, int lParam);
[DllImport("user32.dll")]
internal static extern int EnumWindows(EnumWindowsCallBack lpEnumFunc, int lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, [Out] StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
private static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
internal static string GetWindowText((IntPtr hWnd)
{
StringBuilder sb = new StringBuilder(GetWindowTextLength(hWnd)+ 1);
GetWindowText(hWnd, sb, sb.Capacity);
return sb.ToString();
}
}
} |
Partager