diff --git a/CefSharp.Core.Runtime/NativeMethodWrapper.cpp b/CefSharp.Core.Runtime/NativeMethodWrapper.cpp index 843cb596e..82bc1ef2c 100644 --- a/CefSharp.Core.Runtime/NativeMethodWrapper.cpp +++ b/CefSharp.Core.Runtime/NativeMethodWrapper.cpp @@ -60,5 +60,23 @@ namespace CefSharp SetWindowLongPtr(hwnd, GWL_EXSTYLE, exStyle & ~WS_EX_NOACTIVATE); } } + + IntPtr NativeMethodWrapper::LoadCursorFromLibCef(int resourceIdentifier) + { + auto moduleName = L"libcef.dll"; + + HMODULE hModule = GetModuleHandle(moduleName); + + if (hModule == nullptr) + { + return IntPtr::Zero; + } + + auto lpCursorName = MAKEINTRESOURCE(resourceIdentifier); + + auto cursor = LoadCursor(hModule, lpCursorName); + + return static_cast(cursor); + } } } diff --git a/CefSharp.Core.Runtime/NativeMethodWrapper.h b/CefSharp.Core.Runtime/NativeMethodWrapper.h index 814c41043..980f82698 100644 --- a/CefSharp.Core.Runtime/NativeMethodWrapper.h +++ b/CefSharp.Core.Runtime/NativeMethodWrapper.h @@ -23,6 +23,7 @@ namespace CefSharp static void SetWindowPosition(IntPtr handle, int x, int y, int width, int height); static void SetWindowParent(IntPtr child, IntPtr newParent); static void RemoveExNoActivateStyle(IntPtr browserHwnd); + static IntPtr LoadCursorFromLibCef(int resourceIdentifier); }; } } diff --git a/CefSharp.Core/NativeMethodWrapper.cs b/CefSharp.Core/NativeMethodWrapper.cs index f902c3954..f0f5198df 100644 --- a/CefSharp.Core/NativeMethodWrapper.cs +++ b/CefSharp.Core/NativeMethodWrapper.cs @@ -38,5 +38,10 @@ namespace CefSharp { CefSharp.Core.NativeMethodWrapper.RemoveExNoActivateStyle(browserHwnd); } + + public static IntPtr LoadCursorFromLibCef(int resourceIdentifier) + { + return CefSharp.Core.NativeMethodWrapper.LoadCursorFromLibCef(resourceIdentifier); + } } } diff --git a/CefSharp.Test/Wpf/EmbeddedCursorLoadTests.cs b/CefSharp.Test/Wpf/EmbeddedCursorLoadTests.cs new file mode 100644 index 000000000..ea4d4be94 --- /dev/null +++ b/CefSharp.Test/Wpf/EmbeddedCursorLoadTests.cs @@ -0,0 +1,50 @@ +// Copyright © 2022 The CefSharp Authors. All rights reserved. +// +// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. + +using System; +using CefSharp.Enums; +using CefSharp.Wpf.Internals; +using Xunit; + +namespace CefSharp.Test.Wpf +{ + //NOTE: All Test classes must be part of this collection as it manages the Cef Initialize/Shutdown lifecycle + /// + /// Issue #4021 + /// Validate Cursor resource Ids + /// + [Collection(CefSharpFixtureCollection.Key)] + public class EmbeddedCursorLoadTests + { + [Theory] + [InlineData(CursorType.Alias)] + [InlineData(CursorType.Cell)] + [InlineData(CursorType.ColumnResize)] + [InlineData(CursorType.Copy)] + [InlineData(CursorType.Grab)] + [InlineData(CursorType.Grabbing)] + [InlineData(CursorType.EastPanning)] + [InlineData(CursorType.MiddlePanning)] + [InlineData(CursorType.MiddlePanningHorizontal)] + [InlineData(CursorType.MiddlePanningVertical)] + [InlineData(CursorType.NorthPanning)] + [InlineData(CursorType.NortheastPanning)] + [InlineData(CursorType.NorthwestPanning)] + [InlineData(CursorType.SouthPanning)] + [InlineData(CursorType.SoutheastPanning)] + [InlineData(CursorType.SouthwestPanning)] + [InlineData(CursorType.WestPanning)] + [InlineData(CursorType.RowResize)] + [InlineData(CursorType.VerticalText)] + [InlineData(CursorType.ZoomIn)] + [InlineData(CursorType.ZoomOut)] + public void CanGetEmbeddedCursors(CursorType cursorType) + { + var actual = EmbeddedCursor.TryLoadCursor(cursorType, out IntPtr cursorPtr); + + Assert.True(actual); + Assert.NotEqual(IntPtr.Zero, cursorPtr); + } + } +} diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs index df99258a0..c704ff0c7 100644 --- a/CefSharp.Wpf/ChromiumWebBrowser.cs +++ b/CefSharp.Wpf/ChromiumWebBrowser.cs @@ -1007,7 +1007,16 @@ namespace CefSharp.Wpf { UiThreadRunAsync(() => { - Cursor = CursorInteropHelper.Create(new SafeFileHandle(handle, ownsHandle: false)); + //Workaround for upstream issue + //See #4021 + if (EmbeddedCursor.TryLoadCursor(type, out IntPtr cursorHandle)) + { + Cursor = CursorInteropHelper.Create(new SafeFileHandle(cursorHandle, ownsHandle: false)); + } + else + { + Cursor = CursorInteropHelper.Create(new SafeFileHandle(handle, ownsHandle: false)); + } }); } } diff --git a/CefSharp.Wpf/Internals/EmbeddedCursor.cs b/CefSharp.Wpf/Internals/EmbeddedCursor.cs new file mode 100644 index 000000000..b6eb8802c --- /dev/null +++ b/CefSharp.Wpf/Internals/EmbeddedCursor.cs @@ -0,0 +1,85 @@ +// Copyright © 2022 The CefSharp Authors. All rights reserved. +// +// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. + +using System; +using System.Collections.Generic; +using CefSharp.Enums; + +namespace CefSharp.Wpf.Internals +{ + // Workaround for upstream issue + // It's possible resources Ids might change between versions, so this might need to be updated + // for different builds, will need to test after upgrading + // See #4021 + public static class EmbeddedCursor + { + //const int IDC_ALIAS = 35610; + //const int IDC_CELL = 35611; + //const int IDC_COLRESIZE = 35612; + //const int IDC_COPYCUR = 35613; + //const int IDC_HAND_GRAB = 35614; + //const int IDC_HAND_GRABBING = 35615; + //const int IDC_PAN_EAST = 35616; + //const int IDC_PAN_MIDDLE = 35617; + //const int IDC_PAN_MIDDLE_HORIZONTAL = 35618; + //const int IDC_PAN_MIDDLE_VERTICAL = 35619; + //const int IDC_PAN_NORTH = 35620; + //const int IDC_PAN_NORTH_EAST = 35621; + //const int IDC_PAN_NORTH_WEST = 35622; + //const int IDC_PAN_SOUTH = 35623; + //const int IDC_PAN_SOUTH_EAST = 35624; + //const int IDC_PAN_SOUTH_WEST = 35625; + //const int IDC_PAN_WEST = 35626; + //const int IDC_ROWRESIZE = 35627; + //const int IDC_VERTICALTEXT = 35628; + //const int IDC_ZOOMIN = 35629; + //const int IDC_ZOOMOUT = 35630; + + private static Dictionary EmbeddedCursors = new Dictionary + { + { CursorType.Alias, 35610 }, + { CursorType.Cell, 35611 }, + { CursorType.ColumnResize, 35612 }, + { CursorType.Copy, 35613 }, + { CursorType.Grab, 35614 }, + { CursorType.Grabbing, 35615 }, + { CursorType.EastPanning, 35616 }, + { CursorType.MiddlePanning, 35617 }, + { CursorType.MiddlePanningHorizontal, 35618 }, + { CursorType.MiddlePanningVertical, 35619 }, + { CursorType.NorthPanning, 35620 }, + { CursorType.NortheastPanning, 35621 }, + { CursorType.NorthwestPanning, 35622 }, + { CursorType.SouthPanning, 35623 }, + { CursorType.SoutheastPanning, 35624 }, + { CursorType.SouthwestPanning, 35625 }, + { CursorType.WestPanning, 35626 }, + { CursorType.RowResize, 35627 }, + { CursorType.VerticalText, 35628 }, + { CursorType.ZoomIn, 35629 }, + { CursorType.ZoomOut, 35630 } + }; + + public static bool TryLoadCursor(CursorType cursorType, out IntPtr cursor) + { + cursor = IntPtr.Zero; + + try + { + if (EmbeddedCursors.TryGetValue(cursorType, out int key)) + { + cursor = NativeMethodWrapper.LoadCursorFromLibCef(key); + + return cursor != IntPtr.Zero; + } + } + catch (Exception) + { + + } + + return false; + } + } +}