private void SetWindowPlacement(ref UnsafeNativeMethods.WINDOWPLACEMENT windowPlacement)
        {
            IntPtr handle  = (new WindowInteropHelper(this)).Handle;
            int    showCmd = windowPlacement.ShowCmd;

            windowPlacement.ShowCmd = 0;
            UnsafeNativeMethods.SetWindowPlacement(handle, ref windowPlacement);
            if (!UnsafeNativeMethods.GetWindowPlacement(handle, out windowPlacement))
            {
                base.Left   = SystemParameters.WorkArea.X;
                base.Top    = SystemParameters.WorkArea.Y;
                base.Width  = SystemParameters.WorkArea.Width;
                base.Height = SystemParameters.WorkArea.Height;
                return;
            }
            Point point  = WindowHelper.TransformFromDevice(this, new Point((double)windowPlacement.NormalPosition.Left, (double)windowPlacement.NormalPosition.Top));
            Point point1 = WindowHelper.TransformFromDevice(this, new Point((double)windowPlacement.NormalPosition.Right, (double)windowPlacement.NormalPosition.Bottom));

            base.Left               = point.X;
            base.Top                = point.Y;
            base.Width              = point1.X - point.X;
            base.Height             = point1.Y - point.Y;
            windowPlacement.ShowCmd = showCmd;
            UnsafeNativeMethods.SetWindowPlacement(handle, ref windowPlacement);
        }
Exemple #2
0
        public static void UpdateWindowPlacement(Window window)
        {
            IntPtr handle = new WindowInteropHelper(window).Handle;

            UnsafeNativeMethods.WINDOWPLACEMENT lpwndpl = new UnsafeNativeMethods.WINDOWPLACEMENT();
            lpwndpl.Length = Marshal.SizeOf(typeof(UnsafeNativeMethods.WINDOWPLACEMENT));
            if (!UnsafeNativeMethods.GetWindowPlacement(handle, out lpwndpl))
            {
                return;
            }
            UnsafeNativeMethods.SetWindowPlacement(handle, ref lpwndpl);
        }
Exemple #3
0
        public static void UpdateWindowPlacement(Window window)
        {
            IntPtr handle = (new WindowInteropHelper(window)).Handle;

            UnsafeNativeMethods.WINDOWPLACEMENT wINDOWPLACEMENT = new UnsafeNativeMethods.WINDOWPLACEMENT()
            {
                Length = Marshal.SizeOf(typeof(UnsafeNativeMethods.WINDOWPLACEMENT))
            };
            if (UnsafeNativeMethods.GetWindowPlacement(handle, out wINDOWPLACEMENT))
            {
                UnsafeNativeMethods.SetWindowPlacement(handle, ref wINDOWPLACEMENT);
            }
        }
 protected override void OnClosing(CancelEventArgs e)
 {
     try
     {
         if (this.Configuration != null)
         {
             UnsafeNativeMethods.WINDOWPLACEMENT wINDOWPLACEMENT = new UnsafeNativeMethods.WINDOWPLACEMENT()
             {
                 Length = Marshal.SizeOf(typeof(UnsafeNativeMethods.WINDOWPLACEMENT))
             };
             if (UnsafeNativeMethods.GetWindowPlacement((new WindowInteropHelper(this)).Handle, out wINDOWPLACEMENT))
             {
                 Point point  = WindowHelper.TransformFromDevice(this, new Point((double)wINDOWPLACEMENT.NormalPosition.Left, (double)wINDOWPLACEMENT.NormalPosition.Top));
                 Point point1 = WindowHelper.TransformFromDevice(this, new Point((double)wINDOWPLACEMENT.NormalPosition.Right, (double)wINDOWPLACEMENT.NormalPosition.Bottom));
                 this.Configuration.SetProperty("Height", point1.Y - point.Y);
                 this.Configuration.SetProperty("Width", point1.X - point.X);
                 this.Configuration.SetProperty("Left", point.X);
                 this.Configuration.SetProperty("Top", point.Y);
                 if (wINDOWPLACEMENT.ShowCmd != 3)
                 {
                     this.Configuration.SetProperty("WindowState", WindowState.Normal);
                 }
                 else
                 {
                     this.Configuration.SetProperty("WindowState", WindowState.Maximized);
                 }
             }
         }
     }
     catch
     {
     }
     base.OnClosing(e);
     if (e.Cancel)
     {
         this.OnClosingCanceled(EventArgs.Empty);
         return;
     }
     SystemEvents.UserPreferenceChanged -= new UserPreferenceChangedEventHandler(this.SystemEvents_UserPreferenceChanged);
 }
 public void SetInitialWindowPlacement()
 {
     try
     {
         if (this.Configuration == null || !this.Configuration.HasProperty("Left") || !this.Configuration.HasProperty("Width") || !this.Configuration.HasProperty("Top") || !this.Configuration.HasProperty("Height") || !this.Configuration.HasProperty("WindowState"))
         {
             UnsafeNativeMethods.WINDOWPLACEMENT top = new UnsafeNativeMethods.WINDOWPLACEMENT()
             {
                 Length = Marshal.SizeOf(typeof(UnsafeNativeMethods.WINDOWPLACEMENT))
             };
             if (UnsafeNativeMethods.GetWindowPlacement((new WindowInteropHelper(this)).Handle, out top))
             {
                 UnsafeNativeMethods.RECT rECT = new UnsafeNativeMethods.RECT();
                 if (UnsafeNativeMethods.SystemParametersInfo(48, 0, ref rECT, 0))
                 {
                     if (top.NormalPosition.Top < rECT.Top)
                     {
                         top.NormalPosition.Top = rECT.Top;
                     }
                     if (top.NormalPosition.Left < rECT.Left)
                     {
                         top.NormalPosition.Left = rECT.Left;
                     }
                     if (top.NormalPosition.Bottom > rECT.Bottom)
                     {
                         top.NormalPosition.Bottom = rECT.Bottom;
                     }
                     if (top.NormalPosition.Right > rECT.Right)
                     {
                         top.NormalPosition.Right = rECT.Right;
                     }
                     if (top.NormalPosition.Left > top.NormalPosition.Right)
                     {
                         top.NormalPosition.Left = rECT.Left;
                     }
                     if (top.NormalPosition.Top > top.NormalPosition.Bottom)
                     {
                         top.NormalPosition.Top = rECT.Top;
                     }
                     top.ShowCmd = 3;
                     this.SetWindowPlacement(ref top);
                 }
             }
         }
         else
         {
             double      property    = (double)this.Configuration.GetProperty("Left");
             double      num         = (double)this.Configuration.GetProperty("Top");
             double      property1   = (double)this.Configuration.GetProperty("Width");
             double      num1        = (double)this.Configuration.GetProperty("Height");
             WindowState windowState = (WindowState)this.Configuration.GetProperty("WindowState");
             Point       device      = WindowHelper.TransformToDevice(this, new Point(property, num));
             Point       point       = WindowHelper.TransformToDevice(this, new Point(property + property1, num + num1));
             UnsafeNativeMethods.WINDOWPLACEMENT x = new UnsafeNativeMethods.WINDOWPLACEMENT()
             {
                 Length = Marshal.SizeOf(typeof(UnsafeNativeMethods.WINDOWPLACEMENT)),
                 Flags  = 0
             };
             x.MaxPosition.X         = -1;
             x.MaxPosition.Y         = -1;
             x.MinPosition.X         = -1;
             x.MinPosition.Y         = -1;
             x.NormalPosition.Left   = (int)device.X;
             x.NormalPosition.Top    = (int)device.Y;
             x.NormalPosition.Right  = (int)point.X;
             x.NormalPosition.Bottom = (int)point.Y;
             if (windowState != WindowState.Maximized)
             {
                 x.ShowCmd = 1;
             }
             else
             {
                 x.ShowCmd = 3;
             }
             this.SetWindowPlacement(ref x);
         }
         if (this.FeedbackService != null)
         {
             this.FeedbackService.SetData(37, (int)base.ActualWidth);
             this.FeedbackService.SetData(38, (int)base.ActualHeight);
         }
     }
     catch
     {
     }
 }
 public static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref UnsafeNativeMethods.WINDOWPLACEMENT lpwndpl);