开发者

.net - Losing focus after creating an IE object

开发者 https://www.devze.com 2023-01-24 11:26 出处:网络
I have an application which runs in the background and occasionally creates a hidden IE object. Whenever this object is created tho it causes the window I was viewing to lose focus. Is there a way to

I have an application which runs in the background and occasionally creates a hidden IE object. Whenever this object is created tho it causes the window I was viewing to lose focus. Is there a way to make it so whenever it creates an IE object it doesn't cause my current window to lose focus开发者_开发问答?

oIE = CreateObject("InternetExplorer.Application")
oIE.Visible = False


No, this is being controlled by the OS... CreateObject() creates a COM object, which in this case is the full-blown IE app. Behind the scenes, COM is calling into the object you're creating, which is launching a browser window. There are no parameters you can pass to CreateObject(), because this call literally has no control over what the instantiated COM object does.

However, the OS has a setting to disable windows from stealing focus when they are launched... this is what you need.

http://pcsupport.about.com/od/windowsxp/ht/stealingfocus02.htm - link with instructions to disable this.

If you're still using XP, you can download a tool called TweakUI that will let you set this.

HTH,
James


Assuming you're talking about windows foms, simply call Window.Activate after setting the ie object to visible.

And BTW, I would suggest you to import the com dll to your application as a reference, rather than doing it the tough way using CreateObject(), CreateObject() is a recipe for failure and not a good practice if it can be avoided.
You need to add to your project a reference to the SHDocVw.dll, then you can simply declare the InternetExplorer object strongly typed and not hard-coded:

Public Class Form1 : Inherits Form
  Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) _
      Handles MyBase.Load
    Dim ie As New SHDocVw.InternetExplorer
    ie.Navigate("http://google.com")
    ie.Visible = True
    Me.Activate()
  End Sub
End Class


FYI using TweakUI or changing the 'ForegroundLockTimeout' Registry entry does not solve the problem. Every time you run oIE.Navigate IE will steal focus from your active window, even if oIE.Visible = False.

There is no known solution for this at the time of this writing (maybe that's why the op ended up just having to deal with it as he stated).

0

精彩评论

暂无评论...
验证码 换一张
取 消