private void Configure(Window view, object viewModel = null) { // without VieWModel we cannot uniquely identify the window if (!(viewModel is IMaintainPosition) || _uiConfiguration == null || !_uiConfiguration.AreWindowLocationsStored) { return; } var windowName = viewModel.GetType().FullName; var screenBounds = DisplayInfo.GetAllScreenBounds(); var hasPlacement = _uiConfiguration.WindowLocations.TryGetValue(windowName, out var placement); if (!hasPlacement || placement.ShowCmd == ShowWindowCommands.Normal && !screenBounds.Contains(placement.NormalPosition)) { view.WindowStartupLocation = view.Owner != null ? WindowStartupLocation.CenterOwner : _uiConfiguration.DefaultWindowStartupLocation; hasPlacement = false; } if (hasPlacement) { view.WindowStartupLocation = WindowStartupLocation.Manual; } // Storage for the event handlers, so we can remove them again EventHandler[] eventHandlers = new EventHandler[3]; // Store Placement eventHandlers[1] = (sender, args) => { // ReSharper disable once InvokeAsExtensionMethod var newPlacement = WindowsExtensions.RetrievePlacement(view); if (newPlacement.ShowCmd == ShowWindowCommands.Hide) { // Ignore return; } Log.Debug().WriteLine("Stored placement {0} for Window {1}", newPlacement.NormalPosition, windowName); _uiConfiguration.WindowLocations[windowName] = newPlacement; }; // Cleanup handlers eventHandlers[2] = (s, e) => { view.LocationChanged -= eventHandlers[1]; view.Closed -= eventHandlers[2]; }; //Initialize handlers eventHandlers[0] = (sender, args) => { if (hasPlacement) { // Make sure the placement is set WindowsExtensions.ApplyPlacement(view, placement); } view.LocationChanged -= eventHandlers[0]; view.LocationChanged += eventHandlers[1]; view.Closed += eventHandlers[2]; }; view.LocationChanged += eventHandlers[0]; }
/// <summary> /// The coordinates need to be mapped from 0-65535 where 0 is left and 65535 is right /// </summary> /// <param name="location">NativePoint</param> /// <returns>NativePoint</returns> private static NativePoint RemapLocation(NativePoint location) { var bounds = DisplayInfo.GetAllScreenBounds(); if (bounds.Width * bounds.Height == 0) { return(location); } return(new NativePoint(location.X * (65535 / bounds.Width), location.Y * (65535 / bounds.Height))); }
/// <inheritdoc /> protected override void OnActivate() { var bounds = DisplayInfo.GetAllScreenBounds(); Left = bounds.Left; Top = bounds.Top; Width = bounds.Width; Height = bounds.Height; base.OnActivate(); }
/// <summary> /// Create a MouseInput struct for a mouse move /// </summary> /// <param name="location">Where is the click located</param> /// <param name="timestamp">The time stamp for the event</param> /// <returns>MouseInput</returns> public static MouseInput MouseMove(NativePoint location, uint timestamp = 0) { location = RemapLocation(location); var bounds = DisplayInfo.GetAllScreenBounds(); return(new MouseInput { dx = location.X * (65535 / bounds.Width), dy = location.Y * (65535 / bounds.Height), Timestamp = timestamp, MouseEventFlags = MouseMoveMouseEventFlags }); }
/// <summary> /// Create a MouseInput struct for a mouse move /// </summary> /// <param name="location">Where is the click located</param> /// <param name="timestamp">The time stamp for the event</param> /// <returns>MouseInput</returns> public static MouseInput MouseMove(NativePoint location, uint?timestamp = null) { location = RemapLocation(location); var bounds = DisplayInfo.GetAllScreenBounds(); var messageTime = timestamp ?? (uint)Environment.TickCount; return(new MouseInput { dx = location.X * (65535 / bounds.Width), dy = location.Y * (65535 / bounds.Height), Timestamp = messageTime, MouseEventFlags = MouseMoveMouseEventFlags }); }