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