#669 – Retrieving the Mouse’s Current Position in an Event Handler

You can use the static Mouse.GetPosition method anywhere in your code to retrieve the current position of your mouse.

You can also access the mouse position through either the MouseButtonEventArgs or MouseEventArgs object passed in to an event handler for any of the mouse-related events.

In the example below, we use the MouseButtonEventArgs.GetPosition and MouseEventArgs.GetPosition methods in the handlers to get the mouse’s position and to update a related label.

        private void win1_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
            Point p = e.GetPosition(null);
            MousePosTextLastClick = string.Format("Last click at X = {0}, Y = {1}", p.X, p.Y);

        private void win1_MouseMove_1(object sender, MouseEventArgs e)
            Point p = e.GetPosition(null);
            MousePosText = string.Format("X = {0}, Y = {1}", p.X, p.Y);


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

2 Responses to #669 – Retrieving the Mouse’s Current Position in an Event Handler

  1. Pingback: #670 – Getting the Mouse Position Relative to a Specific Element « 2,000 Things You Should Know About WPF

  2. Pingback: Dew Drop – October 17, 2012 (#1,423) | 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: