Skip to content

Commit 9a4541c

Browse files
authored
Merge 3e38388 into 8ffd29f
2 parents 8ffd29f + 3e38388 commit 9a4541c

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

CefSharp.Wpf/CefSharp.Wpf.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
</ItemGroup>
8080
<ItemGroup>
8181
<Compile Include="CefSettings.cs" />
82+
<Compile Include="DragCursorProvider.cs" />
8283
<Compile Include="Internals\MonitorInfo.cs" />
8384
<Compile Include="Internals\MonitorInfoEx.cs" />
8485
<Compile Include="Internals\RectStruct.cs" />

CefSharp.Wpf/ChromiumWebBrowser.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ private void NoInliningConstructor()
489489
DragOver += OnDragOver;
490490
DragLeave += OnDragLeave;
491491
Drop += OnDrop;
492+
GiveFeedback += OnGiveFeedback;
492493

493494
IsVisibleChanged += OnIsVisibleChanged;
494495

@@ -610,6 +611,7 @@ protected virtual void Dispose(bool isDisposing)
610611
DragOver -= OnDragOver;
611612
DragLeave -= OnDragLeave;
612613
Drop -= OnDrop;
614+
GiveFeedback -= OnGiveFeedback;
613615

614616
IsVisibleChanged -= OnIsVisibleChanged;
615617

@@ -787,7 +789,8 @@ void IRenderWebBrowser.UpdateDragCursor(DragOperationsMask operation)
787789
/// <param name="operation">describes the allowed operation (none, move, copy, link). </param>
788790
protected virtual void UpdateDragCursor(DragOperationsMask operation)
789791
{
790-
//TODO: Someone should implement this
792+
var dragCursor = DragCursorProvider.GetCursor(operation);
793+
UiThreadRunAsync(() => Mouse.SetCursor(dragCursor));
791794
}
792795

793796
/// <summary>
@@ -1531,6 +1534,16 @@ private void OnDragOver(object sender, DragEventArgs e)
15311534
}
15321535
}
15331536

1537+
/// <summary>
1538+
/// Handles the <see cref="E:GiveFeedback" /> event.
1539+
/// </summary>
1540+
/// <param name="sender">The sender.</param>
1541+
/// <param name="e">The <see cref="GiveFeedbackEventArgs"/> instance containing the event data.</param>
1542+
private void OnGiveFeedback(object sender, GiveFeedbackEventArgs e) {
1543+
/// prevent showing default cursor, the appropriate cursor will be set by <see cref=UpdateDragCursor />
1544+
e.Handled = true;
1545+
}
1546+
15341547
/// <summary>
15351548
/// Handles the <see cref="E:DragEnter" /> event.
15361549
/// </summary>

CefSharp.Wpf/DragCursorProvider.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright © 2019 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Runtime.InteropServices;
8+
using System.Windows.Input;
9+
using System.Windows.Interop;
10+
using CefSharp.Enums;
11+
using Microsoft.Win32.SafeHandles;
12+
13+
namespace CefSharp.Wpf
14+
{
15+
16+
internal static class DragCursorProvider
17+
{
18+
[DllImport("kernel32.dll")]
19+
private static extern IntPtr LoadLibrary(string dllToLoad);
20+
21+
[DllImport("user32.dll")]
22+
private static extern IntPtr LoadCursor(IntPtr hInstance, ushort lpCursorName);
23+
24+
private static readonly Dictionary<DragOperationsMask, Cursor> DragCursors;
25+
26+
static DragCursorProvider()
27+
{
28+
var library = LoadLibrary("ole32.dll");
29+
DragCursors = new Dictionary<DragOperationsMask, Cursor>()
30+
{
31+
{ DragOperationsMask.None, GetCursorFromLib(library, 1) },
32+
{ DragOperationsMask.Move, GetCursorFromLib(library, 2) },
33+
{ DragOperationsMask.Copy, GetCursorFromLib(library, 3) },
34+
{ DragOperationsMask.Link, GetCursorFromLib(library, 4) }
35+
// TODO: support black cursors
36+
};
37+
}
38+
39+
private static Cursor GetCursorFromLib(IntPtr library, ushort cursorIndex)
40+
{
41+
var cursorHandle = LoadCursor(library, cursorIndex);
42+
return CursorInteropHelper.Create(new SafeFileHandle(cursorHandle, false));
43+
}
44+
45+
/// <summary>
46+
/// Get the Windows cursor for the drag effect specified.
47+
/// </summary>
48+
/// <param name="operation"></param>
49+
/// <returns>The drop cursor based on the specified drag operation effect</returns>
50+
public static Cursor GetCursor(DragOperationsMask operation)
51+
{
52+
Cursor cursor;
53+
if (DragCursors.TryGetValue(operation, out cursor))
54+
{
55+
return cursor;
56+
}
57+
return Cursors.Arrow;
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)