Opening a Hidden Internet Explorer Window without it getting Focus?

依然范特西╮ 提交于 2019-11-28 04:14:07

问题


I want to open a hidden Internet Explorer window without it stealing focus. I have a Timer object that opens Internet Explorer every 5 minutes to check a website for updates. The problem is every time it checks for updates, it steals focus from the current application in the foreground. Below is how I start the process:

        Process m_Proc = new Process();
        m_Proc.StartInfo.Arguments = String.Format("{0}{1}", "-nomerge ", browserURL);
        m_Proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        m_Proc.StartInfo.UseShellExecute = true;
        m_Proc.StartInfo.CreateNoWindow = true;
        m_Proc.StartInfo.FileName = String.Format("iexplore.exe");
        m_Proc.Start();

It always steals focus, even when it is hidden. I want it to start like nothing is happening, so users can continue to work on what they are doing. Thanks.


回答1:


Try automating Internet Explorer via its COM interfaces rather than creating a process explicitly. Here's how it can be done. Just don't do ie.Visible = true and it will stay hidden. Call ie.Quit() when done with it.




回答2:


I'm not even sure why you would open a browser to do this, would it not be easier to have your code just check the website another way? Perhaps something like this.

        string url = "http://www.google.com";
        string result = null;
        try
        {
            WebClient client = new WebClient();
            result = client.DownloadString(url);
            //Store this result and compare it to last stored result for changes.         
        }
        catch (Exception ex)
        {
            // handle error
            MessageBox.Show(ex.Message);
        }

My C# is rusty so you'll need to double check that.



来源:https://stackoverflow.com/questions/19133531/opening-a-hidden-internet-explorer-window-without-it-getting-focus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!