using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace VDash.WindowSelectors
{
public interface IWindowSelector
{
IntPtr GetWindowHandle();
WindowInfo GetWindowInfo();
bool WindowExists();
}
public class WindowNotFoundException : Exception
{
public WindowNotFoundException(string message) : base(message)
{
}
}
public class WindowInfo
{
#region P/Invokes
private delegate bool EnumWindowsProc(IntPtr hWnd, int lPar);
[DllImport("user32.dll")]
private static extern int EnumWindows(EnumWindowsProc callPtr, int lPar);
[DllImport("user32.dll")]
static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumWindowsProc lpfn, int lPar);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
private static extern bool IsWindowVisible(IntPtr hWnd);
#endregion
public string Title { get; private set; }
public IntPtr hWnd { get; private set; }
public bool IsVisible { get; private set; }
public WindowInfo(string title, IntPtr hWnd, bool isVisible)
{
Title = title;
this.hWnd = hWnd;
this.IsVisible = isVisible;
}
public override string ToString()
{
return Title;
}
///
/// Gets a list of all windows in the current desktop session.
///
///
public static WindowInfo[] GetWindowList()
{
List windows = new List();
EnumWindowsProc del = ((IntPtr hWnd, int lPar) =>
{
StringBuilder sb = new StringBuilder(255);
GetWindowText(hWnd, sb, 255);
bool isVisible = IsWindowVisible(hWnd);
var windowInfo = new WindowInfo(sb.ToString(), hWnd, isVisible);
windows.Add(windowInfo);
return true;
});
EnumDesktopWindows(IntPtr.Zero, del, 0);
return windows.ToArray();
}
}
}