#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)
            Label l = e.Source as Label;
            savedBrush = l.Background;
            l.Background = Brushes.Cyan;

        private void Feast_MouseUp(object sender, MouseButtonEventArgs e)
            Label l = e.Source as Label;
            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).


About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #689 – An Application Can Lose Its Mouse Capture

  1. Pingback: Dew Drop – November 14, 2012 (#1,442) | Alvin Ashcraft's Morning Dew

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: