C# ChromiumWebBrowser: Prevent control from stealing focus

£可爱£侵袭症+ 提交于 2019-12-12 12:32:37

问题


I'm making a program that has a Form with a ChromiumWebBrowser in it. The navigation is done automatically. When webbrowser complete it's task, I'll dispose it, create a new webbrowser, add it to form, and load a new address.

But, when the new webbrowser was created and added to form, the program jumps in front of what ever other program is in the top with focus. Example: I start my program, press the button to start its task, open notepad to type some text and my program jumps in front of it when navigating to a new site.

Even when the window is minimized, it still steals focus from other open programs.

How do I prevent it stealing focus after it is created?


回答1:


As @amaitland said, this looks like a bug.

Workarounds I've used are:

1) disable the browser. This will prevent the browser from receiving mouse/keyboard input, but it won't "grey-out" the control.

Browser1 = New CefSharp.WinForms.ChromiumWebBrowser(url)
Browser1.Enabled = False

2) Pass a callback .net function for when the page loads where you simply put the focus back to winforms by focusing on a label of your choice.

Label1.Focus()



回答2:


  1. Your Form need set: this.Topmost = false; AND just set: this.BringToFront();

  2. Add new browser to the form just like the function as follow:

    private ChromiumWebBrowser AddNewBrowser(FATabStripItem tabStrip, String url)
    {
        if (url == "")
        {
            url = OpenUrl;
            txtUrl.Select();
            txtUrl.Focus();
        }
        else
        {
            tabStrip.Select();
            tabStrip.Focus();
        }
        // ...
    }
    

Hope has help to you. Thanks !



来源:https://stackoverflow.com/questions/37117125/c-sharp-chromiumwebbrowser-prevent-control-from-stealing-focus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!