WPF 获取鼠标屏幕位置、窗口位置、控件位置

时间:2021-03-31 10:23:49
原文: WPF 获取鼠标屏幕位置、窗口位置、控件位置


    public struct POINT
    {
        public int X;
        public int Y;
        public POINT(int x, int y)
        {
            this.X = x;
            this.Y = y;
        }
    }

[DllImport("user32.dll")]
public static extern bool GetCursorPos(out POINT lpPoint);

//e.GetPosition(this);
//(e.Source as FrameworkElement).PointToScreen(new Point(0, 0));            
POINT mousestart = new POINT();
GetCursorPos(out mousestart);

Point p2 = Mouse.GetPosition(this as FrameworkElement);

Point p = Mouse.GetPosition(e.Source as FrameworkElement);

MessageBox.Show(Convert.ToString(mousestart.X));
MessageBox.Show(Convert.ToString(p.X));
MessageBox.Show(Convert.ToString(p2.X));