Skip to content

Commit

Permalink
Convert Rest of Ole to Cswin32 (#10651)
Browse files Browse the repository at this point in the history
* Add test to capture wrapping behavior

* Convert rest of ole to cswin32

* update ComNativeDescriptorTest

* Add test and comment capturing clipboard behavior

* fix build
  • Loading branch information
lonitra authored Jan 18, 2024
1 parent 9c5723b commit 64528f3
Show file tree
Hide file tree
Showing 26 changed files with 516 additions and 814 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

11 changes: 11 additions & 0 deletions src/System.Windows.Forms.Primitives/src/NativeMethods.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
"IDispatchEx.GetTypeInfoCount",
"IDispatchEx.Invoke",
"IDispatchEx.InvokeEx",
"IDragSourceHelper2.SetFlags",
"IDragSourceHelper2.InitializeFromBitmap",
"IDropTargetHelper.DragEnter",
"IDropTargetHelper.DragLeave",
"IDropTargetHelper.DragOver",
"IDropTargetHelper.Drop",
"IEnumConnectionPoints.Next",
"IEnumOLEVERB.Clone",
"IEnumOLEVERB.Next",
Expand Down Expand Up @@ -364,6 +370,11 @@
"ITypeInfo.ReleaseTypeAttr",
"ITypeLib.GetLibAttr",
"ITypeLib.GetTypeInfoOfGuid",
"IUIAutomation.GetFocusedElement",
"IUIAutomationElement.get_CurrentBoundingRectangle",
"IUIAutomationElement.get_CurrentName",
"IUIAutomationElement.GetClickablePoint",
"IUIAutomationElement.SetFocus",
"IUnknown.QueryInterface",
"IValueProvider.get_IsReadOnly",
"IValueProvider.get_Value",
Expand Down
6 changes: 6 additions & 0 deletions src/System.Windows.Forms.Primitives/src/NativeMethods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,10 @@ IDataObject
IDC_*
IDispatchEx
IDropSource
IDragSourceHelper2
IDropSourceNotify
IDropTarget
IDropTargetHelper
IEnumFORMATETC
IEnumOLEVERB
IEnumString
Expand Down Expand Up @@ -414,6 +416,8 @@ ITextProvider
ITextProvider2
ITextRangeProvider
IToggleProvider
IUIAutomation
IUIAutomationElement
IValueProvider
IVBFormat
IVBGetControl
Expand Down Expand Up @@ -548,7 +552,9 @@ OleCreatePropertyFrame
OleCreatePropertyFrameIndirect
OleDuplicateData
OleFlushClipboard
OleGetClipboard
OleInitialize
OleSetClipboard
OleUninitialize
OPEN_FILENAME_FLAGS
OPEN_FILENAME_FLAGS_EX
Expand Down
56 changes: 35 additions & 21 deletions src/System.Windows.Forms/src/System/Windows/Forms/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
using Windows.Win32.System.Ole;
using Windows.Win32.UI.Accessibility;
using Windows.Win32.UI.Input.KeyboardAndMouse;
using static Interop;
using Com = Windows.Win32.System.Com;
using ComTypes = System.Runtime.InteropServices.ComTypes;
using Encoding = System.Text.Encoding;

Expand Down Expand Up @@ -5198,12 +5198,44 @@ public DragDropEffects DoDragDrop(object data, DragDropEffects allowedEffects)
/// value.
/// </para>
/// </remarks>
public DragDropEffects DoDragDrop(
public unsafe DragDropEffects DoDragDrop(
object data,
DragDropEffects allowedEffects,
Bitmap? dragImage,
Point cursorOffset,
bool useDefaultDragImage)
{
ComTypes.IDataObject dataObject = PrepareIncomingDragData(data);

DROPEFFECT finalEffect;

try
{
using var dropSource = ComHelpers.GetComScope<IDropSource>(
new DropSource(this, dataObject, dragImage, cursorOffset, useDefaultDragImage));
using var dataScope = ComHelpers.GetComScope<Com.IDataObject>(dataObject);
if (PInvoke.DoDragDrop(dataScope, dropSource, (DROPEFFECT)(uint)allowedEffects, out finalEffect).Failed)
{
return DragDropEffects.None;
}
}
finally
{
if (DragDropHelper.IsInDragLoop(dataObject))
{
DragDropHelper.SetInDragLoop(dataObject, inDragLoop: false);
}
}

return (DragDropEffects)finalEffect;
}

/// <summary>
/// Prepares the incoming drag data for consumption.
/// The incoming <paramref name="data"/> should implement <see cref="ComTypes.IDataObject"/> to be taken as is.
/// Otherwise, the data will be wrapped in a <see cref="DataObject"/>.
/// </summary>
private static ComTypes.IDataObject PrepareIncomingDragData(object data)
{
ComTypes.IDataObject dataObject;

Expand All @@ -5227,25 +5259,7 @@ public DragDropEffects DoDragDrop(
dataObject = iwdata;
}

DROPEFFECT finalEffect;

try
{
IDropSource.Interface dropSource = new DropSource(this, dataObject, dragImage, cursorOffset, useDefaultDragImage);
if (Ole32.DoDragDrop(dataObject, dropSource, (DROPEFFECT)(uint)allowedEffects, out finalEffect).Failed)
{
return DragDropEffects.None;
}
}
finally
{
if (DragDropHelper.IsInDragLoop(dataObject))
{
DragDropHelper.SetInDragLoop(dataObject, inDragLoop: false);
}
}

return (DragDropEffects)finalEffect;
return dataObject;
}

public void DrawToBitmap(Bitmap bitmap, Rectangle targetBounds)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public HRESULT QueryAcceptData(Com.IDataObject* lpdataobj, ushort* lpcfFormat, R
if (CanShowImage(e))
{
UpdateDropDescription(e);
DragDropHelper.DragEnter(_owner.Handle, e);
DragDropHelper.DragEnter(_owner.HWND, e);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ private void UpdateDropTarget(IDropTarget? newTarget, DragEventArgs e)
if (dragEnterArgs.DropImageType > DropImageType.Invalid && _owner is ToolStrip toolStrip && toolStrip.IsHandleCreated)
{
DragDropHelper.SetDropDescription(dragEnterArgs);
DragDropHelper.DragEnter(toolStrip.Handle, dragEnterArgs);
DragDropHelper.DragEnter(toolStrip.HWND, dragEnterArgs);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
using System.Drawing.Design;
using System.Drawing.Imaging;
using System.Windows.Forms.Layout;
using Com = Windows.Win32.System.Com;
using Windows.Win32.System.Ole;
using static Interop;
using IComDataObject = System.Runtime.InteropServices.ComTypes.IDataObject;

namespace System.Windows.Forms;
Expand Down Expand Up @@ -2211,11 +2211,9 @@ public DragDropEffects DoDragDrop(object data, DragDropEffects allowedEffects)
/// </para>
/// </remarks>
[EditorBrowsable(EditorBrowsableState.Advanced)]
public DragDropEffects DoDragDrop(object data, DragDropEffects allowedEffects, Bitmap? dragImage, Point cursorOffset, bool useDefaultDragImage)
public unsafe DragDropEffects DoDragDrop(object data, DragDropEffects allowedEffects, Bitmap? dragImage, Point cursorOffset, bool useDefaultDragImage)
{
IComDataObject? dataObject = data as IComDataObject;

if (dataObject is null)
if (data is not IComDataObject dataObject)
{
DataObject? iwdata = null;
if (data is IDataObject idataObject)
Expand Down Expand Up @@ -2246,8 +2244,9 @@ public DragDropEffects DoDragDrop(object data, DragDropEffects allowedEffects, B

try
{
IDropSource.Interface dropSource = CreateDropSource(dataObject, dragImage, cursorOffset, useDefaultDragImage);
if (Ole32.DoDragDrop(dataObject, dropSource, (DROPEFFECT)(uint)allowedEffects, out finalEffect).Failed)
using var dropSource = ComHelpers.GetComScope<IDropSource>(CreateDropSource(dataObject, dragImage, cursorOffset, useDefaultDragImage));
using var dataObjectScope = ComHelpers.GetComScope<Com.IDataObject>(dataObject);
if (PInvoke.DoDragDrop(dataObjectScope, dropSource, (DROPEFFECT)(uint)allowedEffects, out finalEffect).Failed)
{
return DragDropEffects.None;
}
Expand Down
Loading

0 comments on commit 64528f3

Please # to comment.