2017-09-11 1 views
2

J'essaie de minimiser le navigateur Microsoft Edge via C#. Tous les autres navigateurs comme Chrome, Firefox, Internet Explorer fonctionnent bien sauf Microsoft Edge.Réduire le navigateur Microsoft Edge de C# ne fonctionne pas

Quelqu'un peut-il m'aider s'il vous plaît à ce sujet.

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

Vous pouvez essayer de décommenter le processus de Chrome cela fonctionne.

Répondre

0

Comme vous pouvez facilement vérifier, le processus que vous regardez n'a pas de fenêtre principale. C'est poignée est 0. Ainsi, vous ne minimisez rien.

Les applications UWP sont (ou peuvent être) un peu différentes des applications Win32 normales. Alors que les processus de contenu Edge ont un titre de fenêtre, vous ne pouvez pas réduire Edge en utilisant ce handle de fenêtre non plus. Le processus auquel cette fenêtre appartient est ApplicationFrameHost. Vous devrez peut-être filtrer le titre de la fenêtre principale de manière appropriée s'il y en a plusieurs.

+0

Merci @Joey. Si vous avez une idée de comment puis-je faire cela, pouvez-vous s'il vous plaît partager votre code. –

2

Cela devrait faire l'affaire (assez explicite et j'ai fourni des commentaires au cas où):

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