window = Window.GetWindow(【Visual】); Point offset = 【Visual】.TransformToAncestor(window).Transform(new Point(0, 0))
上面代码可以获得任意组件的窗口坐标。
获取鼠标相对于任意组件的位置用Mouse.GetPosition(IInputElement relativeTo)或MouseEventArgs.GetPosition(IInputElement relativeTo)
获取屏幕鼠标位置用win32api
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace Samples { class Win32 { [StructLayout(LayoutKind.Sequential)] public struct POINT { public int X; public int Y; public POINT(int x, int y) { this.X = x; this.Y = y; } } [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern bool GetCursorPos(out POINT pt); } }