public void Change(IntPtr newCursor, SystemCursorId targetCursorId) { if (newCursor == IntPtr.Zero) { throw new ArgumentNullException("newCursor"); } if (originalCursorPointers[targetCursorId] == IntPtr.Zero) { var originalCursor = PInvoke.Cursor.Functions.LoadSystemCursor(IntPtr.Zero, targetCursorId); if (originalCursor != IntPtr.Zero) { var originalCursorCopy = PInvoke.Resources.Functions.CopyImage(originalCursor, ImageType.IMAGE_CURSOR, 0, 0, CopyImageFlag.LR_NONE); if (originalCursorCopy != IntPtr.Zero) { if (PInvoke.Cursor.Functions.SetSystemCursor(newCursor, targetCursorId)) { originalCursorPointers[targetCursorId] = originalCursorCopy; } else { throw new PInvokeException("Unable to set system cursor"); } } else { throw new PInvokeException("Unable to copy original cursor"); } } else { throw new PInvokeException("Unable to load original cursor"); } } else { if (!PInvoke.Cursor.Functions.SetSystemCursor(newCursor, targetCursorId)) { throw new PInvokeException("Unable to set system cursor"); } } }
public static extern bool SetSystemCursor(IntPtr cursor, SystemCursorId systemCursorId);
public static extern IntPtr LoadSystemCursor(IntPtr module, SystemCursorId systemCursorId);
public void Copy(SystemCursorId sourceCursorId, SystemCursorId targetCursorId) { var sourceCursor = PInvoke.Cursor.Functions.LoadSystemCursor(IntPtr.Zero, sourceCursorId); if (sourceCursor != IntPtr.Zero) { var sourceCursorCopy = PInvoke.Resources.Functions.CopyImage(sourceCursor, PInvoke.Resources.Structures.ImageType.IMAGE_CURSOR, 0, 0, PInvoke.Resources.Structures.CopyImageFlag.LR_NONE); if (sourceCursorCopy != IntPtr.Zero) { Change(sourceCursorCopy, targetCursorId); PInvoke.Cursor.Functions.DestroyCursor(sourceCursorCopy); } else { throw new PInvokeException("Unable to copy source cursor"); } } else { throw new PInvokeException("Unable to load source cursor"); } }
private bool TryRestoreOne(SystemCursorId cursorId) { var result = false; IntPtr cursorHandle = originalCursorPointers[cursorId]; if (cursorHandle != IntPtr.Zero) { result = PInvoke.Cursor.Functions.SetSystemCursor(cursorHandle, cursorId); PInvoke.Cursor.Functions.DestroyCursor(cursorHandle); originalCursorPointers[cursorId] = IntPtr.Zero; } return result; }
public void RestoreOne(SystemCursorId cursorId) { IntPtr cursorHandle = originalCursorPointers[cursorId]; if (cursorHandle != IntPtr.Zero) { var changed = PInvoke.Cursor.Functions.SetSystemCursor(cursorHandle, cursorId); PInvoke.Cursor.Functions.DestroyCursor(cursorHandle); originalCursorPointers[cursorId] = IntPtr.Zero; if (!changed) { throw new PInvokeException("Unable to set system cursor"); } } else { throw new InvalidOperationException("This cursor was not changed"); } }
public void ReloadOne(SystemCursorId cursorId) { if (originalCursorPaths == null) { try { InitReload(); } catch (Exception ex) { throw new PInvokeException("Unable to read cursor paths from system registry", ex); } } var cursorPath = originalCursorPaths[cursorId]; if (cursorPath == null) { throw new PInvokeException("Unable to read cursor path from system registry", null); } var newCursor = PInvoke.Cursor.Functions.LoadCursorFromFile(cursorPath); if (newCursor == IntPtr.Zero) { throw new PInvokeException("Unable to load original cursor from file", null); } IntPtr originalCursor = originalCursorPointers[cursorId]; if (originalCursor != IntPtr.Zero) { PInvoke.Cursor.Functions.DestroyCursor(originalCursor); originalCursorPointers[cursorId] = IntPtr.Zero; } if (!PInvoke.Cursor.Functions.SetSystemCursor(newCursor, cursorId)) { throw new PInvokeException("Unable to set system cursor", null); } }