|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | +using System.Windows.Input; |
| 5 | +using System.Windows.Interop; |
| 6 | +using CefSharp.Enums; |
| 7 | +using Microsoft.Win32.SafeHandles; |
| 8 | + |
| 9 | +namespace CefSharp.Wpf { |
| 10 | + |
| 11 | + internal static class DragCursorProvider |
| 12 | + { |
| 13 | + [DllImport("kernel32.dll")] |
| 14 | + private static extern IntPtr LoadLibrary(string dllToLoad); |
| 15 | + |
| 16 | + [DllImport("user32.dll")] |
| 17 | + private static extern IntPtr LoadCursor(IntPtr hInstance, ushort lpCursorName); |
| 18 | + |
| 19 | + private static readonly Dictionary<DragOperationsMask, Cursor> DragCursors; |
| 20 | + |
| 21 | + static DragCursorProvider() { |
| 22 | + var library = LoadLibrary("ole32.dll"); |
| 23 | + DragCursors = new Dictionary<DragOperationsMask, Cursor>() |
| 24 | + { |
| 25 | + { DragOperationsMask.None, GetCursorFromLib(library, 1) }, |
| 26 | + { DragOperationsMask.Move, GetCursorFromLib(library, 2) }, |
| 27 | + { DragOperationsMask.Copy, GetCursorFromLib(library, 3) }, |
| 28 | + { DragOperationsMask.Link, GetCursorFromLib(library, 4) } |
| 29 | + // TODO: support black cursors |
| 30 | + }; |
| 31 | + } |
| 32 | + |
| 33 | + private static Cursor GetCursorFromLib(IntPtr library, ushort cursorIndex) |
| 34 | + { |
| 35 | + var cursorHandle = LoadCursor(library, cursorIndex); |
| 36 | + return CursorInteropHelper.Create(new SafeFileHandle(cursorHandle, false)); |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Get the Windows cursor for the drag effect specified. |
| 41 | + /// </summary> |
| 42 | + /// <param name="operation"></param> |
| 43 | + /// <returns>The drop cursor based on the specified drag operation effect</returns> |
| 44 | + public static Cursor GetCursor(DragOperationsMask operation) |
| 45 | + { |
| 46 | + Cursor cursor; |
| 47 | + if (DragCursors.TryGetValue(operation, out cursor)) |
| 48 | + { |
| 49 | + return cursor; |
| 50 | + } |
| 51 | + return Cursors.Arrow; |
| 52 | + } |
| 53 | + } |
| 54 | +} |
0 commit comments