WPF 获得鼠标相对于屏幕的位置,相对于控件的位置

原文: WPF 获得鼠标相对于屏幕的位置,相对于控件的位置

相对于屏幕的位置  

第一步:

    /// <summary>
    /// 用于获得鼠标相对于屏幕的位置
    /// </summary>
    public 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("shell32.dll")]
        public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);

    }

第二步:

在窗体的CS文件中写代码

 Win32.POINT p = new Win32.POINT(0, 0);
 GetCursorPos(out p);

输出参数p即为当前鼠标相对于屏幕的位置

相对于控件的位置

e.GetPosition(this.list)

这是鼠标相对于控件list的相对位置

猜你喜欢

转载自www.cnblogs.com/lonelyxmas/p/9292377.html