wpf 查找鼠标指针所在的监视器/屏幕

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26402955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:37:08  来源:igfitidea点击:

Finding Monitor/Screen on which mouse pointer is present

c#wpfmouse

提问by Mustahsan Abdullah

I will start from beginning, I aw working on an application which will span over multiple monitors, each monitor will contain a single WPF window and these windows are controled using a single viewmodel class. now lets say i have a button on all the windows at 200,300 (x,y) and i want that this button should be responsible for a tool on same window, while all rest are responsible for the application. When i try to get current mouse position or last click position i get position relative to the current monitor , i.e. in this case 200,300, irrespective of in what screen i am on.

我将从头开始,我将开发一个跨越多个监视器的应用程序,每个监视器将包含一个 WPF 窗口,并且这些窗口使用一个视图模型类进行控制。现在假设我在 200,300 (x,y) 处的所有窗口上都有一个按钮,我希望这个按钮应该负责同一个窗口上的工具,而其余的都负责应用程序。当我尝试获取当前鼠标位置或上次点击位置时,我会获得相对于当前显示器的位置,即在这种情况下为 200,300,无论我在哪个屏幕上。

following sre the code i tried for getting mouse position

按照 sre 我尝试获取鼠标位置的代码

  1. [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    internal static extern bool GetCursorPos(ref Win32Point pt);
    
    [StructLayout(LayoutKind.Sequential)]
    internal struct Win32Point
    {
        public Int32 X;
        public Int32 Y;
    };
    
    public static Point GetMousePosition()
    {
        Win32Point w32Mouse = new Win32Point();
        GetCursorPos(ref w32Mouse);
        return new Point(w32Mouse.X, w32Mouse.Y);
    }
    
  2. Point point = Control.MousePosition;

  3. Mouse.GetPosition(null);

  1. [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    internal static extern bool GetCursorPos(ref Win32Point pt);
    
    [StructLayout(LayoutKind.Sequential)]
    internal struct Win32Point
    {
        public Int32 X;
        public Int32 Y;
    };
    
    public static Point GetMousePosition()
    {
        Win32Point w32Mouse = new Win32Point();
        GetCursorPos(ref w32Mouse);
        return new Point(w32Mouse.X, w32Mouse.Y);
    }
    
  2. Point point = Control.MousePosition;

  3. Mouse.GetPosition(null);

following is the code which should return me the screen no.

以下是应该返回屏幕编号的代码。

private int ConvertMousePointToScreenIndex(System.Windows.Point mousePoint)
    {
        //first get all the screens 
        System.Drawing.Rectangle ret;

        for (int i = 1; i <= System.Windows.Forms.Screen.AllScreens.Count(); i++)
        {
            ret = System.Windows.Forms.Screen.AllScreens[i - 1].Bounds;
            if (ret.Contains(new System.Drawing.Point((int)mousePoint.X, (int)mousePoint.Y)))
                return i - 1;
        }
        return 0;
    }

I always get screen as 0 :( Please help me in getting appropriate value

我总是将屏幕设为 0 :( 请帮助我获得适当的值

采纳答案by Mustahsan Abdullah

Thanks KnottytOmo, it worked now, may be there was something else wrong at that moment. I changed my code to

感谢 KnottytOmo,它现在起作用了,当时可能还有其他问题。我将代码更改为

 private int ConvertMousePointToScreenIndex(Point mousePoint)
    {
        //first get all the screens 
        System.Drawing.Rectangle ret;

        for (int i = 1; i <= Screen.AllScreens.Count(); i++)
        {
            ret = Screen.AllScreens[i - 1].Bounds;
            if (ret.Contains(mousePoint))
                return i - 1;
        }
        return 0;
    }

and called it as ConvertMousePointToScreenIndex(System.Windows.Forms.Cursor.Position);

并将其称为 ConvertMousePointToScreenIndex(System.Windows.Forms.Cursor.Position);

回答by KnottytOmo

Could you use the Screenstatic class?

你可以使用Screen静态类吗?

For example, something like:

例如,类似于:

Screen s = Screen.FromPoint(Cursor.Position);

Or get the current screen from a particular form using:

或者使用以下命令从特定表单获取当前屏幕:

Screen s = Screen.FromControl(this);

With thisbeing your Form control.

随着this被你的窗体控件。

http://msdn.microsoft.com/en-us/library/system.windows.forms.screen.aspx

http://msdn.microsoft.com/en-us/library/system.windows.forms.screen.aspx