Webbrowser steals focus

I am using a webbrowser control in my winforms application (C #). And when he does automation things, I lose focus control from the window I worked with. The Webbrowsers form is also not showing, I just lose focus with contol. Now I am writing this message, I need to click on the text field again and again ...

How to disable this behavior in webbrowser?

I create an invisible web browser this way:

var br = new WebBrowser(); br.Visible = false; br.ScriptErrorsSuppressed = true; 

Please inform.

+4
source share
7 answers

I had the same problem:

Managing your web browser has stolen focus from the application after loading the URL.

This worked for me:

  • Before calling the Webbrowser.Navigate() method, set the parent Webbrowser control to Enabled = false .
  • In the DocumentCompleted event of the web browser, reset the parent Webbrowser control to Enabled = true .

You cannot do this directly in Webbrowser because WebBrowserBase.Enabled not supported.

Let me know if this works for you.

+14
source

You can try to disable it globally through the SystemParametersInfo api. Use SPI_SETFOREGROUNDLOCKTIMEOUT . Foreground lock settings are global settings, so you will need to clear this option when done. A more permanent solution is to change the HKCU \ Control Panel \ Desktop \ ForegroundLockTimeout registry key. See also this discussion on social.msdn (specifically billb08's answer).

+2
source

I think WebBrowser gets focus after the page loads by calling Navigate (or the Click HtmlElement method, which invokes navigation). The focus can be returned to control in the window ( TextBox ) in the DocumentComplete WebBrowser event handler, but it is very difficult:

  • When you determine which control belongs to the focus initially? Before calling Navigate ? This is not enough, because the user can switch to another control after calling Navigate , but before processing DocumentComplete .

  • AFAIK, which sets focus on the TextBox , will select all the content, so you will need to return the cursor to the original position. But when did you keep your starting position? Same problem.

  • After a single event, there may be more than one DocumentComplete Navigate (or Click ) event.

A possible solution would be to create a separate application for your hidden WebBrowser . This second application would be invisible and could interact with the original graphical interface using some InterProcess Communication (IPC) technique. Since WebBrowser in this case will work in a different process, you will have a better chance of not losing focus and disturbing the user.

+2
source

This is a very difficult problem to fix and needs to be reviewed using microsoft, the application, just stealing focus, is illogical, it depends on what the website is doing. I had to resort to the CBT filter, see http://msdn.microsoft.com/en-us/magazine/cc188966.aspx and filter out the unwanted HCBT_ACTIVATE and HCBT_SETFOCUS (return 1;). You can use GetWindowClass (wParam) to find out what is happening.

Not everything works even above, the application window will still appear on the front, so it worked around it using SetWindowPos HWND_TOPMOST and HWND_NOTOPMOST in the window in the foreground. HCBT_SETFOCUS hits 2 or 3 times, so the 1st set is HWND_TOPMOST and the last set is HWND_NOTOPMOST. Calculate how much classname == "Internet Explorer_Server" should be 2 (or maybe depends on the website?), And the other is "Shell attachment", but it does not always happen. Hope this helps.

+2
source

I looked at all the other answers to this question, and they did not work for me, but I saw one thing about the settings of Browser.Parent.Enabled = false; I tried it and got an error, so I tried it, but it occurred to me.

Browser.Parent = new Control();

Browser.Parent.Enabled = false;

And now the problem has completely disappeared, it no longer takes focus. I use the web browser class as a variable, this is not my form. well it worked for me, try it, it seems like a 100% solution.

+2
source
 Most of the methods won't work for me on more than one web browser. This method is work with any amount of web browsers; 1. Put web browser into a panel and set panel enabled to false, then navigate; webBrowser.Parent = panelBottom; panelWebBrowser.Enabled = false; webBrowser.Navigate("http://www.google.com"); 2. Define a navigated event to web browser and delay panels enabling for a second; private void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e) { System.Threading.Timer timer = null; timer = new System.Threading.Timer((obj) => { panelWebBrowser.Enabled = true; timer.Dispose(); },null, 1000, Timeout.Infinite); } 
0
source

My solution to send focus back to the form:

  Private Sub Web_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles Web.DocumentCompleted If Me.Visible = False Then For Each f As Form In My.Application.OpenForms If TypeOf f Is frmLogin Then Dim fl As frmLogin = DirectCast(f, frmLogin) If fl.Visible = True Then fl.Focus() Exit For End If End If Next End If End Sub 
0
source

Source: https://habr.com/ru/post/1386184/


All Articles