|
| 1 | +# Background |
| 2 | + |
| 3 | +In visual hosting, the current cursor image cannot be determined easily by the |
| 4 | +hosting application easily. For example, if the cursor over the WebView is also |
| 5 | +over a textbox inside of the WebView, the cursor should usually be IDC_IBEAM. |
| 6 | +And by default, the cursor is IDC_ARROW. Currently, we return the what the |
| 7 | +cursor should be as an HCURSOR object (After calling LoadCursor on those IDC |
| 8 | +values). The hosting application can then use the HCURSOR object to change the |
| 9 | +current cursor to the correct image. |
| 10 | + |
| 11 | +However, Office already has a well established way of using cursors internally |
| 12 | +by using IDC_* values and there is no easy way to convert the HCURSOR object we |
| 13 | +are returning into an IDC value after it's been created. |
| 14 | + |
| 15 | +This new API is to enable hosting applications like Office to get the current |
| 16 | +IDC value of the cursor instead of the HCURSOR as an option. |
| 17 | + |
| 18 | + |
| 19 | +# Description |
| 20 | + |
| 21 | +The `SystemCursorId` property will return the current system cursor ID reproted |
| 22 | +by the underlying rendering engine for WebView2. It is not meant to return a |
| 23 | +value for any custom cursors, such as those defined by CSS. |
| 24 | + |
| 25 | +It can be used at any time but will only change after a `CursorChanged` event. |
| 26 | + |
| 27 | + |
| 28 | +# Examples |
| 29 | + |
| 30 | +```cpp |
| 31 | +CHECK_FAILURE(m_compositionController->add_CursorChanged( |
| 32 | + Callback<ICoreWebView2ExperimentalCursorChangedEventHandler>( |
| 33 | + [this](ICoreWebView2ExperimentalCompositionController* sender, IUnknown* args) |
| 34 | + -> HRESULT { |
| 35 | + HRESULT hr = S_OK; |
| 36 | + HCURSOR cursor; |
| 37 | + UINT32 cursorId; |
| 38 | + wil::com_ptr<ICoreWebView2ExperimentalCompositionController2> compositionController2 = |
| 39 | + m_controller.query<ICoreWebView2ExperimentalCompositionController2>(); |
| 40 | + CHECK_FAILURE(compositionController2->get_SystemCursorId(&cursorId)); |
| 41 | + cursor = ::LoadCursor(nullptr, MAKEINTRESOURCE(cursorId)); |
| 42 | + if (cursor == nullptr) |
| 43 | + { |
| 44 | + hr = HRESULT_FROM_WIN32(GetLastError()); |
| 45 | + } |
| 46 | + |
| 47 | + if (SUCCEEDED(hr)) |
| 48 | + { |
| 49 | + SetClassLongPtr( |
| 50 | + m_appWindow->GetMainWindow() /* HWND */, GCLP_HCURSOR, (LONG_PTR)cursor); |
| 51 | + } |
| 52 | + return hr; |
| 53 | + }) |
| 54 | + .Get(), |
| 55 | + &m_cursorChangedToken)); |
| 56 | + |
| 57 | + |
| 58 | +# API Notes |
| 59 | + |
| 60 | +See [API Details](#api-details) section below for API reference. |
| 61 | + |
| 62 | +# API Details |
| 63 | + |
| 64 | +/// This interface is continuation of the |
| 65 | +/// ICoreWebView2ExperimentalCompositionController interface. |
| 66 | +[uuid(279ae616-b7cb-4946-8da3-dc853645d2ba), object, pointer_default(unique)] |
| 67 | +interface ICoreWebView2ExperimentalCompositionController2 : IUnknown { |
| 68 | + /// The current system cursor ID reported by the underlying rendering engine |
| 69 | + /// for WebView. For example, most of the time, when the cursor is over text, |
| 70 | + /// this will return the int value for IDC_IBEAM. The systemCursorId is only |
| 71 | + /// valid if the rendering engine reports a default Windows cursor resource |
| 72 | + /// value. See: |
| 73 | + /// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-loadcursorw |
| 74 | + /// Otherwise, if custom CSS cursors are being used, this will return 0. |
| 75 | + /// To actually use systemCursorId in LoadCursor or LoadImage, |
| 76 | + /// MAKEINTRESOURCE must be called on it first. |
| 77 | + /// |
| 78 | + /// \snippet ViewComponent.cpp SystemCursorId |
| 79 | + [propget] HRESULT SystemCursorId([out, retval] UINT32* systemCursorId); |
| 80 | +} |
| 81 | + |
| 82 | +# Appendix |
| 83 | + |
| 84 | +I expect that most apps will use the get_Cursor API which returns an HCURSOR (Or |
| 85 | +the UWP equivalent that will be implemented in the future that will return a |
| 86 | +CoreCursor) outside of Office as HCURSOR is more comprehensive and covers the |
| 87 | +scenarios in which custom cursors are used. |
0 commit comments