c# - 从 C# 最小化 Microsoft Edge 浏览器不起作用

标签 c# .net browser microsoft-edge

我正在尝试通过 C# 最小化 Microsoft Edge 浏览器。除 Microsoft Edge 外,所有其他浏览器(如 Chrome、Firefox、Internet Explorer)都运行良好。

谁能帮我解决这个问题。

这是我的代码。

   [DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    static void Main(string[] args)
    {

        var processes = Process.GetProcessesByName("MicrosoftEdge");
        //var processes = Process.GetProcessesByName("chrome");

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

您可以尝试取消注释正在运行的 Chrome 进程。

最佳答案

这应该可以解决问题(不言自明,我提供了评论以防万一):

using System;
using System.Runtime.InteropServices;
using System.Text;

namespace EdgeApp
{
    class Program
    {
        [DllImport("user32.dll")]
        private static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);

        [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        private static extern int GetWindowTextLength(IntPtr hWnd);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

        [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

        public const int SW_HIDE = 0;
        public const int SW_SHOWNORMAL = 1;
        public const int SW_SHOWMINIMIZED = 2;
        public const int SW_SHOWMAXIMIZED = 3;

        public static void Main(string[] args)
        {
            // Enumerate over windows.
            EnumWindows((handle, param) =>
            {
                // Get the class name. We are looking for ApplicationFrameWindow.
                var className = new StringBuilder(256);
                GetClassName(handle, className, className.Capacity);

                // Get the window text. We're looking for Microsoft Edge.
                int windowTextSize = GetWindowTextLength(handle);
                var windowText = new StringBuilder(windowTextSize + 1);
                GetWindowText(handle, windowText, windowText.Capacity);

                // Check if we have a match. If we do, minimize that window.
                if (className.ToString().Contains("ApplicationFrameWindow") && 
                    windowText.ToString().Contains("Microsoft Edge"))
                {
                    ShowWindow(handle, SW_SHOWMINIMIZED);
                }

                // Return true so that we continue enumerating,
                // in case there are multiple instances.
                return true;
            }, IntPtr.Zero);
        }
    }
}

关于c# - 从 C# 最小化 Microsoft Edge 浏览器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46150661/

相关文章:

c# - 如果安装了 IE9,WPF 4.0 WebBrowser Control 是否支持 HTML5?

c# - MySqlContext如何将查询写入变量

c# - Poloniex C# API - 尝试提取 ETH 时出错

C# 集合?

c# - 服务器不接受在 HttpClientHandler 上设置的证书

javascript - jQuery weekcalendar 在 Firefox 中正确显示,但在 Chrome/Safari 中无法正确显示(仅在 iOS/OSX 设备上)

c# - 将 LinkedList 附加到另一个 LinkedList 的末尾?

c# - 如何确定控制台应用程序是否从控制台窗口内启动?

javascript - 使用ckeditor从用户本地路径上传文件?

html - 泰卢固语文本在网页中显示为方框。如何显示原文?