Beispiel #1
0
        /// <summary>
        /// Creates an WindowNode based on <c ref="hWnd" />
        /// </summary>
        /// <param name="hWnd">window handler to base our windownode on</param>
        /// <param name="validation">instruction on what to validate</param>
        /// <returns>null if anything wrong or if window handler deems not possible</returns>
        public WindowNode CreateNode(IntPtr hWnd, ValidateHwndParams validation = null)
        {
            WindowNode node = null;

            if (CanHandleHwnd(hWnd, validation ?? new ValidateHwndParams()))
            {
                if (!pinvokeHandler.GetWindowRect(hWnd, out RECT rect))
                {
                    Log.Warning($"{nameof(WindowNode)}.{nameof(CreateNode)} Could not retrieve rect for hWnd {hWnd}, error: {pinvokeHandler.GetLastError()}");
                    return(null);
                }

                node = windowNodeCreater?.Invoke(rect, hWnd);
            }

            return(node);
        }
Beispiel #2
0
        /// <summary>
        /// Validates if an specific hWnd could probably be handled by WindowNode
        /// </summary>
        /// <param name="hWnd">window handler to validate</param>
        /// <param name="validation">Instruction on what to validate</param>
        /// <returns>true if it probably can</returns>
        public bool CanHandleHwnd(IntPtr hWnd, ValidateHwndParams validation)
        {
            if (validation.DoValidate == false)
            {
                return(true);
            }

            if (hWnd == IntPtr.Zero)
            {
                return(false);
            }

            if (ShouldIgnoreHwnd(hWnd))
            {
                return(false);
            }

            var cb      = new StringBuilder(1024);
            var style   = pinvokeHandler.GetWindowLongPtr(hWnd, PInvoker.GWL_STYLE).ToInt64();
            var exstyle = pinvokeHandler.GetWindowLongPtr(hWnd, PInvoker.GWL_EXSTYLE).ToInt64();

            if (style == 0)
            {
                Log.Warning($"{nameof(WindowNode)}.{nameof(CanHandleHwnd)} Could not retrieve GWL_STYLE for hWnd {hWnd}, error: {pinvokeHandler.GetLastError()}");
                return(false);
            }

            if (validation.ValidateChild && (style & PInvoker.WS_CHILD) == PInvoker.WS_CHILD)
            {
                return(false);
            }

            if (validation.ValidateVisible && (style & PInvoker.WS_VISIBLE) != PInvoker.WS_VISIBLE)
            {
                return(false);
            }

            if (validation.ValidateNoActivate && (exstyle & PInvoker.WS_EX_NOACTIVATE) == PInvoker.WS_EX_NOACTIVATE)
            {
                return(false);
            }

            if (pinvokeHandler.GetClassName(hWnd, cb, cb.Capacity) == 0)
            {
                Log.Warning($"{nameof(WindowNode)}.{nameof(CanHandleHwnd)} Could not retrieve class name for hWnd {hWnd}, error: {pinvokeHandler.GetLastError()}");
                return(false);
            }

            var className = cb.ToString();

            if (classNamesToIgnore.Any(regex => regex.IsMatch(className)))
            {
                return(false);
            }

            if (validation.ValidateApplicationFrame && className == "ApplicationFrameWindow")
            {
                if (IsSpecialAppVisible(hWnd) == false)
                {
                    Log.Warning($"{nameof(WindowNode)}.{nameof(CanHandleHwnd)} Going to ignore {hWnd} \"{GetWindowText(hWnd)}\" [{className}] because it is not visible according to window 10 special thingy");
                    return(false);
                }
            }

            if (validation.ValidateDwm && className == "Windows.UI.Core.CoreWindow")
            {
                var result = 0;
                if ((result = pinvokeHandler.DwmGetWindowAttribute(hWnd, DWMWINDOWATTRIBUTE.Cloaked, out bool pvAttribute, sizeof(int))) != 0 || pvAttribute)
                {
                    return(false);
                }
            }

            var text = GetWindowText(hWnd);

            if (captionsToIgnore.Any(regex => regex.IsMatch(text)))
            {
                return(false);
            }

            return(true);
        }