Browse Source

"Cherry-picked" EmbeddedWindowImpl.cs from single win32 assembly PR

Nikita Tsukanov 7 years ago
parent
commit
75d7341fa1

+ 1 - 0
src/Windows/Avalonia.Win32/Avalonia.Win32.Shared.projitems

@@ -11,6 +11,7 @@
   <ItemGroup>
     <Compile Include="$(MSBuildThisFileDirectory)ClipboardImpl.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)CursorFactory.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)EmbeddedWindowImpl.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)FramebufferManager.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Input\KeyInterop.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Input\WindowsKeyboardDevice.cs" />

+ 0 - 1
src/Windows/Avalonia.Win32/Avalonia.Win32.csproj

@@ -53,7 +53,6 @@
     <Reference Include="WindowsFormsIntegration" />
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="Embedding\EmbeddedWindowImpl.cs" />
     <Compile Include="Embedding\WinFormsAvaloniaControlHost.cs">
       <SubType>Component</SubType>
     </Compile>

+ 90 - 0
src/Windows/Avalonia.Win32/EmbeddedWindowImpl.cs

@@ -0,0 +1,90 @@
+// Copyright (c) The Avalonia Project. All rights reserved.
+// Licensed under the MIT license. See licence.md file in the project root for full license information.
+
+using System;
+using System.ComponentModel;
+using System.Runtime.InteropServices;
+using Avalonia.Platform;
+using Avalonia.Win32.Interop;
+
+namespace Avalonia.Win32
+{
+    class EmbeddedWindowImpl : WindowImpl, IEmbeddableWindowImpl
+    {
+        private static IntPtr DefaultParentWindow = CreateParentWindow();
+        private static UnmanagedMethods.WndProc _wndProcDelegate;
+
+        protected override IntPtr CreateWindowOverride(ushort atom)
+        {
+            var hWnd = UnmanagedMethods.CreateWindowEx(
+                0,
+                atom,
+                null,
+                (int)UnmanagedMethods.WindowStyles.WS_CHILD,
+                0,
+                0,
+                640,
+                480,
+                DefaultParentWindow,
+                IntPtr.Zero,
+                IntPtr.Zero,
+                IntPtr.Zero);
+            return hWnd;
+        }
+
+        protected override IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
+        {
+            if (msg == (uint)UnmanagedMethods.WindowsMessage.WM_KILLFOCUS)
+                LostFocus?.Invoke();
+            return base.WndProc(hWnd, msg, wParam, lParam);
+        }
+
+        public event Action LostFocus;
+
+        private static IntPtr CreateParentWindow()
+        {
+            _wndProcDelegate = new UnmanagedMethods.WndProc(ParentWndProc);
+
+            var wndClassEx = new UnmanagedMethods.WNDCLASSEX
+            {
+                cbSize = Marshal.SizeOf<UnmanagedMethods.WNDCLASSEX>(),
+                hInstance = UnmanagedMethods.GetModuleHandle(null),
+                lpfnWndProc = _wndProcDelegate,
+                lpszClassName = "AvaloniaEmbeddedWindow-" + Guid.NewGuid(),
+            };
+
+            var atom = UnmanagedMethods.RegisterClassEx(ref wndClassEx);
+
+            if (atom == 0)
+            {
+                throw new Win32Exception();
+            }
+
+            var hwnd = UnmanagedMethods.CreateWindowEx(
+                0,
+                atom,
+                null,
+                (int)UnmanagedMethods.WindowStyles.WS_OVERLAPPEDWINDOW,
+                UnmanagedMethods.CW_USEDEFAULT,
+                UnmanagedMethods.CW_USEDEFAULT,
+                UnmanagedMethods.CW_USEDEFAULT,
+                UnmanagedMethods.CW_USEDEFAULT,
+                IntPtr.Zero,
+                IntPtr.Zero,
+                IntPtr.Zero,
+                IntPtr.Zero);
+
+            if (hwnd == IntPtr.Zero)
+            {
+                throw new Win32Exception();
+            }
+
+            return hwnd;
+        }
+
+        private static IntPtr ParentWndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
+        {
+            return UnmanagedMethods.DefWindowProc(hWnd, msg, wParam, lParam);
+        }
+    }
+}

+ 0 - 43
src/Windows/Avalonia.Win32/Embedding/EmbeddedWindowImpl.cs

@@ -1,43 +0,0 @@
-// Copyright (c) The Avalonia Project. All rights reserved.
-// Licensed under the MIT license. See licence.md file in the project root for full license information.
-
-using System;
-using Avalonia.Platform;
-using Avalonia.Win32.Interop;
-
-namespace Avalonia.Win32
-{
-    class EmbeddedWindowImpl : WindowImpl, IEmbeddableWindowImpl
-    {
-        private static readonly System.Windows.Forms.UserControl WinFormsControl = new System.Windows.Forms.UserControl();
-
-        public static IntPtr DefaultParentWindow = WinFormsControl.Handle;
-
-        protected override IntPtr CreateWindowOverride(ushort atom)
-        {
-            var hWnd = UnmanagedMethods.CreateWindowEx(
-                0,
-                atom,
-                null,
-                (int)UnmanagedMethods.WindowStyles.WS_CHILD,
-                0,
-                0,
-                640,
-                480,
-                DefaultParentWindow,
-                IntPtr.Zero,
-                IntPtr.Zero,
-                IntPtr.Zero);
-            return hWnd;
-        }
-
-        protected override IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
-        {
-            if(msg == (uint)UnmanagedMethods.WindowsMessage.WM_KILLFOCUS)
-                LostFocus?.Invoke();
-            return base.WndProc(hWnd, msg, wParam, lParam);
-        }
-
-        public event Action LostFocus;
-    }
-}

+ 0 - 4
src/Windows/Avalonia.Win32/Win32Platform.cs

@@ -196,13 +196,9 @@ namespace Avalonia.Win32
 
         public IEmbeddableWindowImpl CreateEmbeddableWindow()
         {
-#if NETSTANDARD
-            throw new NotSupportedException();
-#else
             var embedded = new EmbeddedWindowImpl();
             embedded.Show();
             return embedded;
-#endif
         }
 
         public IPopupImpl CreatePopup()