#690 – Being Notified When Mouse Capture Is Lost
November 14, 2012 Leave a comment
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; }