/// <summary> /// Enumerate all windows /// </summary> /// <returns></returns> public static List<WindowInformation> EnumerateWindows() { var windows = new List<WindowInformation>(); WindowPlacement.EnumWindows((handle, parameter) => { var sb = new StringBuilder(0x1024); if (WindowPlacement.IsWindowVisible(handle) != 0 && WindowPlacement.GetWindowText(handle, sb, sb.Capacity) != 0) { var window = new WindowInformation() { Title = sb.ToString(), Handle = handle, Id = (int)WindowPlacement.GetWindowThreadProcessId(handle, IntPtr.Zero), }; var title = sb.ToString(); if (windows.Count == 0 || windows.Last().Id != window.Id || windows.Last().Title != window.Title) { windows.Add(window); } } return 1; }, 0); if (windows.Count > 0 && windows.Last().Title == "Program Manager") { windows.RemoveAt(windows.Count - 1); } return windows; }
/// <summary> /// Set position and size to top window /// </summary> /// <param name="positon"></param> public static void RelocateTop(Rectangle positon) { int thisProcessId = -1; WindowInformation window = null; WindowPlacement.EnumWindows((handle, parameter) => { var sb = new StringBuilder(0x1024); if (WindowPlacement.IsWindowVisible(handle) != 0 && WindowPlacement.GetWindowText(handle, sb, sb.Capacity) != 0) { var id = (int)WindowPlacement.GetWindowThreadProcessId(handle, IntPtr.Zero); if (thisProcessId < 0) { thisProcessId = id; } else if (id != thisProcessId) { window = new WindowInformation() { Title = sb.ToString(), Handle = handle, Id = id, }; return 0; } } return 1; }, 0); if (window != null) { window.Relocate(positon); } }