public static void SetSystemCursor(Cursor cursor) { // Get previous cursor var pci = new CURSORINFO(); pci.cbSize = Marshal.SizeOf(typeof(CURSORINFO)); GetCursorInfo(out pci); // Get previous cursor type (the one to be replaced) _previousCursorType = GetCursorType(pci); // Copy previous cursor because next SetSystemCursor might destroy it _previousCursor = CopyImage(pci.hCursor, (uint)CopyImageType.IMAGE_CURSOR, Cursors.Default.Size.Width, Cursors.Default.Size.Height, (uint)FUFlags.LR_COPYFROMRESOURCE); // Copy the cursor to set IntPtr newCursor = CopyImage(cursor.Handle, (uint)CopyImageType.IMAGE_CURSOR, cursor.Size.Width, cursor.Size.Height, (uint)FUFlags.LR_COPYFROMRESOURCE); // Set the cursor in replacement of the current one bool r = SetSystemCursor(newCursor, (uint)_previousCursorType); if (!r) { Console.WriteLine("Error: " + Marshal.GetLastWin32Error()); } }
public static extern bool SetSystemCursor(IntPtr hcur, OCR_SYSTEM_CURSORS id);
public static void SetSystemCursor(Cursor cursor) { // Get previous cursor var pci = new CURSORINFO(); pci.cbSize = Marshal.SizeOf(typeof(CURSORINFO)); GetCursorInfo(out pci); // Get previous cursor type (the one to be replaced) _previousCursorType = GetCursorType(pci); // Copy previous cursor because next SetSystemCursor might destroy it _previousCursor = CopyImage(pci.hCursor, (uint)CopyImageType.IMAGE_CURSOR, Cursors.Default.Size.Width, Cursors.Default.Size.Height, (uint)FUFlags.LR_COPYFROMRESOURCE); // Copy the cursor to set IntPtr newCursor = CopyImage(cursor.Handle, (uint)CopyImageType.IMAGE_CURSOR, cursor.Size.Width, cursor.Size.Height, (uint)FUFlags.LR_COPYFROMRESOURCE); // Set the cursor in replacement of the current one bool r = SetSystemCursor(newCursor, (uint)_previousCursorType); if (!r) Console.WriteLine("Error: " + Marshal.GetLastWin32Error()); }
public static extern IntPtr LoadCursor(IntPtr hInstance, OCR_SYSTEM_CURSORS lpCursorName);
public static extern bool SetSystemCursor(IntPtr hcur, OCR_SYSTEM_CURSORS id);