当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Webbrowser steals focus

问题描述:

I'm using webbrowser control in my winforms app (c#). And when it is doing automation things, I'm losing focus control from the window I was working with. Webbrowsers' form doesn't show up also, I just lose focus from the contol. I now writing this message I have to click into textbox again and again...

How to disable such behaviour in webbrowser?

I create invisible webbrowser like that:

var br = new WebBrowser();

br.Visible = false;

br.ScriptErrorsSuppressed = true;

Please advise.

网友答案:

I had the same problem:

The Webbrowser Control stole focus from the application once the URL is loaded.

This worked for me:

  1. Before Webbrowser.Navigate() method call, set the parent control of the Webbrowser to Enabled = false.
  2. At the DocumentCompleted event of the Webbrowser, reset parent control of the Webbrowser to Enabled = true.

You can't do it directly on Webbrowser because WebBrowserBase.Enabled is not supported.

Let me know if it works for you.

网友答案:

You could try disabling it globally via the SystemParametersInfo api. Use SPI_SETFOREGROUNDLOCKTIMEOUT. Setting foreground lockout is a global settings, so you will want to clear this setting when you're done. A more permanent solution is to change HKCU\Control Panel\Desktop\ForegroundLockTimeout registry key. See also this discussion on social.msdn (specifically, billb08's answer).

网友答案:

I guess WebBrowser acquires the focus after a page is loaded by calling Navigate (or the Click method of an HtmlElement, which causes navigation). The focus could be given back to the control on the window (the TextBox) in the DocumentComplete event handler of the WebBrowser, but this is very difficult:

  1. When would you determine which control owned the focus originally? Before calling Navigate? This is not enough, because the user can move to another control after calling Navigate, but before handling DocumentComplete.

  2. AFAIK setting the focus to a TextBox will select its whole content, so you will have to put the cursor back to its original position. But when would you store the original position? Same problem.

  3. There can be more than one DocumentComplete event after a single Navigate (or Click).

A possible solution would be to create a separate application for your hidden WebBrowser. This second application would be invisible, and could communicate with the original GUI application using some InterProcess Communication (IPC) technique. Because the WebBrowser in this case would run in a different process, you would have a better chance not to lose lose the focus and bother the user.

网友答案:

it's a very complex problem to fix, and should be revised by microsoft, an app just stealing the focus is not logical, it does depend on what the website is doing though. I had to resort to a CBT filter, see http://msdn.microsoft.com/en-us/magazine/cc188966.aspx, and filter out unwanted HCBT_ACTIVATE and HCBT_SETFOCUS (return 1;). You can use GetWindowClass(wParam) to see what's going on.

Even above didn't entirely work, the app window would still pop to the front temporarily so worked around that using SetWindowPos HWND_TOPMOST and HWND_NOTOPMOST on the window currently in foreground. The HCBT_SETFOCUS gets hit 2 or 3 times so on 1st set HWND_TOPMOST and last set HWND_NOTOPMOST. Count how many classname == "Internet Explorer_Server" which should be 2 (or possibly depends on website?), the other is "Shell Embedding" but doesn't always occur. Hope it helps.

网友答案:

I was looking at all the other answers to this question and they weren't working for me, but i saw the one about settings Browser.Parent.Enabled = false; i tried so and got an error, so i tried this instead it just came to mind.

Browser.Parent = new Control();

Browser.Parent.Enabled = false;

And now the problem is completely gone, it does not take away focus anymore. I am using the web browser class as a variable, it is not on my form. well this worked for me try it, this seemed to be a 100% solution.

网友答案:
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);
    }
分享给朋友:
您可能感兴趣的文章:
随机阅读: