@@ -41,11 +41,6 @@ specifies which corresponding IDropTarget function was called.
4141## Win32
4242``` c++
4343// Win32 Sample
44- // Initialized elsewhere
45- wil::com_ptr<ICoreWebView2Controller> webViewController;
46-
47- webViewCompositionController =
48- webViewController.query<ICoreWebView2CompositionController>();
4944
5045// Implementation for IDropTarget
5146HRESULT DropTarget::DragEnter (IDataObject* dataObject,
@@ -64,7 +59,7 @@ HRESULT DropTarget::DragEnter(IDataObject* dataObject,
6459
6560 // Convert the screen point to client coordinates add the WebView's offset.
6661 m_viewComponent->OffsetPointToWebView(&point);
67- return webViewCompositionController ->DragEnter(
62+ return m_webViewCompositionController ->DragEnter(
6863 dataObject, keyState, point, effect);
6964}
7065
@@ -84,7 +79,7 @@ HRESULT DropTarget::DragOver(DWORD keyState,
8479 // Convert the screen point to client coordinates add the WebView's offset.
8580 // This returns whether the resultant point is over the WebView visual.
8681 m_viewComponent->OffsetPointToWebView(&point);
87- return webViewCompositionController ->DragOver(
82+ return m_webViewCompositionController ->DragOver(
8883 keyState, point, effect);
8984}
9085
@@ -97,7 +92,7 @@ HRESULT DropTarget::DragLeave()
9792 dropHelper->DragLeave();
9893 }
9994
100- return webViewCompositionController ->DragLeave();
95+ return m_webViewCompositionController ->DragLeave();
10196}
10297
10398HRESULT DropTarget::Drop(IDataObject* dataObject,
@@ -117,7 +112,7 @@ HRESULT DropTarget::Drop(IDataObject* dataObject,
117112 // Convert the screen point to client coordinates add the WebView's offset.
118113 // This returns whether the resultant point is over the WebView visual.
119114 m_viewComponent->OffsetPointToWebView(&point);
120- return webViewCompositionController ->Drop(
115+ return m_webViewCompositionController ->Drop(
121116 dataObject, keyState, point, effect);
122117}
123118```
@@ -162,6 +157,35 @@ private void WebView_Drop(object sender, DragEventArgs e)
162157 e.Data, keyboardState, pointerPosition);
163158 e.AcceptedOperation = operation;
164159}
160+
161+ // Win32 keyboard state modifiers that are relevant during drag and drop.
162+ public const uint MK_LBUTTON = 1;
163+ public const uint MK_RBUTTON = 2;
164+ public const uint MK_SHIFT = 4;
165+ public const uint MK_CONTROL = 8;
166+ public const uint MK_MBUTTON = 16;
167+ public const uint MK_ALT = 32;
168+
169+ // Helper function to convert DragDropModifiers to win32 keyboard state
170+ // modifiers that WebView2 uses during drag and drop operation.
171+ private uint ConvertDragDropModifiersToWin32KeyboardState(
172+ Windows.ApplicationModel.DataTransfer.DragDrop.DragDropModifiers modifiers)
173+ {
174+ uint win32DragDropModifiers = 0;
175+ if ((modifiers & DragDropModifiers.Shift) == DragDropModifiers.Shift)
176+ win32DragDropModifiers |= MK_SHIFT;
177+ if ((modifiers & DragDropModifiers.Control) == DragDropModifiers.Control)
178+ win32DragDropModifiers |= MK_CONTROL;
179+ if ((modifiers & DragDropModifiers.Alt) == DragDropModifiers.Alt)
180+ win32DragDropModifiers |= MK_ALT;
181+ if ((modifiers & DragDropModifiers.LeftButton) == DragDropModifiers.LeftButton)
182+ win32DragDropModifiers |= MK_LBUTTON;
183+ if ((modifiers & DragDropModifiers.MiddleButton) == DragDropModifiers.MiddleButton)
184+ win32DragDropModifiers |= MK_MBUTTON;
185+ if ((modifiers & DragDropModifiers.RightButton) == DragDropModifiers.RightButton)
186+ win32DragDropModifiers |= MK_RBUTTON;
187+ return win32DragDropModifiers;
188+ }
165189```
166190
167191
@@ -263,21 +287,24 @@ interface ICoreWebView2CompositionController : IUnknown {
263287``` c#
264288namespace Microsoft .Web .WebView2 .Core
265289{
266- uint DragEnter(
267- Windows.ApplicationModel.DataTransfer.DataPackage dataObject,
268- uint keyState,
269- Point point);
270-
271- void DragLeave();
272-
273- uint DragOver(
274- uint keyState,
275- Windows.Foundation.Point point);
276-
277- uint Drop(
278- Windows.ApplicationModel.DataTransfer.DataPackage dataObject,
279- uint keyState,
280- Windows.Foundation.Point point);
290+ public sealed class CoreWebView2CompositionController : CoreWebView2Controller , ICoreWebView2CompositionController
291+ {
292+ uint DragEnter (
293+ Windows.ApplicationModel.DataTransfer.DataPackage dataObject ,
294+ uint keyState ,
295+ Point point );
296+
297+ void DragLeave ();
298+
299+ uint DragOver (
300+ uint keyState ,
301+ Windows.Foundation.Point point );
302+
303+ uint Drop (
304+ Windows.ApplicationModel.DataTransfer.DataPackage dataObject ,
305+ uint keyState ,
306+ Windows.Foundation.Point point );
307+ }
281308}
282309```
283310
0 commit comments