From 85e32974ae21ea2f774351c758ba6a75c9c5b555 Mon Sep 17 00:00:00 2001 From: amaitland Date: Wed, 24 May 2017 12:54:14 +1000 Subject: [PATCH] WinForms - If the ChromiumWebBrowser has it's handle recreated then in OnHandleCreated we will now attempt to reparent a existing instance rather than creating a new browser # Conflicts: # CefSharp.WinForms/ChromiumWebBrowser.cs --- CefSharp.Core/NativeMethodWrapper.cpp | 8 ++++++++ CefSharp.Core/NativeMethodWrapper.h | 1 + CefSharp.WinForms/ChromiumWebBrowser.cs | 11 ++++++++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CefSharp.Core/NativeMethodWrapper.cpp b/CefSharp.Core/NativeMethodWrapper.cpp index c40b0bd69..b840a91d7 100644 --- a/CefSharp.Core/NativeMethodWrapper.cpp +++ b/CefSharp.Core/NativeMethodWrapper.cpp @@ -37,4 +37,12 @@ namespace CefSharp } } } + + void NativeMethodWrapper::SetWindowParent(IntPtr child, IntPtr newParent) + { + HWND childHwnd = static_cast(child.ToPointer()); + HWND newParentHwnd = static_cast(newParent.ToPointer()); + + SetParent(childHwnd, newParentHwnd); + } } diff --git a/CefSharp.Core/NativeMethodWrapper.h b/CefSharp.Core/NativeMethodWrapper.h index 7cb57ea9b..8857b4f62 100644 --- a/CefSharp.Core/NativeMethodWrapper.h +++ b/CefSharp.Core/NativeMethodWrapper.h @@ -14,5 +14,6 @@ namespace CefSharp static void CopyMemoryUsingHandle(IntPtr dest, IntPtr src, int numberOfBytes); static bool IsFocused(IntPtr handle); static void SetWindowPosition(IntPtr handle, int x, int y, int width, int height); + static void SetWindowParent(IntPtr child, IntPtr newParent); }; } \ No newline at end of file diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs index 27f84930c..dfffa86b8 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -552,7 +552,16 @@ namespace CefSharp.WinForms if (((IWebBrowserInternal)this).HasParent == false) { - managedCefBrowserAdapter.CreateBrowser(BrowserSettings, (RequestContext)requestContext, Handle, Address); + if(IsBrowserInitialized == false || browser == null) + { + managedCefBrowserAdapter.CreateBrowser(BrowserSettings, (RequestContext)RequestContext, Handle, Address); + } + else + { + //If the browser already exists we'll reparent it to the new Handle + var browserHandle = browser.GetHost().GetWindowHandle(); + NativeMethodWrapper.SetWindowParent(browserHandle, Handle); + } } }