Windows API-cursor operation, Windows API-cursor
Obtain and set the cursor position on the screen
GetCursorPos obtains the position of the cursor on the screen. The cursor position is always specified vertically in the screen coordinate and is not affected by the window ing mode that contains the cursor.
Function prototype:
BOOL GetCursorPos (LPPOINT lpPoint );
Parameter description:
LpPoint: Type LPPOINT, output parameter; A structure pointer pointing to the cursor's coordinate point on the screen
Return Value:
Boolean type. If the call is successful, no value is returned. If the call fails, no value is returned;
SetCursorPos sets the cursor position on the screen. If the new coordinates are not in the screen rectangle set by the latest ClipCursor function call, the system automatically adjusts the coordinates so that the cursor stays in the rectangle.
Function prototype:
BOOL SetCursorPos (int X, int Y );
Parameter description:
X: Type int, input parameter; set the x coordinate of the cursor in the screen Coordinate
Y: int type, input parameter; set the y coordinate of the cursor in the screen Coordinate
Return Value:
Boolean type. If the call is successful, no value is returned. If the call fails, no value is returned;
C # code call Case
/// <Summary> // coordinates of the cursor /// </summary> [StructLayout (LayoutKind. Sequential)] public struct LPPOINT {public int X; public int Y ;}
1 // obtain the cursor position 2 [DllImport ("user32.dll", EntryPoint = "GetCursorPos")] 3 unsafe public static extern bool GetCursorPos (LPPOINT * lpPoint ); 4 // set the cursor position 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}
Get current cursor handle
GetCursor: Get the handle of the current cursor
Function prototype:
Hcursor winapi GetCursor (void );
Parameter description:
No Parameter
Return Value:
Returns the handle of the current cursor. If NULL is not returned
C # code call Case
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 }
Obtain global cursor Information
GetCursorInfo: Get global cursor Information
Function prototype:
BOOL GetCursorInfo (PCURSORINFO pci );
Parameter description:
Pci: the input and output parameters of the PCURSORINFO type. a pointer to the structure of PCURSORINFO. Before calling a function, you must set the value of the cSize Member of the parameter structure to sizeof (CURSORINFO)
Return Value:
Boolean type. If the call is successful, no value is returned. If the call fails, no value is returned;
C # code call Case
1 public struct CURSORINFO 2 {3 public int cbSize; // the size of the struct. You can use sizeof (CURSORINFO) to obtain the value of 4 public int flags. // The value is 0 and the cursor is hidden; the value is 0x00000001. The value is 0x00000002. The cursor is disabled. The system does not draw the cursor. You can input the cursor through touch instead of 5 public IntPtr hCursor. // The cursor handle is 6 public LPPOINT ptScreenPos; // coordinates of the cursor on the screen 7} 8 9 class Program10 {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}
Specify the cursor position
ClipCursor limits the cursor to the hold area
Function prototype:
Bool winapi ClipCursor (const RECT * lpRect );
Parameter description:
LpRect: RECT type, input parameter; a screen coordinate structure pointer containing the upper left and lower right corner. If it is set to NULL, the cursor can be moved to any position on the screen.
Return Value:
Boolean type. If the call is successful, no value is returned. If the call fails, no value is returned;
C # code call Case
1 public struct RECT 2 {3 public int left; // x coordinate 4 public int top in the upper left corner of the rectangle; // y coordinate 5 public int right in the upper left corner of the rectangle; // x coordinate 6 public int bottom in the bottom right corner of the rectangle; // coordinate 7} 8 9 class Program10 {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
Reference resources: https://msdn.microsoft.com/zh-cn/vstudio/ms646970%28v=vs.90%29
Case: https://msdn.microsoft.com/zh-cn/vstudio/ms648380%28v=vs.90%29#_win32_Creating_a_Cursor