WPF 中获取和设置鼠标位置

WPF 中获取和设置鼠标位置
用的是API
       

private void button2_Click(object sender, RoutedEventArgs e)//获取位置
        {
            POINT p = new POINT();

            Point pp = Mouse.GetPosition(e.Source as FrameworkElement);//WPF方法
            Point ppp = (e.Source as FrameworkElement).PointToScreen(pp);//WPF方法

            if (GetCursorPos(out p))//API方法
            {
                MessageBox.Show(string.Format("GetCursorPos {0},{1}  GetPosition {2},{3}\r\n {4},{5}", p.X, p.Y, pp.X, pp.Y, ppp.X, ppp.Y));
            }
        }

        ///

   
        /// 设置鼠标的坐标   
        ///
   
        /// 横坐标   
        /// 纵坐标   
        [DllImport("User32")]
        public extern static void SetCursorPos(int x, int y);
        public struct POINT
        {
            public int X;
            public int Y;
            public POINT(int x, int y)
            {
                this.X = x;
                this.Y = y;
            }
        }

        ///    
        /// 获取鼠标的坐标   
        ///
   
        /// 传址参数,坐标point类型   
        /// 获取成功返回真   
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool GetCursorPos(out POINT pt);

posted on 2012-02-04 17:53 Old.T 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/tn-wal/archive/2012/02/04/2338150.html

你可能感兴趣的:(WPF 中获取和设置鼠标位置)