2017-09-12 1 views
1

Ich habe versucht, aktuelle aktive Anwendung Name (oder Prozessname), aber in einigen Anwendungen wie Microsoft Edge ist das Ergebnis ApplicationFrameHost. Gibt es eine Möglichkeit, den Anwendungsnamen wie im Task-Manager zu erhalten?Holen Sie sich aktuelle aktive Anwendung Name in Windows 10 mit C#

Meine eigentliche Code:

[DllImport("user32.dll")] 
    static extern IntPtr GetForegroundWindow(); 
    [DllImport("user32.dll")] 
    static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count); 
    [DllImport("user32.dll")] 
    public static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out uint processId); 

      private string GetActiveProcess() 
    { 

     const int nChars = 256; 
     uint processId; 
     StringBuilder Buff = new StringBuilder(nChars); 
     IntPtr handle = GetForegroundWindow(); 

     if (GetWindowText(handle, Buff, nChars) > 0) 
     { 
      GetWindowThreadProcessId(handle, out processId); 
      return Process.GetProcessById((int)processId).ProcessName; 
     } 
     return null; 
    } 
+8

Dann bitte eine Antwort mit der Lösung erstellen - Sie können es sich auch nicht annehmen können. Setzen Sie nicht "Gelöst" in den Titel - die akzeptierte Antwort wird zeigen, dass es gelöst ist. –

+0

Victor, wenn Sie möchten, können Sie die Antwort kopieren, die ich angegeben habe (klicken Sie auf Bearbeiten, alle auswählen) und fügen Sie sie als Ihre eigene Antwort ein. Ich werde dann meine löschen. Sie können dann Ihre eigene Antwort selbst akzeptieren, und Sie können sogar Upvotes dafür bekommen. – halfer

Antwort

1

Diese Lösung sieht für mich in Form Anwendung funktional:

 //aktivneOknoProces 
     string aktivneOknoProces = GetActiveProcess(); 

private string GetActiveProcess() 
     { 
      string app = ""; 
      var foregroundProcess = Process.GetProcessById(WinAPIFunctions.GetWindowProcessId(WinAPIFunctions.GetforegroundWindow())); 
      if (foregroundProcess.ProcessName == "ApplicationFrameHost") 
      { 
       foregroundProcess = GetRealProcess(foregroundProcess); 
      } 
      if(foregroundProcess != null) 
      { 
       app = foregroundProcess.ProcessName; 
      } 
      return app; 
     } 

     private Process GetRealProcess(Process foregroundProcess) 
     { 
      WinAPIFunctions.EnumChildWindows(foregroundProcess.MainWindowHandle, ChildWindowCallback, IntPtr.Zero); 
      return _realProcess; 
     } 

     private bool ChildWindowCallback(IntPtr hwnd, IntPtr lparam) 
     { 
      var process = Process.GetProcessById(WinAPIFunctions.GetWindowProcessId(hwnd)); 
      if (process.ProcessName != "ApplicationFrameHost") 
      { 
       _realProcess = process; 
      } 
      return true; 
     } 

     public class WinAPIFunctions 
     { 
      //Used to get Handle for Foreground Window 
      [DllImport("user32.dll", CharSet = CharSet.Auto)] 
      private static extern IntPtr GetForegroundWindow(); 

      //Used to get ID of any Window 
      [DllImport("user32.dll", CharSet = CharSet.Auto)] 
      private static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId); 
      public delegate bool WindowEnumProc(IntPtr hwnd, IntPtr lparam); 

      [DllImport("user32.dll")] 
      [return: MarshalAs(UnmanagedType.Bool)] 
      public static extern bool EnumChildWindows(IntPtr hwnd, WindowEnumProc callback, IntPtr lParam); 

      public static int GetWindowProcessId(IntPtr hwnd) 
      { 
       int pid; 
       GetWindowThreadProcessId(hwnd, out pid); 
       return pid; 
      } 

      public static IntPtr GetforegroundWindow() 
      { 
       return GetForegroundWindow(); 
      } 
     } 
0

Ich glaube, für das, was Sie suchen, ist dies:

Process.GetCurrentProcess().ProcessName 

GetCurrentProcess

ProcessName

, dass Sie sollten den Namen des geben laufendes Verfahren

Verwandte Themen