C# :: Aufgabe #45 :: Lösung #4

5 Lösungen Lösungen öffentlich
#45

Fensternamen ausgeben

Fortgeschrittener - C# von Process1 - 15.01.2013 um 06:51 Uhr
Erstelle ein Konsolenprogramm. In der Konsole sollen Fenstertitel & und zugehöriger
Prozessname ausgegeben werden, wenn das Vordergrundfenster wechselt. (Programm soll durchgehend laufen)

Die Konsole selber soll nicht ausgegeben werden.
Der Windows-Explorer soll nicht ausgegeben werden.

Konsolenausgabe:

Fenstername:  Windows Task-Manager
Prozessname: taskmgr

Fenstername: Neue Aufgabe erstellen - TRAIN your programmer - Google Chrome
Prozessname: chrome

usw.


#4
vote_ok
von kjaenke (1140 Punkte) - 30.10.2017 um 10:33 Uhr
Quellcode ausblenden C#-Code
namespace Exercise_45
{
    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;

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

        [DllImport("user32.dll", SetLastError = true)]
        private static extern uint GetWindowThreadProcessId(IntPtr frgWnd, out uint frgWndProcessId);

        private static void Main()
        {
            var processTitel = "";

            while (true)
            {
                var currentProcess = Process.GetCurrentProcess();
                var fw = GetForegroundWindow();

                GetWindowThreadProcessId(fw, out var processId);

                var actualProcess = Process.GetProcessById(Convert.ToInt32(processId)).MainWindowTitle;

                var processName = Process.GetProcessById(Convert.ToInt32(processId)).ProcessName;

                if (processId == currentProcess.Id || processName == "explorer" || actualProcess == processTitel || actualProcess == "")
                {
                    continue;
                }
                processTitel = actualProcess;

                
                Console.WriteLine("Fenstername: " + processTitel);
                Console.WriteLine("Prozessname: " + processName + "\n");
            }
        }
    }
}

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben