Minimize Microsoft Edge browser from C# not working

血红的双手。 提交于 2020-01-24 12:55:12

问题


I am trying to minimize the Microsoft Edge browser through C#. All the other browsers like Chrome, Firefox, Internet Explorer are working fine except Microsoft Edge.

Can anybody please help me on this.

Here is my code.

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

You can try uncommenting Chrome process it is working.


回答1:


This should do the trick (pretty self-explanatory and I've provided comments just in case):

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



回答2:


As you can easily verify, the process you're looking at has no main window. It's handle is 0. Thus you're not minimising anything.

UWP apps are (or can be) a bit different from normal Win32 applications. While the Edge content processes have a window title, you cannot minimise Edge by using that window handle either. The actual process that window belongs to is ApplicationFrameHost. You may need to filter the main window title appropriately if there are multiple of them.



来源:https://stackoverflow.com/questions/46150661/minimize-microsoft-edge-browser-from-c-sharp-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!