如何使用C#最小化IE浏览器? [英] How to minimize IE Browser using C# ?

查看:190
本文介绍了如何使用C#最小化IE浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用C#最小化IE浏览器?我尝试了下面提到的无效代码:

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); 

}

是否有其他方法可以最小化IE浏览器?

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

推荐答案

正如Damien所说,没有完全可靠的方法来执行此操作,因为用户拥有浏览器,而不是您的应用。您的代码无效,因为您正尝试像在Google上一样使用通配符(*),但这在这里不起作用。 GetProcessesByName 实际上是在寻找名为 * iexplorer。* 的进程。您可以通过在此行下放置一个断点并悬停在 processList 上来确认这一点,它是一个空数组。将其更改为 iexplore 可解决此问题。

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);
            }
        }
    }
}

这篇关于如何使用C#最小化IE浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆