查找存在鼠标指针的监视器/屏幕 [英] Finding Monitor/Screen on which mouse pointer is present

查看:115
本文介绍了查找存在鼠标指针的监视器/屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

以下是我尝试获取鼠标位置的代码

following sre the code i tried for getting mouse position

[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);
}

  • Point point = Control.MousePosition;

    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;
        }
    

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

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

    推荐答案

    感谢KnottytOmo,它现在可以正常工作,可能那时还存在其他问题. 我将代码更改为

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

     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;
        }
    

    并命名为ConvertMousePointToScreenIndex(System.Windows.Forms.Cursor.Position);

    这篇关于查找存在鼠标指针的监视器/屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆