Focus Problem!!
-
Hi, I have a TIMER and a WebBrowser component. When the program goes to the System Tray and Timer Refreshs the WebBrowser Component ( i cant see the program form, normal), i loose the focus of my active window :( How can i fix this?
The WebBrowser control - an ActiveX control that is hosted through an RCW, or Runtime-Callable Wrapper - is subject to its own focus handling. Either the
AxHost
(the base class for theAxWebBrowser
class) has the focus or it doesn't, but how the WebBrowser control itself maintains focus is completely independent of .NET. IT could, for example, have the focus while theAxHost
doesn't even realize it has the focus. This is a common problem (not just with the WebBrowser control, but with all ActiveX controls hosted in .NET). Once way of solving the problem is to get the handle of the currently active window before you refresh the WebBrowser and then restore it. For this, you'll need to P/Invoke the nativeGetForegroundWindow
andSetForegroundWindow
APIs. If you need help declaring the P/Invoke signatures, see http://pinvoke.net[^]. To solve the problem with the WebBrowser control itself, you either need to correctly identify the container (this gets deep into COM interfaces, so experience in this area is helpful; there are some articles on this site that may help if you search for "WebBrowser", for example) or to make sure that the WebBrowser control never takes the focus to begin with. One way - and this is untested but should work - is to set theWebBrowser.Visible
property (not the inherittedAxHost.Visible
property!) tofalse
. IF correctly implemented, the control should not try to steal the focus if it is not even visible. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]