使用C#获取光标相对于显示器屏幕的位置,以下方式在Windows 8的WPF程序中测试通过
方式一:
using System;
using System.Drawing;
using System.Runtime.InteropServices;
namespace ColorPicker
{
/// <summary>
/// win8下wpf程序测试成功
/// </summary>
public class CursorPointManager
{
#region 得到光标在屏幕上的位置
[DllImport("user32")]
private static extern bool GetCursorPos(out Point lpPoint);
/// <summary>
/// 获取光标相对于显示器的位置
/// </summary>
/// <returns></returns>
public static Point GetCursorPosition()
{
Point showPoint = new Point();
GetCursorPos(out showPoint);
return showPoint;
}
#endregion
}
}
方式二:
使用System.Windows.Forms的Cursor类。
在WPF程序中引入System.Windows.Forms命名空间,如下调用
System.Drawing.Point point = System.Windows.Forms.Cursor.Position;
本文介绍两种在C#中获取光标相对于显示器屏幕位置的方法。一种是通过调用Win32 API函数GetCursorPos,另一种是利用System.Windows.Forms中的Cursor类。这两种方法均适用于Windows8下的WPF应用程序。
826

被折叠的 条评论
为什么被折叠?



