7

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

  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

2 Answers2

22

Could you use the Screen static class?

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 this being your Form control.

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

DerApe
  • 3,097
  • 2
  • 35
  • 55
KnottytOmo
  • 546
  • 7
  • 20
  • The Points I get are in respect to the monitor and not universal i.e. even on 2nd monitor the point i get is 70,150. which will give me a result as primary monitor i.e screen[0] – Mustahsan Abdullah Oct 16 '14 at 11:21
  • @MustahsanAbdullah, you can use `Cursor.Position`. – KnottytOmo Oct 16 '14 at 11:25
  • *Why* is `Cursor.Position` not helpful? – Alex K. Oct 16 '14 at 12:43
  • I agree with @AlexK. Can you please explain in more detail why this solution does not work? – KnottytOmo Oct 16 '14 at 13:07
  • 2
    Great answer, thank you, but you can simplify it even more. Cursor.Position is already a Point. So `Screen.FromPoint(new Point(Cursor.Position.X, Cursor.Position.Y))` can just be `Screen.FromPoint(Cursor.Position)` – Adam Plocher Feb 20 '16 at 02:02
5

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

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