Exemple #1
0
        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 == SW_SHOWMINIMIZED ? SW_SHOWNORMAL : placement.showCmd;
                    WindowsPlacementNativeMethods.SetWindowPlacement(windowHandle, ref placement);
                }
                catch (InvalidOperationException ex)
                {
                    Log.Logger.Error(ex, "Parsing placement XML failed");
                }
            }
        }
Exemple #2
0
        private static string GetPlacement(IntPtr windowHandle)
        {
            WindowsPlacementNativeMethods.GetWindowPlacement(windowHandle, out var placement);

            using (MemoryStream memoryStream = new MemoryStream())
            {
                XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
                Serializer.Serialize(xmlTextWriter, placement);
                byte[] xmlBytes = memoryStream.ToArray();
                return(Encoding.GetString(xmlBytes));
            }
        }