多个监视器应用程序的鼠标点坐标

时间:2014-05-01 11:48:53

标签: .net wpf

我有一个可以在多个监视器上运行的应用程序。现在我想要打开多个显示器然后点击鼠标我怎么知道哪个显示器和当前鼠标指向哪个坐标?

1 个答案:

答案 0 :(得分:1)

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
                }