private void Hook() { _handle = new WindowInteropHelper(this).EnsureHandle(); _window = new Native.Window(_handle); UpdateNonClientBorder(); var source = HwndSource.FromHwnd(_handle); if (source != null) { source.AddHook(WndProc); } }
private void OnLoadedInternal(object sender, RoutedEventArgs e) { if (Equals(WindowChrome.GetWindowChrome(this), _chrome) && _previousSizeToContent != SizeToContent.Manual) { SizeToContent = _previousSizeToContent; _previousSizeToContent = SizeToContent.Manual; if (WindowStartupLocation == WindowStartupLocation.CenterScreen) { Left = SystemParameters.VirtualScreenLeft + SystemParameters.PrimaryScreenWidth / 2 - ActualWidth / 2; Top = SystemParameters.VirtualScreenTop + SystemParameters.PrimaryScreenHeight / 2 - ActualHeight / 2; } if (WindowStartupLocation == WindowStartupLocation.CenterOwner) { if (Owner != null) { if (Owner.WindowState == WindowState.Maximized) { var source = PresentationSource.FromVisual(Owner); if (source != null && source.CompositionTarget != null) { var ownerHandle = new WindowInteropHelper(Owner).EnsureHandle(); var ownerWindow = new Native.Window(ownerHandle); ownerWindow.Invalidate(); Left = -ownerWindow.NonClientBorderWidth * source.CompositionTarget.TransformFromDevice.M11; Top = -ownerWindow.NonClientBorderHeight * source.CompositionTarget.TransformFromDevice.M22; } else { Left = 0; Top = 0; } } else { Left = Owner.Left; Top = Owner.Top; } Left += Owner.ActualWidth / 2 - ActualWidth / 2; Top += Owner.ActualHeight / 2 - ActualHeight / 2; } } UpdateNonClientBorder(); if (_dispatcherFrame != null) { _dispatcherFrame.Continue = false; } } }