/// <summary> /// entry point of the program /// </summary> public static List <DesktopWindow> GetDesktopWindows() { var windows = new List <DesktopWindow>(); NativeMethods.EnumDelegate filter = delegate(IntPtr hWnd, int lParam) { StringBuilder strbTitle = new StringBuilder(255); int nLength = NativeMethods.GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1); string strTitle = strbTitle.ToString(); if (NativeMethods.IsWindowVisible(hWnd) && string.IsNullOrEmpty(strTitle) == false) { uint pid; NativeMethods.GetWindowThreadProcessId(hWnd, out pid); Process process = Process.GetProcessById(Convert.ToInt32(pid)); windows.Add( new DesktopWindow { Handle = hWnd, Title = strTitle, ProcessId = Convert.ToInt32(pid), Exe = GetProcessExe(process) }); } return(true); }; if (!NativeMethods.EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero)) { //Log.Warn("Unable to enum desktop windows"); } return(windows); }
private void UpdateWindowsList() { WindowsListBox.Items.Clear(); NativeMethods.EnumDelegate filter = delegate(IntPtr hWnd, int lParam) { StringBuilder strbTitle = new StringBuilder(255); int nLength = NativeMethods.GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1); string strTitle = strbTitle.ToString(); if (NativeMethods.IsWindowVisible(hWnd) && !string.IsNullOrEmpty(strTitle) && strTitle != "Пуск" && strTitle != "Start") { var screenWindow = new ScreenWindow(hWnd, strTitle); WindowsListBox.Items.Add(screenWindow); } return(true); }; NativeMethods.EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero); }
public static IList <ApplicationInfo> GetRunningApplicationInfo() { // get the visible applications var collection = new List <ApplicationInfo>(); NativeMethods.EnumDelegate filter = delegate(IntPtr hWnd, int lParam) { if (IsAltTabWindow(hWnd) == false) { return(true); } StringBuilder strbTitle = new StringBuilder(255); int nLength = NativeMethods.GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1); string strTitle = strbTitle.ToString(); if (string.IsNullOrEmpty(strTitle) == false) { NativeMethods.Rect wndRect = new NativeMethods.Rect(); NativeMethods.GetWindowRect(hWnd, ref wndRect); uint processId; NativeMethods.GetWindowThreadProcessId(hWnd, out processId); int style = NativeMethods.GetWindowLong(hWnd, Constant.GWL_STYLE); collection.Add(new ApplicationInfo { id = hWnd.ToInt32(), processId = processId, name = strTitle, posX = wndRect.Left, posY = wndRect.Top, width = wndRect.Right - wndRect.Left, height = wndRect.Bottom - wndRect.Top, style = style }); } return(true); }; if (NativeMethods.EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero)) { return(collection.AsReadOnly()); } Trace.WriteLine("Enum windows failed"); return(collection.AsReadOnly()); }
public void DoWork() { int currentProcessId = Process.GetCurrentProcess().Id; while (!_shouldStop) { wndList.Clear(); // get the visible applications var collection = new List <Windows.WindowsAppMgr.WndAttributes>(); NativeMethods.EnumDelegate filter = delegate(IntPtr hWnd, int lParam) { uint processId = 0; NativeMethods.GetWindowThreadProcessId(hWnd, out processId); if (processId == currentProcessId) { return(true); } if (IsAltTabWindow(hWnd) == false) { return(true); } StringBuilder strbTitle = new StringBuilder(255); int nLength = NativeMethods.GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1); string strTitle = string.Empty; try { strTitle = strbTitle.ToString(); } catch (Exception) { } try { Process process = Process.GetProcessById((int)processId); string processName = process.ProcessName; if (processName.Contains("rgbxsvr")) //|| processName.Contains("vncviewer")) { // for video capture window NativeMethods.Rect wndRect = new NativeMethods.Rect(); NativeMethods.GetWindowRect(hWnd, ref wndRect); int style = NativeMethods.GetWindowLong(hWnd, Constant.GWL_STYLE); style |= (int)Constant.WS_THICKFRAME; // always able to resize the window style |= (int)Constant.WS_CAPTION; // always able to close the window collection.Add(new Windows.WindowsAppMgr.WndAttributes { id = hWnd.ToInt32(), processId = processId, name = strTitle, posX = wndRect.Left, posY = wndRect.Top, width = wndRect.Right - wndRect.Left, height = wndRect.Bottom - wndRect.Top, style = style }); return(true); } if (string.IsNullOrEmpty(strTitle) == false) { NativeMethods.Rect wndRect = new NativeMethods.Rect(); NativeMethods.GetWindowRect(hWnd, ref wndRect); int style = NativeMethods.GetWindowLong(hWnd, Constant.GWL_STYLE); collection.Add(new Windows.WindowsAppMgr.WndAttributes { id = hWnd.ToInt32(), processId = processId, name = strTitle, posX = wndRect.Left, posY = wndRect.Top, width = wndRect.Right - wndRect.Left, height = wndRect.Bottom - wndRect.Top, style = style }); } } catch (Exception) { } return(true); }; if (NativeMethods.EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero)) { foreach (var item in collection) { wndList.Add(item); } } if (EvtWndAttributes != null) { EvtWndAttributes(wndList); } Thread.Sleep(100); } }