一、WPF 中获取和设置鼠标位置
方法一:WPF方法
Point p = Mouse.GetPosition (e.Source as FrameworkElement); Point p = (e.Source as FrameworkElement).PointToScreen(pp);
方法二: API方法
/// <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); private void Window_MouseMove(object sender, MouseEventArgs e) { POINT p = new POINT(); if (GetCursorPos(out p))//API方法 { txtStat.Text = string.Format("X:{0} Y:{1}", p.X, p.Y); } }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索wpf win32
, progressbar
, 鼠标
, int
, getcursorpos函数使用
, 方法
, 坐标
, point
, public
, wpf 坐标+
获取鼠标位置
,以便于您获取更多的相关知识。
时间: 2024-12-01 01:49:03