#692 – Common Controls Typically Do Not Capture the Mouse

It’s a convention within Windows that controls typically do not capture the mouse after they intercept a mouse down event.  For example, you click on a Button by pressing and releasing the left mouse button.  But if you press the left mouse button, and then move the mouse pointer off the button before releasing, the mouse will not get clicked.

Avoiding a mouse capture in this case allows the user to avoid a click action after inadvertently pressing the left mouse button down over the wrong user interface element.

Advertisement

#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;
        }

#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).