mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-05 11:42:36 +00:00
* Code Review changes
* Add float right select drop down for additional move test.
This commit is contained in:
@@ -14,6 +14,11 @@
|
||||
<option>one</option>
|
||||
<option>two</option>
|
||||
</select>
|
||||
<select style="float: right">
|
||||
<option selected>testRight</option>
|
||||
<option>three</option>
|
||||
<option>four</option>
|
||||
</select>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
@@ -54,7 +54,8 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="Internals\DefaultFocusHandler.cs" />
|
||||
<Compile Include="Internals\ControlExtensions.cs" />
|
||||
<Compile Include="Internals\MovingListener.cs" />
|
||||
<Compile Include="Internals\ParentFormMessageInterceptor.cs" />
|
||||
<Compile Include="Internals\NativeMethods.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="ChromiumWebBrowser.cs">
|
||||
<SubType>Component</SubType>
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
+82
-38
@@ -10,31 +10,40 @@ using System.Windows.Forms;
|
||||
|
||||
namespace CefSharp.WinForms.Internals
|
||||
{
|
||||
internal class MovingListener : NativeWindow
|
||||
internal class ParentFormMessageInterceptor : NativeWindow, IDisposable
|
||||
{
|
||||
public event EventHandler<EventArgs> 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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Call to force refinding of the parent Form.
|
||||
/// (i.e. top level window that owns the ChromiumWebBrowserControl)
|
||||
/// </summary>
|
||||
public void RefindParentForm()
|
||||
{
|
||||
ParentParentChanged(ParentControl, null);
|
||||
ParentParentChanged(Browser, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adjust the form to listen to if the ChromiumWebBrowserControl's parent changes.
|
||||
/// </summary>
|
||||
/// <param name="sender">The ChromiumWebBrowser whose parent has changed.</param>
|
||||
/// <param name="e"></param>
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user