#690 – Being Notified When Mouse Capture Is Lost

Your application can lose its mouse capture due to some system event.  When this happens, you might want to know that the capture was lost so that you can restore some application state.

You can discover when you’ve lost the mouse capture due to some external event by handling the LostMouseCapture event.

In the example below, the code normally releases the mouse capture and reverts the label’s color on a MouseUp event.  It also reverts the label’s color when it discovers that it has lost the mouse capture.

        private Brush savedBrush;

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

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

        private void Feast_LostMouseCapture(object sender, MouseEventArgs e)
        {
            Label l = e.Source as Label;
            l.Background = savedBrush;
        }
Advertisements

#689 – An Application Can Lose Its Mouse Capture

You can capture the mouse on one mouse button event and then release it on another, with your application in some state while the mouse is captured.

For example, changing the background color of a label while the left mouse button is down.

        private Brush savedBrush;

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

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

Although your code normally releases the mouse capture, it’s also possible that some system event causes the mouse capture to be lost.  (E.g. A system dialog pops up).  In this case, you won’t get the expected event, so your event handler never gets a chance to release the mouse capture.  Your application then ends up in an unacceptable state.  (E.g. Label’s background color wrong).

#685 – Capturing the Mouse

When you are handling mouse events, the event is generally fired by the control that the mouse is over.  For example, if you hold down the mouse button over one control, move the mouse, and release over another control, the first control will see the MouseDown event and the second will see the MouseUp event.

A control can request that it receive all future mouse events by executing the CaptureMouse event.  When it sees the event it was waiting for, it can then call the ReleaseMouseCapture event to go back to normal behavior.

        private void MouseDown_Raph(object sender, MouseButtonEventArgs e)
        {
            Console.WriteLine("Raphael sees MouseDown");
            ((UIElement)e.Source).CaptureMouse();
        }

        private void MouseUp_Raph(object sender, MouseButtonEventArgs e)
        {
            Console.WriteLine("Raphael sees MouseUp");
            ((UIElement)e.Source).ReleaseMouseCapture();
        }

With the code above, the “Raphael” label captures the mouse on a MouseDown, allowing it to see the corresponding MouseUp event, no matter where the mouse pointer is moved.