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); + } } }