2017-07-27 2 views
-1

Fokus wurde vor, Dies hat darum gebeten, und hier sind einige der Fragen, die ich versucht haben, aber die Umsetzung ist fehlgeschlagen:Wie kann man prüfen, welcher Prozess zur Zeit

Determine if current application is activated (has focus)

C#: Detecting which application has focus

How can i get application name which is currently focused

Was ich zur Zeit das funktioniert nicht:

private class User32 
{ 
    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] 
    public static extern IntPtr GetForegroundWindow(); 

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    public static extern int GetWindowThreadProcessId(IntPtr handle, out int processId); 
} 

public static bool ApplicationIsActivated() 
{ 
    try 
    { 
     var activatedHandle = User32.GetForegroundWindow(); 
     if (activatedHandle == IntPtr.Zero) 
     { 
     // No window is currently activated 
     return false; 
     } 

     var procId = Process.GetCurrentProcess().Id; 
     int activeProcId; 
     User32.GetWindowThreadProcessId(activatedHandle, out activeProcId); 

     return activeProcId == procId; 
    } 
    catch(Exception ex) 
    { 
     Console.WriteLine(ex); 
    } 
} 

static void Main(string[] args) 
{ 
    try 
    { 
     while (true) 
     { 
      if (ApplicationIsActivated()) 
       CaptureApplication(); 
      Thread.Sleep(3000); 
     } 
    } 
    catch (Exception ex) 
    { 
     Console.WriteLine(ex); 
    } 
} 

Was ich überprüfen möchte ist, ob der Prozess WINWORD derzeit Fokus hat. Ich habe etwas Code an anderer Stelle in meinem Programm, das tatsächlich Process.GetProcessesByName("WINWORD")[0]; verwendet, aber ich kann nicht herausfinden, wie man überprüft, ob WINWORD den Fokus hat.

Edit: Während des Debugging habe ich festgestellt, dass activeProcID hat die richtige Prozess-ID, aber procID enthält eine Prozess-ID, die ich nicht sehe, wenn ich tasklist /svc in CMD ausführen.

+1

GetCurrentProcess ist Prozess Ihrer Anwendung - also im Grunde der Code überprüft, ob Ihr Prozess das aktive Fenster hat. Haben Sie versucht, "var procId = Process.GetProcessesByName (" WINWORD ") zu verwenden. FirstOrDefault()?. Id;" – ckuri

+0

Danke für die Idee, ich wusste nicht, dass es weitere Erweiterungen auf 'GetProcessById' gibt. Es gibt eine Methode' .ProcessName', die perfekt funktioniert, ich habe die Lösung als Antwort gepostet. –

Antwort

0

Hier ist meine Lösung:

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] 
public static extern IntPtr GetForegroundWindow(); 

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
public static extern int GetWindowThreadProcessId(IntPtr handle, out int processId); 

public static bool ApplicationIsActivated() 
{ 
    var activatedHandle = User32.GetForegroundWindow(); 
    if (activatedHandle == IntPtr.Zero) 
    { 
     // No window is currently activated 
     return false; 
    } 

    var procId = Process.GetCurrentProcess().Id; 
    int activeProcId; 

    User32.GetWindowThreadProcessId(activatedHandle, out activeProcId); 
    string activeProcName = Process.GetProcessById(activeProcId).ProcessName.ToString(); 

    if (activeProcName.Equals("WINWORD")) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

Warum dies funktioniert:

Process.GetProcessById(activeProcId).ProcessName.ToString();

Verwandte Themen