【背景】
由于自己的瘦客户端上的Windows自带截图软件功能被阉割,所以自己写了一个,其中有窗口截图功能,涉及到获得所有可见窗口的信息。
【代码】
public WindowInfo[] GetAllDesktopWindows()
{
//用来保存窗口对象 列表
List<WindowInfo> wndList = new List<WindowInfo>();
//enum all desktop windows
EnumWindows(delegate (IntPtr hWnd, int lParam)
{
WindowInfo wnd = new WindowInfo();
StringBuilder sb = new StringBuilder(256);
Rect rcowner = new Rect();
//get hwnd
wnd.hWnd = hWnd;
//judge if visible
if (IsWindowVisible(hWnd) )
{
//get window rect
GetWindowRect(hWnd, ref rcowner);
wnd.rc = rcowner;
if ((rcowner.Left - rcowner.Right) != 0 && (rcowner.Top - rcowner.Bottom) != 0)
{
//get window name
GetWindowTextW(hWnd, sb, sb.Capacity);
wnd.szWindowName = sb.ToString();
//get window class
GetClassNameW(hWnd, sb, sb.Capacity);
wnd.szClassName = sb.ToString();
//get window info
uint x=0;
GetWindow(hWnd, x);
//add it into list
wndList.Add(wnd);
}
}
return true;
}, 0);
return wndList.ToArray();
}