コード例 #1
0
ファイル: WindowController.cs プロジェクト: namse/overview
        public ActionResult <IEnumerable <WindowInfo> > GetWindowInfos()
        {
            var windowInfos = new List <WindowInfo>();

            WindowsFunctions.EnumWindows(((windowHandle, param) =>
            {
                if (!IsAltTabWindow(windowHandle))
                {
                    return(true);
                }

                var stringBuilder = new StringBuilder(1024);
                WindowsFunctions.GetWindowText(windowHandle, stringBuilder, stringBuilder.MaxCapacity);

                if (stringBuilder.Length == 0)
                {
                    return(true);
                }

                WindowsFunctions.GetWindowThreadProcessId(windowHandle, out var processId);
                var process = Process.GetProcessById((int)processId);

                try
                {
                    windowInfos.Add(new WindowInfo
                    {
                        WindowHandle = windowHandle.ToInt32(),
                        WindowTitle = stringBuilder.ToString(),
                        ProcessFileLocation = process.MainModule?.FileName,
                    });
                }
                catch
                {
                    Console.WriteLine($"Cannot get window info - processId: {processId}, windowHandle: {windowHandle}");
                }


                return(true);
            }), IntPtr.Zero);

            return(windowInfos);
        }