Kaynağa Gözat

Merge branch 'master' into visual-tree-traversal-v2

Dariusz Komosiński 6 yıl önce
ebeveyn
işleme
dc9dd5b7ef

+ 4 - 1
src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs

@@ -1098,7 +1098,10 @@ namespace Avalonia.Win32.Interop
         
         [DllImport("ole32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
         public static extern HRESULT RegisterDragDrop(IntPtr hwnd, IDropTarget target);
-        
+
+        [DllImport("ole32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
+        public static extern HRESULT RevokeDragDrop(IntPtr hwnd);
+
         [DllImport("ole32.dll", EntryPoint = "OleInitialize")]
         public static extern HRESULT OleInitialize(IntPtr val);
 

+ 17 - 6
src/Windows/Avalonia.Win32/OleContext.cs

@@ -7,9 +7,9 @@ using Avalonia.Win32.Interop;
 
 namespace Avalonia.Win32
 {
-    class OleContext
+    internal class OleContext
     {
-        private static OleContext fCurrent;
+        private static OleContext s_current;
 
         internal static OleContext Current
         {
@@ -18,13 +18,12 @@ namespace Avalonia.Win32
                 if (!IsValidOleThread())
                     return null;
 
-                if (fCurrent == null)
-                    fCurrent = new OleContext();
-                return fCurrent;
+                if (s_current == null)
+                    s_current = new OleContext();
+                return s_current;
             }
         }
 
-
         private OleContext()
         {
             UnmanagedMethods.HRESULT res = UnmanagedMethods.OleInitialize(IntPtr.Zero);
@@ -43,9 +42,21 @@ namespace Avalonia.Win32
         internal bool RegisterDragDrop(IPlatformHandle hwnd, IDropTarget target)
         {
             if (hwnd?.HandleDescriptor != "HWND" || target == null)
+            {
                 return false;
+            }
 
             return UnmanagedMethods.RegisterDragDrop(hwnd.Handle, target) == UnmanagedMethods.HRESULT.S_OK;
         }
+
+        internal bool UnregisterDragDrop(IPlatformHandle hwnd)
+        {
+            if (hwnd?.HandleDescriptor != "HWND")
+            {
+                return false;
+            }
+
+            return UnmanagedMethods.RevokeDragDrop(hwnd.Handle) == UnmanagedMethods.HRESULT.S_OK;
+        }
     }
 }

+ 1 - 1
src/Windows/Avalonia.Win32/OleDropTarget.cs

@@ -6,7 +6,7 @@ using IDataObject = Avalonia.Input.IDataObject;
 
 namespace Avalonia.Win32
 {
-    class OleDropTarget : IDropTarget
+    internal class OleDropTarget : IDropTarget
     {
         private readonly IInputRoot _target;
         private readonly ITopLevelImpl _tl;

+ 5 - 0
src/Windows/Avalonia.Win32/WindowImpl.cs

@@ -253,6 +253,11 @@ namespace Avalonia.Win32
 
         public void Dispose()
         {
+            if (_dropTarget != null)
+            {
+                OleContext.Current?.UnregisterDragDrop(Handle);
+                _dropTarget = null;
+            }
             if (_hwnd != IntPtr.Zero)
             {
                 UnmanagedMethods.DestroyWindow(_hwnd);