From 7c3dcb656ea368ba72a65191c2767552dffaf6ec Mon Sep 17 00:00:00 2001 From: rassilon Date: Wed, 11 Feb 2015 17:30:33 -0500 Subject: [PATCH] * Code Review changes * Add float right select drop down for additional move test. --- CefSharp.Example/Resources/TooltipTest.html | 5 + CefSharp.WinForms/CefSharp.WinForms.csproj | 3 +- CefSharp.WinForms/ChromiumWebBrowser.cs | 12 +- CefSharp.WinForms/Internals/NativeMethods.cs | 13 ++ ...ner.cs => ParentFormMessageInterceptor.cs} | 120 ++++++++++++------ 5 files changed, 108 insertions(+), 45 deletions(-) create mode 100644 CefSharp.WinForms/Internals/NativeMethods.cs rename CefSharp.WinForms/Internals/{MovingListener.cs => ParentFormMessageInterceptor.cs} (56%) diff --git a/CefSharp.Example/Resources/TooltipTest.html b/CefSharp.Example/Resources/TooltipTest.html index 6de4139d7..0295225b4 100644 --- a/CefSharp.Example/Resources/TooltipTest.html +++ b/CefSharp.Example/Resources/TooltipTest.html @@ -14,6 +14,11 @@ + \ No newline at end of file diff --git a/CefSharp.WinForms/CefSharp.WinForms.csproj b/CefSharp.WinForms/CefSharp.WinForms.csproj index b4b177cd6..c17b5eee6 100644 --- a/CefSharp.WinForms/CefSharp.WinForms.csproj +++ b/CefSharp.WinForms/CefSharp.WinForms.csproj @@ -54,7 +54,8 @@ - + + Component diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs index 0e8ebac13..f028014e1 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -14,7 +14,7 @@ namespace CefSharp.WinForms public class ChromiumWebBrowser : Control, IWebBrowserInternal, IWinFormsWebBrowser { private ManagedCefBrowserAdapter managedCefBrowserAdapter; - private MovingListener movingListener; + private ParentFormMessageInterceptor parentFormMessageInterceptor; public BrowserSettings BrowserSettings { get; set; } public string Title { get; set; } @@ -107,13 +107,13 @@ namespace CefSharp.WinForms IsBrowserInitialized = true; // By the time this callback gets called, this control - // is most likely hooked into a parent Form of some sort. - // (Which is what MovingListener relies on.) - // Ensure the MovingListener construction occurs on the WinForms UI thread: + // is most likely hooked into a browser Form of some sort. + // (Which is what ParentFormMessageInterceptor relies on.) + // Ensure the ParentFormMessageInterceptor construction occurs on the WinForms UI thread: this.InvokeOnUiThreadIfRequired(() => { - movingListener = new MovingListener(this); - movingListener.Moving += MovingListenerMoving; + parentFormMessageInterceptor = new ParentFormMessageInterceptor(this); + parentFormMessageInterceptor.Moving += MovingListenerMoving; }); ResizeBrowser(); diff --git a/CefSharp.WinForms/Internals/NativeMethods.cs b/CefSharp.WinForms/Internals/NativeMethods.cs new file mode 100644 index 000000000..3710be351 --- /dev/null +++ b/CefSharp.WinForms/Internals/NativeMethods.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace CefSharp.WinForms.Internals +{ + internal static class NativeMethods + { + public const int WM_MOVE = 0x3; + public const int WM_MOVING = 0x216; + } +} diff --git a/CefSharp.WinForms/Internals/MovingListener.cs b/CefSharp.WinForms/Internals/ParentFormMessageInterceptor.cs similarity index 56% rename from CefSharp.WinForms/Internals/MovingListener.cs rename to CefSharp.WinForms/Internals/ParentFormMessageInterceptor.cs index f5a824600..62148a181 100644 --- a/CefSharp.WinForms/Internals/MovingListener.cs +++ b/CefSharp.WinForms/Internals/ParentFormMessageInterceptor.cs @@ -10,31 +10,40 @@ using System.Windows.Forms; namespace CefSharp.WinForms.Internals { - internal class MovingListener : NativeWindow + internal class ParentFormMessageInterceptor : NativeWindow, IDisposable { public event EventHandler Moving; private bool isMoving = false; private Rectangle movingRectangle; - private Control ParentControl { get; set; } + private ChromiumWebBrowser Browser { get; set; } private Form ParentForm { get; set; } - public MovingListener(Control parent) + public ParentFormMessageInterceptor(ChromiumWebBrowser browser) { - ParentControl = parent; - // Get notified if our parent window changes: - ParentControl.ParentChanged += ParentParentChanged; - // Find the parent form to subclass to monitor WM_MOVE/WM_MOVING + Browser = browser; + // Get notified if our browser window parent changes: + Browser.ParentChanged += ParentParentChanged; + // Find the browser form to subclass to monitor WM_MOVE/WM_MOVING RefindParentForm(); } + /// + /// Call to force refinding of the parent Form. + /// (i.e. top level window that owns the ChromiumWebBrowserControl) + /// public void RefindParentForm() { - ParentParentChanged(ParentControl, null); + ParentParentChanged(Browser, null); } + /// + /// Adjust the form to listen to if the ChromiumWebBrowserControl's parent changes. + /// + /// The ChromiumWebBrowser whose parent has changed. + /// private void ParentParentChanged(object sender, EventArgs e) { Control control = (Control)sender; @@ -59,6 +68,8 @@ namespace CefSharp.WinForms.Internals { newForm.HandleCreated += OnHandleCreated; newForm.HandleDestroyed += OnHandleDestroyed; + // If newForm's Handle has been created already, + // our event listener won't be called, so call it now. if (newForm.IsHandleCreated) { OnHandleCreated(newForm, null); @@ -70,7 +81,6 @@ namespace CefSharp.WinForms.Internals private void OnHandleCreated(object sender, EventArgs e) { AssignHandle(((Form)sender).Handle); - Kernel32.OutputDebugString(String.Format("Attaching '{1:x}' to form '{0:x}'\r\n", ParentForm.Handle, ParentControl.Handle)); } private void OnHandleDestroyed(object sender, EventArgs e) @@ -80,7 +90,7 @@ namespace CefSharp.WinForms.Internals protected override void WndProc(ref Message m) { - bool care = false; + bool isMovingMessage = false; // Negative initial values keeps the compiler quiet and to // ensure we have actual window movement to notify CEF about. @@ -91,27 +101,29 @@ namespace CefSharp.WinForms.Internals // Listen for operating system messages switch (m.Msg) { - // WM_MOVING: - case 0x0216: - movingRectangle = (Rectangle)Marshal.PtrToStructure(m.LParam, typeof(Rectangle)); - x = movingRectangle.Left; - y = movingRectangle.Top; - care = true; - break; - // WM_MOVE - case 0x3: - x = (m.LParam.ToInt32() & 0xffff); - y = ((m.LParam.ToInt32() >> 16) & 0xffff); - care = true; - break; + case NativeMethods.WM_MOVING: + { + movingRectangle = (Rectangle)Marshal.PtrToStructure(m.LParam, typeof(Rectangle)); + x = movingRectangle.Left; + y = movingRectangle.Top; + isMovingMessage = true; + break; + } + case NativeMethods.WM_MOVE: + { + x = (m.LParam.ToInt32() & 0xffff); + y = ((m.LParam.ToInt32() >> 16) & 0xffff); + isMovingMessage = true; + break; + } } // Only notify about movement if: - // * ParentControl Handle Created + // * Browser Handle Created // NOTE: This is checked for paranoia. // This WndProc can't be called unless ParentForm has // its handle created, but that doesn't necessarily mean - // ParentControl has had its handle created. + // Browser has had its handle created. // WinForm controls don't usually get eagerly created Handles // in their constructors. // * ParentForm Actually moved @@ -120,12 +132,12 @@ namespace CefSharp.WinForms.Internals // This check is to simplify the effort here. // Other logic already handles the maximize/minimize // cases just fine. - // You might consider checking ParentControl.Visible and - // not notifying our parent control if the parent control isn't visible. + // You might consider checking Browser.Visible and + // not notifying our browser control if the browser control isn't visible. // However, if you do that, the non-Visible CEF tab will still // have any SELECT drop downs rendering where they shouldn't. - if (care - && ParentControl.IsHandleCreated + if (isMovingMessage + && Browser.IsHandleCreated && ParentForm.WindowState == FormWindowState.Normal && (ParentForm.Left != x || ParentForm.Top != y) @@ -139,16 +151,7 @@ namespace CefSharp.WinForms.Internals && ParentForm.Right >= 0) { OnMoving(); - Kernel32.OutputDebugString(String.Format("Called OnMoving from control '{1:x}' to form '{0:x}'\r\n", ParentForm.Handle, ParentControl.Handle)); } - else - { - Kernel32.OutputDebugString(String.Format("Skipped OnMoving from control '{1:x}' to form '{0:x}'\r\n", ParentForm.Handle, ParentControl.Handle)); - } - } - else if (care) - { - Kernel32.OutputDebugString(String.Format("Skipped OnMoving from control '{1:x}' to form '{0:x}'\r\n", ParentForm.Handle, ParentControl.Handle)); } this.DefWndProc(ref m); } @@ -164,5 +167,46 @@ namespace CefSharp.WinForms.Internals } isMoving = false; } + + public void Dispose() + { + Dispose(true); + } + + protected virtual void Dispose(bool disposing) + { + if (disposing) + { + if (ParentForm != null) + { + ParentForm.HandleCreated -= OnHandleCreated; + ParentForm.HandleDestroyed -= OnHandleDestroyed; + ParentForm = null; + } + + // Unmanaged resource, but release here anyway. + // NativeWindow has its own finalization logic + // that should be run if this instance isn't disposed + // properly before arriving at the finalization thread. + // See: http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/NativeWindow.cs,147 + // for the gruesome details. + if (this.Handle != IntPtr.Zero) + { + ReleaseHandle(); + } + + if (Browser != null) + { + Browser.ParentChanged -= ParentParentChanged; + Browser = null; + } + } + } + + protected override void OnThreadException(Exception e) + { + // TODO: Do something more interesting here, logging, whatever, something. + base.OnThreadException(e); + } } }