Check out example codes for "c# get foreground window". It will help you in understanding the concepts better.

Code Example 1

#region Retrieve list of windows

    private static extern int GetWindowLongA(IntPtr hWnd, int index);

    private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

    private static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam);

    private static extern IntPtr GetForegroundWindow();

    private const int GWL_STYLE = -16;

    private const ulong WS_VISIBLE = 0x10000000L;
    private const ulong WS_BORDER = 0x00800000L;
    private const ulong TARGETWINDOW = WS_BORDER | WS_VISIBLE;

    internal class Window
        public string Title;
        public IntPtr Handle;

        public override string ToString()
            return Title;

    private List<Window> windows;

    private void GetWindows()
        windows = new List<Window>();
        EnumWindows(Callback, 0);

    private bool Callback(IntPtr hwnd, int lParam)
        if (this.Handle != hwnd && (GetWindowLongA(hwnd, GWL_STYLE) & TARGETWINDOW) == TARGETWINDOW)
            StringBuilder sb = new StringBuilder(100);
            GetWindowText(hwnd, sb, sb.Capacity);

            Window t = new Window();
            t.Handle = hwnd;
            t.Title = sb.ToString();

        return true; //continue enumeration


Learn ReactJs, React Native from