How to minimize IE Browser using C# ?

只谈情不闲聊 提交于 2019-12-02 05:24:59

问题


How can I minimize the IE Browser using C#? I tried the code mentioned below which didn't work:

var processes = Process.GetProcessesByName("*iexplorer.*");

if (processes.Any()) 
{

    var handle = processes.First().MainWindowHandle;
    ShowWindow(handle, SW_SHOWMINIMIZED); 

}

Are there any other methods to achieve minimizing of the IE Browser?


回答1:


As Damien says, there is no fullproof way to do this as the user owns the browser, not your app. Your code isn't working because you are trying to use a wildcard symbol (*) like you would do on Google, but this doesn't work here. GetProcessesByName is literally looking for a process named *iexplorer.*. You can confirm this by placing a breakpoint underneath this line, and hovering over processList, it is an empty array. Changing this to iexplore fixes this problem.

Some tested and working code is below:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        static void Main()
        {
            var processes = Process.GetProcessesByName("iexplore");

            foreach (var process in processes)
            {
                ShowWindow(process.MainWindowHandle, 2);
            }
        }
    }
}


来源:https://stackoverflow.com/questions/12473343/how-to-minimize-ie-browser-using-c-sharp

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