public static void Run(string filepath) { List <RestoreWindowProps> windows = JsonConvert.DeserializeObject <List <RestoreWindowProps> >(File.ReadAllText(filepath)); Utils.GetWindows() .ForEach(wh => { WindowProps props = WindowProps.CreateFromHandle(wh); if (props == null) { return; } RestoreWindowProps storedProps = windows.FirstOrDefault(p => p.Matches(props)); if (storedProps != null) { Console.WriteLine($"Restoring {props.Caption} to"); Console.WriteLine(JsonConvert.SerializeObject(storedProps.Rect, Formatting.Indented)); wh.SetWindowRect(storedProps.Rect); if (storedProps.ZOrder != null) { wh.SetZPosition(storedProps.ZOrder.Value); } } }); }
public static WindowProps CreateFromHandle(WindowHandle handle) { WindowProps props = new WindowProps(); props.Caption = handle.GetWindowText(); if (!string.IsNullOrEmpty(props.Caption)) { props.Class = handle.GetClassName(); props.Exec = handle.GetWindowExec(); WindowHandleExtensions.Rect rect = new WindowHandleExtensions.Rect(); if (handle.GetWindowRect(ref rect)) { props.Rect = rect; return(props); } } return(null); }
public bool Matches(WindowProps props) { if (Caption != null && !Regex.IsMatch(props.Caption, Caption)) { return(false); } if (Class != null && !Regex.IsMatch(props.Class, Class)) { return(false); } if (Exec != null && !Regex.IsMatch(props.Exec, Exec)) { return(false); } if (Width != null && !Width.Matches(props.Rect.right - props.Rect.left)) { return(false); } if (Height != null && !Height.Matches(props.Rect.bottom - props.Rect.top)) { return(false); } return(true); }
public static void Run(string filepath) { List <WindowProps> windows = Utils.GetWindows().Select(wh => WindowProps.CreateFromHandle(wh)).Where(p => p != null).ToList(); File.WriteAllText(filepath, JsonConvert.SerializeObject(windows, Formatting.Indented)); }