【背景】

由于自己的瘦客户端上的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 visibleif (IsWindowVisible(hWnd) ){//get window rectGetWindowRect(hWnd, ref rcowner);wnd.rc = rcowner;if ((rcowner.Left - rcowner.Right) != 0 && (rcowner.Top - rcowner.Bottom) != 0){//get window nameGetWindowTextW(hWnd, sb, sb.Capacity);wnd.szWindowName = sb.ToString();//get window class GetClassNameW(hWnd, sb, sb.Capacity);wnd.szClassName = sb.ToString();//get window infouint x=0;GetWindow(hWnd, x);//add it into list wndList.Add(wnd);}}return true;}, 0);return wndList.ToArray();}