#691 – IsMouseCaptured Indicates Whether Mouse Is Currently Captured

You can use the CaptureMouse and ReleaseMouseCapture methods to capture/release the mouse pointer, so that a user interface element will get all future mouse events.

You can also check the IsMouseCaptured property of a UIElement object at any time, to see whether this element has currently captured the mouse.  In the example below, IsMouseCaptured is true while we’re moving the mouse pointer across the label only while we have a mouse button pressed.

        private void Feast_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Console.WriteLine("Feast_MouseDown");
            Label l = e.Source as Label;
            l.CaptureMouse();
        }

        private void Feast_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Console.WriteLine("Feast_MouseUp");
            Label l = e.Source as Label;
            l.ReleaseMouseCapture();
        }

        private void Feast_MouseMove(object sender, MouseEventArgs e)
        {
            Label l = e.Source as Label;
            Console.WriteLine(string.Format("IsMouseCaptured={0}", l.IsMouseCaptured));
        }

Advertisement