コード例 #1
0
        private static bool EnumChildWindowsFunc(IntPtr hWnd, IntPtr lParam)
        {
            var info = (WindowInfo)Marshal.PtrToStructure(lParam, typeof(WindowInfo));

            LowLevel.GetWindowThreadProcessId(hWnd, out var processId);

            if (info != null && processId != info.OwnerPid)
            {
                info.ChildPid = processId;
            }

            Marshal.StructureToPtr(info, lParam, true);
            return(true);
        }
コード例 #2
0
        public OpenedWindow GetActive()
        {
            var activeHwnd = LowLevel.GetForegroundWindow();
            var buffer     = new StringBuilder(255);

            LowLevel.GetWindowText(activeHwnd, buffer, buffer.Capacity + 1);

            var title = buffer.ToString();

            return(new OpenedWindow
            {
                Hwnd = activeHwnd,
                Title = title == string.Empty ? "EMPTY" : title
            });
        }
コード例 #3
0
        public IEnumerable <OpenedWindow> GetOpenedWindows()
        {
            var openedWindows = new List <OpenedWindow>();

            var filter = new EnumerateFunc((hwnd, param) =>
            {
                var buffer = new StringBuilder(255);
                LowLevel.GetWindowText(hwnd, buffer, buffer.Capacity + 1);

                var title = buffer.ToString();

                if (!LowLevel.IsWindowVisible(hwnd) || string.IsNullOrEmpty(title) || hwnd == (IntPtr)0x0)
                {
                    return(true);
                }

                LowLevel.GetWindowThreadProcessId(hwnd, out var processId);
                var executablePath = LowLevelHelper.GetUwpApplicationName(hwnd, processId);

                if (executablePath == null)
                {
                    return(true);
                }

                if (_excludedProcesses.Any(x => executablePath.Contains(x) || title.Contains(x)))
                {
                    return(true);
                }

                openedWindows.Add(new OpenedWindow
                {
                    Hwnd           = hwnd,
                    Title          = title,
                    ExecutablePath = executablePath,
                    Uid            = _hashProvider.Create(hwnd.ToString())
                });

                return(true);
            });

            LowLevel.EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero);
            return(openedWindows);
        }
コード例 #4
0
        public static string GetUwpApplicationName(IntPtr hWnd, uint processId)
        {
            var windowInfo = new WindowInfo
            {
                OwnerPid = processId,
                ChildPid = processId
            };

            var pWindowInfo = Marshal.AllocHGlobal(Marshal.SizeOf(windowInfo));

            Marshal.StructureToPtr(windowInfo, pWindowInfo, false);
            LowLevel.EnumChildWindows(hWnd, EnumChildWindowsFunc, pWindowInfo);

            windowInfo = (WindowInfo)Marshal.PtrToStructure(pWindowInfo, typeof(WindowInfo));

            var proc = IntPtr.Zero;

            if (windowInfo != null)
            {
                proc = LowLevel.OpenProcess((uint)(ProcessAccess.QueryInformation | ProcessAccess.VirtualMemoryRead),
                                            false, (int)windowInfo.ChildPid);

                if (proc == IntPtr.Zero)
                {
                    return(null);
                }
            }

            var capacity = 2048;
            var buffer   = new StringBuilder(capacity);

            LowLevel.QueryFullProcessImageName(proc, 0, buffer, ref capacity);

            Marshal.FreeHGlobal(pWindowInfo);

            return(buffer.ToString(0, capacity));
        }
コード例 #5
0
 public void SetTopMode(OpenedWindow window, WindowTopMode mode)
 {
     LowLevel.SetWindowPos(window.Hwnd, (IntPtr)mode, 0, 0, 0, 0,
                           (uint)(WindowPositionParameters.NoMove | WindowPositionParameters.NoSize));
 }