我将从头开始,我将在跨越多个监视器的应用程序上工作,每个监视器将包含一个WPF窗口,并且这些窗口使用单个viewmodel类进行控制。现在可以说我在200,300(x,y)的所有窗口上都有一个按钮,我希望这个按钮应该在同一个窗口中负责工具,而所有其余部分都负责应用程序。 当我尝试获取当前鼠标位置或最后一次点击位置时,我获取相对于当前监视器的位置,即在本例中为200,300,而不管在我处于何种屏幕上。查找监视器/屏幕上存在鼠标指针
以下SRE我尝试了让下面的鼠标位置
[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);
的代码是应该返回我的屏幕没有代码。
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 :( 请帮我在得到合适的值
'Cursor.Position'将返回非相对位置 - (X,Y)... +(X,Y) – 2014-10-16 11:22:23