RE: Hidden WebBrowser stealing focus
- From: Dave Booker <dbooksta@xxxxxxxxxxxxxxxx>
- Date: Fri, 14 Jul 2006 12:41:01 -0700
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.
- Follow-Ups:
- RE: Hidden WebBrowser stealing focus
- From: "Jeffrey Tan[MSFT]"
- RE: Hidden WebBrowser stealing focus
- References:
- RE: Hidden WebBrowser stealing focus
- From: "Jeffrey Tan[MSFT]"
- RE: Hidden WebBrowser stealing focus
- Prev by Date: DataGrid Column sort!
- Next by Date: c# widows services...
- Previous by thread: RE: Hidden WebBrowser stealing focus
- Next by thread: RE: Hidden WebBrowser stealing focus
- Index(es):
Relevant Pages
|