Reputation: 5
I have a application which can be run on multiple monitors. Now I want if multiple monitors are open then on mouse click how could I know on which monitor and which coordinate that current mouse point is pointing ?
Upvotes: 0
Views: 65
Reputation: 2686
Mouse.Capture(Application.Current.MainWindow);
var mousePointerPosition = Mouse.GetPosition(Application.Current.MainWindow);
Application.Current.MainWindow.ReleaseMouseCapture();
Screen targetScreen = null;
if (Application.Current.MainWindow.Visibility == Visibility.Visible)
{
var pointAbs = Application.Current.MainWindow.PointToScreen(mousePointerPosition);
foreach (var screen in Screen.AllScreens)
{
if (screen.Bounds.Contains(new System.Drawing.Point((int)pointAbs.X, (int)pointAbs.Y)))
{
targetScreen = screen;
break;
}
}
}
if (targetScreen != null)
{
var targetX = (targetScreen.WorkingArea.Width / 2) + targetScreen.WorkingArea.Left;
var targetY = (targetScreen.WorkingArea.Height / 2) + targetScreen.WorkingArea.Top;
//now here you can use this value as current mouse point in case of multiple monitors
}
Upvotes: 1