Ejemplo n.º 1
0
        public static bool TryGetThirdPartyForgroundWindow(out IntPtr hwnd)
        {
            hwnd = IntPtr.Zero;
            var foregroundWindow = WinApi.GetForegroundWindow();
            var currentThreadId  = WinApi.GetCurrentThreadId();
            var foregroundThread = WinApi.GetWindowThreadProcessId(foregroundWindow, out var _);

            if (currentThreadId == foregroundThread)
            {
                return(false);
            }
            else
            {
                var hwndRoot = WinApi.GetAncestor(foregroundWindow, WinApi.GA_ROOT);
                if (hwndRoot != IntPtr.Zero)
                {
                    foregroundWindow = hwndRoot;
                }

                var className = new StringBuilder(capacity: 256);
                WinApi.GetClassName(foregroundWindow, className, className.Capacity - 1);
                if (className.ToString().CompareTo("Shell_TrayWnd") == 0)
                {
                    return(false);
                }

                hwnd = foregroundWindow;
                return(true);
            }
        }
Ejemplo n.º 2
0
        public static IntPtr GetPrevActiveWindow()
        {
            var foregroundWindow = WinApi.GetAncestor(WinApi.GetForegroundWindow(), WinApi.GA_ROOT);

            var hwndPrev  = IntPtr.Zero;
            var hwndFound = IntPtr.Zero;

            bool enumWindowsProc(IntPtr hwnd, IntPtr lparam)
            {
                if (WinApi.IsWindowVisible(hwnd))
                {
                    if (hwndPrev == foregroundWindow)
                    {
                        hwndFound = hwnd;
                        return(false);
                    }
                    hwndPrev = hwnd;
                }
                return(true);
            }

            WinApi.EnumDesktopWindows(IntPtr.Zero, enumWindowsProc, IntPtr.Zero);
            return(hwndFound);
        }