diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs index cfe5b9d7cb..95b6e8d343 100644 --- a/CefSharp.WinForms/ChromiumWebBrowser.cs +++ b/CefSharp.WinForms/ChromiumWebBrowser.cs @@ -344,6 +344,32 @@ public Task GetTextAsync() return taskStringVisitor.Task; } + /// + /// Manually implement Focused because cef does not implement it. + /// + /// + /// This is also how the Microsoft's WebBrowserControl implements the Focused property. + /// + public override bool Focused + { + get + { + if (base.Focused) + { + return true; + } + + if (!IsHandleCreated) + { + return false; + } + + // Ask Windows which control has the focus and then check if it's one of our children + IntPtr focus = User32.GetFocus(); + return focus != IntPtr.Zero && User32.IsChild(Handle, focus); + } + } + protected override void OnSizeChanged(EventArgs e) { base.OnSizeChanged(e); diff --git a/CefSharp/CefSharp.csproj b/CefSharp/CefSharp.csproj index 82c6541505..c761eb579d 100644 --- a/CefSharp/CefSharp.csproj +++ b/CefSharp/CefSharp.csproj @@ -89,6 +89,7 @@ + diff --git a/CefSharp/Internals/User32.cs b/CefSharp/Internals/User32.cs new file mode 100644 index 0000000000..5e6b5a1c65 --- /dev/null +++ b/CefSharp/Internals/User32.cs @@ -0,0 +1,18 @@ +// Copyright © 2010-2014 The CefSharp Authors. All rights reserved. +// +// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. + +using System; +using System.Runtime.InteropServices; + +namespace CefSharp.Internals +{ + public static class User32 + { + [DllImport("User32.dll")] + public extern static IntPtr GetFocus(); + + [DllImport("user32.dll")] + public extern static bool IsChild(IntPtr parent, IntPtr child); + } +} \ No newline at end of file