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

时间:2016-01-15 20:01:15   收藏:0   阅读:287

相对于屏幕的位置  

第一步:

    /// <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的相对位置

 

原文:http://www.cnblogs.com/zhaolili/p/5133940.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!