Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WinForms - Browser switches focus after SendMouseClickEvent when two browser forms shown #4280

Closed
erches opened this issue Oct 20, 2022 · 2 comments

Comments

@erches
Copy link

erches commented Oct 20, 2022

What version of the product are you using?

106.0.290

What architecture x86 or x64?

x64

On what operating system?

Win10

Are you using WinForms, WPF or OffScreen?

WinForms/WPF (using WindowsFormsHost component)

What steps will reproduce the problem?

I create and show two browser windows with ChromiumWebBrowser control at the same time and then start sending left mouse click events using SendMouseClickEvent function with a short period of time. Browser windows start to switch focus.

I have no such problem with version 79.1.360 . This behavior appears with CefSharp versions above 79.1.360.

Program.cs

static void Main()
{
    Application.Run(new AppContext());
}

AppContext.cs

public class AppContext : ApplicationContext
{
    private Browser form1;
    private Browser form2;

    public AppContext()
    {
        InitCef();

        form1 = new Browser();
        form1.WindowState = FormWindowState.Normal;

        form2 = new Browser();
        form2.WindowState = FormWindowState.Normal;

        form1.FormClosed += OnFormClosed;
        form2.FormClosed += OnFormClosed;

        form1.Show();
        form2.Show();

        var task1 = CreateClicksTask(form1.browser);
        var task2 = CreateClicksTask(form2.browser);

        task1.Start();
        task2.Start();
    }

    private void OnFormClosed(object sender, FormClosedEventArgs e)
    {
        if (Application.OpenForms.Count == 0)
        {
            ExitThread();
        }
    }

    private void InitCef()
    {
        Cef.Initialize(new CefSettings()
            {
                LogSeverity = LogSeverity.Error
            });
    }
        

    Task CreateClicksTask(ChromiumWebBrowser browser)
    {
        return new Task(() =>
        {
            for (int i = 0; i < 300; i++)
            {
                browser.GetBrowserHost().SendMouseClickEvent(100, 200, MouseButtonType.Left, false, 1, CefEventFlags.None);
                browser.GetBrowserHost().SendMouseClickEvent(100, 200, MouseButtonType.Left, true, 1, CefEventFlags.None);
                Thread.Sleep(1000);
            }
        });
    }
}

Browser.cs

public partial class Browser : Form
{
    public ChromiumWebBrowser browser;
    public Browser()
    {
        InitializeComponent();
        browser = new ChromiumWebBrowser("www.google.com")
        {
            Dock = DockStyle.Fill,
        };
        Controls.Add(browser);
    }
}
@amaitland
Copy link
Member

amaitland commented Oct 20, 2022

The behaviour is implemented entirely in chromium, not something I can control/fix.

@amaitland amaitland closed this as not planned Won't fix, can't repro, duplicate, stale Oct 20, 2022
@amaitland
Copy link
Member

As an alternative you can try using the DevTools protocol which has mouse emulation methods.

https://chromedevtools.github.io/devtools-protocol/tot/Input/#method-dispatchMouseEvent

See #3165 for details on how to use the strongly typed DevTools methods in CefSharp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants