void BeginDragMove(MouseDevice device) { if (device == null) { device = Mouse.PrimaryDevice; } if (device.LeftButton != MouseButtonState.Pressed) { return; } if (this.WindowState == WindowState.Maximized) { device.Capture(this.mover); this.mover.MouseMove += OnMoverMouseMove; this.mover.LostMouseCapture += OnMoverLostCapture; this.mover.MouseLeftButtonUp += OnMoverMouseLeftButtonUp; this.moveStartPoint = device.GetPosition(this); } else { this.inDragMove = true; this.DragMove(); this.inDragMove = false; } }
public void MouseDeviceCaptureTest() { Border child1 = new Border { Background = Brushes.Transparent, Width = 100, Height = 100 }; Border child2 = new Border { Background = Brushes.Transparent, Width = 100, Height = 100 }; Border child3 = new Border { Background = Brushes.Transparent, Width = 100, Height = 100 }; StackPanel panel = new StackPanel { IsRootElement = true }; panel.Children.Add(child1); panel.Children.Add(child2); MouseEventArgs lastArgs = null; panel.MouseMove += (sender, e) => lastArgs = e; TestPresentationSource presentationSource = new TestPresentationSource(); presentationSource.RootElement = panel; panel.Measure(Size.Infinity); panel.Arrange(new Rect(panel.DesiredSize)); MouseDevice mouseDevice = new MouseDevice(presentationSource); mouseDevice.Activate(); mouseDevice.ProcessRawEvent(new RawMouseEventArgs(new Point(50, 50), 0)); Assert.AreEqual(child1, lastArgs.Source); Assert.AreEqual(child1, mouseDevice.HitTarget); Assert.AreEqual(null, mouseDevice.CaptureTarget); Assert.AreEqual(child1, mouseDevice.Target); mouseDevice.ProcessRawEvent(new RawMouseEventArgs(new Point(50, 150), 0)); Assert.AreEqual(child2, lastArgs.Source); Assert.AreEqual(child2, mouseDevice.HitTarget); Assert.AreEqual(null, mouseDevice.CaptureTarget); Assert.AreEqual(child2, mouseDevice.Target); mouseDevice.Capture(child2); mouseDevice.ProcessRawEvent(new RawMouseEventArgs(new Point(50, 50), 0)); Assert.AreEqual(child2, lastArgs.Source); Assert.AreEqual(child1, mouseDevice.HitTarget); Assert.AreEqual(child2, mouseDevice.CaptureTarget); Assert.AreEqual(child2, mouseDevice.Target); mouseDevice.ReleaseCapture(); mouseDevice.ProcessRawEvent(new RawMouseEventArgs(new Point(50, 50), 0)); Assert.AreEqual(child1, lastArgs.Source); Assert.AreEqual(child1, mouseDevice.HitTarget); Assert.AreEqual(null, mouseDevice.CaptureTarget); Assert.AreEqual(child1, mouseDevice.Target); }