P/Invoke 光标的操作

获取与设置光标在屏幕上的位置 

  GetCursorPos 获取光标在屏幕上的位置,光标位置始终是在屏幕坐标纵指定的,并且不受包含光标的窗口映射模式的影响

    函数原型:

      BOOL GetCursorPos(LPPOINT lpPoint);

    参数说明:

      lpPoint:类型LPPOINT,输出参数;一个指向光标在屏幕坐标点的结构指针

    返回值:

      BOOL类型,调用成功返回非0,失败返回0;

  SetCursorPos 设置光标在屏幕上的位置,如果新的坐标不是由最新的ClipCursor函数调用设置的屏幕矩形中,系统自动调整坐标以便光标停留在该矩形内

    函数原型:

      BOOL SetCursorPos(int X,int Y);

    参数说明:

      X:类型int,输入参数;设置光标在屏幕坐标中的x坐标

      Y:类型int,输入参数;设置光标在屏幕坐标中的y坐标

    返回值:

       BOOL类型,调用成功返回非0,失败返回0;

  C#代码调用案例

    /// <summary>
    /// 光标的坐标
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    public struct LPPOINT
    {
        public int X;
        public int Y;
    }
 1         //获取光标位置
 2         [DllImport("user32.dll", EntryPoint = "GetCursorPos")]
 3         unsafe public static extern bool GetCursorPos(LPPOINT* lpPoint);
 4         //设置光标位置
 5         [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
 6         public static extern bool SetCursorPos(int X, int Y);
 7 
 8         unsafe static void Main(string[] args)
 9         {
10             int x = 100, y = 100;
11             for (int i = 0; i < 200; i++)
12             {
13                 SetCursorPos(x + i, y + i);
14                 LPPOINT lpPoint;
15                 GetCursorPos(&lpPoint);
16                 Console.WriteLine("[x:{0},y:{1}]", lpPoint.X, lpPoint.Y);
17                 Thread.Sleep(50);
18             }
19             Console.ReadKey();
20         }

获取当前光标句柄

  GetCursor 获取当前光标的句柄

    函数原型:

       HCURSOR WINAPI GetCursor(void);

    参数说明:

      无参

    返回值:

       返回当前光标的句柄,如果没有返回NULL

  C#代码调用案例

1         [DllImport("user32.dll", EntryPoint = "GetCursor")]
2         public static extern IntPtr GetCursor();
3 
4         unsafe static void Main(string[] args)
5         {
6             Console.WriteLine(GetCursor());
7             Console.ReadKey();
8         }

获取全局光标信息

  GetCursorInfo 获取全局光标的信息

    函数原型:

      BOOL GetCursorInfo(PCURSORINFO pci);

    参数说明:

      pci:PCURSORINFO类型,输入输出参数;一个指向PCURSORINFO的结构体的指针,函数调用前必须设置参数结构体cSize成员的值为sizeof(CURSORINFO)

    返回值:

       BOOL类型,调用成功返回非0,失败返回0;

  C#代码调用案例

 1     public struct CURSORINFO
 2     {
 3         public int cbSize;//结构体的大小,可通过sizeof(CURSORINFO)获取赋值
 4         public int flags; //值为0光标隐藏;值为0x00000001光标显示;值为0x00000002禁用光标,该标志显示系统未绘制光标,用户通过触控输入而不是鼠标
 5         public IntPtr hCursor;//光标句柄
 6         public LPPOINT ptScreenPos;//光标在屏幕上的坐标
 7     }
 8 
 9     class Program
10     {
11         [DllImport("user32.dll", EntryPoint = "GetCursorInfo")]
12         unsafe public static extern bool GetCursorInfo(CURSORINFO* pci);
13 
14         unsafe static void Main(string[] args)
15         {
16             CURSORINFO pci;
17             pci.cbSize = sizeof(CURSORINFO);
18             GetCursorInfo(&pci);
19             Console.WriteLine("cbSize:{0},flags:{1},hCursor:{2},[X:{3},Y:{4}]",
20                 pci.cbSize, pci.flags, pci.hCursor, pci.ptScreenPos.X, pci.ptScreenPos.Y);
21             Console.ReadKey();
22         }
23     }

限定光标位置

  ClipCursor 将光标限定在举行区域内

    函数原型:

      BOOL WINAPI ClipCursor(const RECT * lpRect);

    参数说明:

      lpRect:RECT类型,输入参数;一个包含左上角和右下角的屏幕坐标结构指针,如果设置为NULL,则光标可以任意移动到屏幕上的任何位置

    返回值:

       BOOL类型,调用成功返回非0,失败返回0;

  C#代码调用案例

 1     public struct RECT
 2     {
 3         public int left;//矩形的左上角的x坐标
 4         public int top;//矩形的左上角的y坐标
 5         public int right;//矩形的右下角的x坐标
 6         public int bottom;//矩形的右下角坐标
 7     }
 8 
 9     class Program
10     {
11         [DllImport("user32.dll", EntryPoint = "ClipCursor")]
12         unsafe public static extern IntPtr ClipCursor(RECT* lpRect);
13 
14         unsafe static void Main(string[] args)
15         {
16             RECT rect;
17             rect.left = 100;
18             rect.top = 100;
19             rect.right = 200;
20             rect.bottom = 200;
21             ClipCursor(&rect);
22             Console.ReadKey();
23         }
24     }

Header---Winuser.h

Library---user32.dll

参考资源:https://msdn.microsoft.com/zh-cn/vstudio/ms646970%28v=vs.90%29

使用案例:https://msdn.microsoft.com/zh-cn/vstudio/ms648380%28v=vs.90%29#_win32_Creating_a_Cursor

原文地址:https://www.cnblogs.com/liunlls/p/Cursor.html