如何使用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浏览器?
发布评论