Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从C#不工作最小化Microsoft Edge浏览器_C#_.net_Browser_Microsoft Edge - Fatal编程技术网

从C#不工作最小化Microsoft Edge浏览器

从C#不工作最小化Microsoft Edge浏览器,c#,.net,browser,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) { v

我正试图通过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进程的注释。

您可以轻松验证,您正在查看的进程没有主窗口。它的句柄是
0
。因此,你没有最小化任何东西

UWP应用程序与(或可能)普通Win32应用程序有点不同。虽然边缘内容进程具有窗口标题,但也不能使用该窗口句柄最小化边缘。窗口所属的实际进程是
ApplicationFrameHost
。如果有多个主窗口标题,您可能需要对其进行适当过滤。

这应该可以做到(非常不言自明,我提供了一些注释以防万一):


谢谢你,乔伊。如果你知道我该怎么做,请分享你的代码。
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);
        }
    }
}