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));
            }
        }

        /// <summary>   
        /// 設置鼠標的坐標   
        /// </summary>   
        /// <param name="x">橫坐標</param>   
        /// <param name="y">縱坐標</param>   
        [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;
            }
        }

        /// <summary>   
        /// 獲取鼠標的坐標   
        /// </summary>   
        /// <param name="lpPoint">傳址參數,坐標point類型</param>   
        /// <returns>獲取成功返回真</returns>   
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool GetCursorPos(out POINT pt);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM