如何使用C#最小化IE浏览器?

编程入门 行业动态 更新时间:2024-10-23 17:30:59
本文介绍了如何使用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浏览器?

本文发布于:2023-10-11 22:39:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483042.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最小化   如何使用   浏览器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!