public static void SetPlacement(this Window window, string placementJson) { var windowHandle = new WindowInteropHelper(window).Handle; if (!string.IsNullOrEmpty(placementJson)) { byte[] xmlBytes = Encoding.GetBytes(placementJson); try { WINDOWPLACEMENT placement; using (var memoryStream = new MemoryStream(xmlBytes)) { placement = (WINDOWPLACEMENT)Serializer.Deserialize(memoryStream); } placement.length = Marshal.SizeOf(typeof(WINDOWPLACEMENT)); placement.flags = 0; placement.showCmd = placement.showCmd == SwShowMinimized ? SwShowNormal : placement.showCmd; WindowsPlacementNativeMethods.SetWindowPlacement(windowHandle, ref placement); } catch (InvalidOperationException ex) { Log.Logger.Error(ex, "Parsing placement XML failed"); } } }
private static string GetPlacement(IntPtr windowHandle) { WindowsPlacementNativeMethods.GetWindowPlacement(windowHandle, out var placement); using (var memoryStream = new MemoryStream()) { var xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); Serializer.Serialize(xmlTextWriter, placement); var xmlBytes = memoryStream.ToArray(); return(Encoding.GetString(xmlBytes)); } }