RE: Hidden WebBrowser stealing focus



This is both fascinating and unfortunate.

It is easy enough to wrap the call to the browser's .Navigate in a function
that reads and resets focus. I.e., instead of having the WebBrowser
subscribe to the Timer, my own event handler subscribes to it and within the
eventhandler I:
1. Make a note of the current user focus
2. Call .Navigate()
3. Reset the focus to what it was before

But the problem is compounded by the fact that the user may have switched to
another application. Even this brief flicker of focus is enough to disrupt
any controls with which the user is interacting (e.g., scrolling).

So if you there is another way to prevent the WebBrowser from taking focus
at all that would be best. (Should I submit that requirement as a support
incident for a custom solution?)

But if that's not immediately possible, could you provide sample code to
read the user's focus even if it's in another application, and return focus
to that application and its active control?


""Jeffrey Tan[MSFT]"" wrote:

Hi Dbooksta,

Thanks for your post!

Yes, I can reproduce out this behavior. Based on my research, it is the
mshtml.dll(which is the internal component of WebBrowser) that calls
SetFocus to change the application focus into the page. Since WebBrowser
control does not expose any interface for changing this behavior and the
code is embeded deep inside mshtml.dll, there is no perfect solution to
resolve this issue.

To workaround this issue, we have to reset back the focus into the original
focused control. This can be easily done with Control.Focus method. The key
problem is when and where to call this method. It is easy to conclude that
we should call it after the focus is changed to the WebBrowser. But after
testing all the events of WebBrowser, I found that all the events are
called before the focus is changed, so they all do not meet our need. A
simple workaround is place a second timer in DocumentCompleted event and
delay the calling. Something like this:

void webBrowser1_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
this.timer2.Interval = 100;
this.timer2.Start();
this.timer2.Tick += new EventHandler(timer2_Tick);
}

void timer2_Tick(object sender, EventArgs e)
{
this.textBox1.Focus();
this.timer2.Stop();
}
The disadvantage of this approach is that it is hard to determine the
interval for timer2. You may have to do some test to choose a most suitable
interval.

My another thought is hooking Focused control's Leave event and reset the
focus in this event. However, based on my test, this event does not fire at
all. Based on the further research, I found a WM_KILLFOCUS message is also
sent to the original focused control, so logically, we can monitor
WM_KILLFOCUS message and reset the focus back while this message appears.
However, monitoring an arbitrary control's message is not easy to be done
in .Net Winform, we have 2 choices: using NativeWindow to subclass that
control or we can use a local process hook to monitor all the messages of
all the controls in the application.

This approach is more complex than the first one, but it does not depend on
an Interval. If you are curious with this approach, please feel free to
tell me, I will help you to work this approach out. Thanks!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


.



Relevant Pages

  • RE: Hidden WebBrowser stealing focus
    ... Since WebBrowser ... control does not expose any interface for changing this behavior and the ... sent to the original focused control, so logically, we can monitor ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Printing HTML from Window Forms
    ... private static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint Msg, ... control) is not up to the task of printing reliably from a Windows Forms. ... Maybe you could suggest to the team in charge of WebBrowser that they should ... add a bit more functionality to the managed control so that you can specify a ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: BIOS startup ??
    ... the reset threshold of the processor, ... disk drives have their heads ... clocks in the system, for buss access, video control, port clocking etc. ... And typically all the clocks are ...
    (Fedora)
  • Re: Delegation - Password Reset - Access Denied
    ... Control Wizard from the MS TechNet web site. ... Yet a user in that group gets and error when trying to reset a password. ... BLOG --> http://blogs.dirteam.com/blogs/jorge/default.aspx ... Administrators Group defeat the purpose of using Deligation? ...
    (microsoft.public.windows.server.active_directory)
  • Re: Context Menu for Embedded WebBRowser
    ... I don't remember how the WebBrowser control works. ... > Hi Paul ... > click on the context menu. ...
    (microsoft.public.dotnet.framework.compactframework)

Quantcast