473,699 Members | 2,353 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hidden WebBrowser stealing focus

I am using a WebBrowser object in my .NET 2.0 application, but it is not
shown to the user. Every time a timer event triggers it to perform a
m_WebBrowser.Na vigate() I get that classic IE 'click' and it steals the focus
from the user's current application.

How can I prevent the hidden WebBrowser from stealing focus? (And better
yet can I even suppress that click?_
Jul 13 '06 #1
7 11921
Hi Dbooksta,

Thanks for your post!

Yes, I can reproduce out this behavior. Based on my research, it is the
mshtml.dll(whic h 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 DocumentComplet ed event and
delay the calling. Something like this:

void webBrowser1_Doc umentCompleted( object sender,
WebBrowserDocum entCompletedEve ntArgs e)
{
this.timer2.Int erval = 100;
this.timer2.Sta rt();
this.timer2.Tic k += new EventHandler(ti mer2_Tick);
}

void timer2_Tick(obj ect sender, EventArgs e)
{
this.textBox1.F ocus();
this.timer2.Sto p();
}
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.

Jul 14 '06 #2
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(whic h 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 DocumentComplet ed event and
delay the calling. Something like this:

void webBrowser1_Doc umentCompleted( object sender,
WebBrowserDocum entCompletedEve ntArgs e)
{
this.timer2.Int erval = 100;
this.timer2.Sta rt();
this.timer2.Tic k += new EventHandler(ti mer2_Tick);
}

void timer2_Tick(obj ect sender, EventArgs e)
{
this.textBox1.F ocus();
this.timer2.Sto p();
}
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.

Jul 14 '06 #3
Hi Dbooksta,

Thanks for your feedback!

Sorry, do you mean that you reset the focus after Navigate() method and it
works in your scenario? However, based on my test, this does not work on my
side, below is my testing code snippet:
void timer1_Tick(obj ect sender, EventArgs e)
{
this.webBrowser 1.Navigate("htt p://www.google.com" );
this.textBox1.F ocus();
this.timer1.Sto p();
}
I recommend you give it a recheck. Anyway, if it works on your side, you
may go with it.

Regarding your further question, I am not sure if I understand it
completely. If you invoked Navigate() method and switch to another
application what is the problem you are encounterring? Thanks for clarify.

Normally, I think you may hook Form.Activated event and reset the focus
into the original focused control, like this:
private void Form1_Activated (object sender, EventArgs e)
{
this.textBox1.F ocus();
}

Then whenever the user switch back to your application, the Activated event
handler will reset the focus as your need.

Finally, based on my research, not all web page you are accessing will
steal the focus, it is the javascript/jscript Element.focus() function that
causes the focus stealing behavior. If you view the source code of
www.google.com, you will find the following line in it:
function sf(){document.f .q.focus();}

Any web page does not call the focus internally will not steal the focus.
So I do not think there is any way to prevent the WebBrowser control from
stealing the focus, it is the javascript/Jscript code that steals the
focus. This behavior is by design. If you are curious, below is call stack
I captured with windbg for this stealing focus:

0013b84c 4a570824 USER32!NtUserSe tFocus
0013b858 4a5ce628 mshtml!CDoc::Ta keFocus+0x2a
0013b880 4a63fc0b mshtml!CElement ::BecomeCurrent +0x167
0013b8b4 4a63fb72 mshtml!CElement ::focusHelper+0 xcc
0013b8c0 4a587c85 mshtml!CElement ::focus+0x1d
0013b8cc 4a5d7477 mshtml!Method_v oid_void+0x17
0013b94c 4a57fae8 mshtml!CBase::C ontextInvokeEx+ 0x462
0013b97c 4a575413 mshtml!CElement ::ContextInvoke Ex+0x72
0013b9b0 76fa5295 mshtml!CElement ::ContextThunk_ InvokeEx+0x44
0013b9e8 76fa5208 jscript!IDispat chExInvokeEx2+0 xa9
0013ba20 76fa5323 jscript!IDispat chExInvokeEx+0x 56
0013ba90 76fa577b jscript!InvokeD ispatchEx+0x78
0013bad8 76fa57c6 jscript!VAR::In vokeByName+0x1c 1
0013bb18 76fa4ab0 jscript!VAR::In vokeDispName+0x 43
0013bb3c 76fa5a14 jscript!VAR::In vokeByDispID+0x fb
0013bd30 76fa46d8 jscript!CScript Runtime::Run+0x 195b
0013bdf4 76fa506e jscript!ScrFncO bj::Call+0x69
0013be6c 76fa5f6a jscript!CSessio n::Execute+0xb8
0013bf6c 76fa672f jscript!NameTbl ::InvokeDef+0x1 83
0013c040 76fa5295 jscript!NameTbl ::InvokeEx+0xd2

Hope this helps. Also, please feel free to feedback any concern or
questions. Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 17 '06 #4
OK, just to clarify:

1. I see that this focus problem is associated with navigating to web pages
that demand focus, e.g., through jscript .focus().

2. I am running calls to .Navigate() to such pages using a Timer.
Therefore, the user will probably be in a completely different application
when the .Navigate() is called. So the trick would be preventing the
WebBrowser from stealing focus from that other application.

3. Or, not as good as preventing the focus-change to begin with, at least
having the WebBrowser process return the focus to the other application and
whatever control the user had active in that other application.

I hope this is clear. If so: Is either 2 or 3 possible in a trivial way?
Or should I submit this requirement it for paid support?
Jul 17 '06 #5
Hi Dbooksta,

Thanks for your feedback!
So the trick would be preventing the
WebBrowser from stealing focus from that other application.
If I did not misunderstand you, do you mean that while the Navigate method
is executing with Timer, you are currently working on some other
application(the WebBrowser application is in background), and you find that
WebBrowser.Navi gate method will steal focus from the foreground
application.

However, based on my experience, the background application can not steal
the focus of foreground application. My testing application also uses a
timer to execute the Navigate method after clicking a button 3 seconds,
like this:

private void button1_Click(o bject sender, EventArgs e)
{
this.timer1.Sta rt();
this.timer1.Int erval = 3000;
this.timer1.Tic k += new EventHandler(ti mer1_Tick);
}

void timer1_Tick(obj ect sender, EventArgs e)
{
this.webBrowser 1.Navigate("htt p://www.google.com" );
this.timer1.Sto p();
}
In my testing application, after clicking the button, I switch to a notepad
immediately, the WebBrowser application in background can not steal the
focus from the foreground notepad application.

If you are seeing the different behavior, is it possible to provide a
little sample to demonstrate stealing focus from another application
behavior?
>Or should I submit this requirement it for paid support?
Yes, I want to continue help you on newsgroup for this issue. Anyway, if
you feel the PSS case support is more efficiently, please feel free to go
ahead and tell me.

Thanks!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 18 '06 #6
Shoot, it looks like I can't produce a simple example of that extended
behavior I thought I was seeing.

Let's assume this problem is no more complicated than you described ...
unless in the future I discover otherwise.

Thanks for the help!

Jul 18 '06 #7
Ok, if you need further help, please feel free to post, I will work with
you. Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 19 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4140
by: wesley | last post by:
Hello, My application is hosting three WebBrowser control. What I am trying to do is when the mouse move over one of the WebBrowser controls I want to set the focus on that control so the mouse wheel can move correctly. At the moment I have to click on the WebBrowser control itself to make the mouse wheel to work. In addition to that, the WebBrowser control does not expose a MouseMove/Over/Enter events. Does anyone know how to do this?...
2
9398
by: Robert Misiak | last post by:
Is it possible to display a window on top without it stealing focus? (and I'm not talking about stealing focus from my application - I'm talking about it stealing focus from any application.) I've read that calling ShowWindow() with SW_SHOWNOACTIVATE would work but I tried and it didn't. I'm implementing a Windows Messenger-style notification window that occasionally pops up with information. Thanks, Robert
0
1335
by: gkelly | last post by:
I have an AppBar that works fine, except for one problem. It's stealing focus -The AppBar is written in C# -Access to it is via a COM object -Main app is a C++ app. -Button on app will use com to access C# object which pops up AppBar -When the appbar is floating it works fine, but when it is docked it wants to have the focus all the time. -If you click on the main app window the focus will blink and go back to the
0
1385
by: criscannon | last post by:
Hi, I need to something like a YM notification window that tells whether a user signs in or out. I am able to use animatewindow to show my form. however, whenever the form is shown, my parent form loses focus. In that scenario of YM, the notify window is animated without stealing focus from the main window. I have tried using ShowWindow noactivate but it doesn't work with animatewindow, in which case, the window is show nonactive but is...
1
1686
by: damonwischik | last post by:
I'm using the Python Image Library (PIL) for Python 2.4. If I have an image and I show it from PIL import Image im = Image.new('RGB',100,100) im.show() then the output window steals focus. It's very handy to use an image to show the progress of execution for my program, but the computer is unusable when focus keeps on being stolen every few seconds. How can I prevent Image.show() from stealing
0
1441
by: sherlockweb | last post by:
Hi there, I am trying to simultaneously run multiple Windows Forms (in this case 3 windows) each with a WebBrowser object. Each of the WebBrowser objects is running a PowerPoint presentation (set to cycle through automatically). The problem I have run into is that when there is more than one window only the last created window allows the PowerPoint presentation to cycle. The other presentations appear to pause - until I click on their
21
9958
by: Sharon | last post by:
I have added an auto scroll feature to my DataGrid control like this: private void DoAutoScroll() { DataView dv = m_DataGrid.DataSource as DataView; DataGridCell cell = m_DataGrid.CurrentCell; cell.RowNumber = dv.Count; m_DataGrid.CurrentCell = cell; } But this the last line of this code is putting the focus on the current
2
3507
by: spamlaanbroek | last post by:
The .NET WebBrowser control in the Compact Framework 2.0 is a major headache. Once it takes the focus (e.g. by the user tapping on it) it keeps it and keyboard events are not passed to the form anymore even though KeyPreview is set to true. If there are no other controls on the form we don't get them at all to start with. I added a tiny textbox with same colors as the form so we can at least
1
1232
by: gary.hibbard | last post by:
Hello, I have a webbrowser control on a pretty simple form (txtAddress, btnGo, WebBrowser1). When going to Google w/ this browser control, the focus changes to Google's search bar. Without going in to a whole lot of detail as to WHY I don't want this to happen, does anybody know if there is a way to prevent focus from
0
8622
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9184
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8892
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7767
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5878
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4382
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3062
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.