计算机屏幕上显示的窗口数量?

时间:2016-03-20 09:51:05

标签: c# winforms

我想计算C#中计算机屏幕上显示的窗口数量。谁能帮我? 谢谢!

1 个答案:

答案 0 :(得分:1)

您可以尝试这种方式。使用Win32

namespace TestConsole
{
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using System;
    using System.Text;


    public class CountWindows
    {

        public delegate bool EnumDelegate(IntPtr hWnd, int lParam);


        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool IsWindowVisible(IntPtr hWnd);


        [DllImport("user32.dll", EntryPoint = "GetWindowText",
        ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
        public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount);


        [DllImport("user32.dll", EntryPoint = "EnumDesktopWindows",
        ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction, IntPtr lParam);


        static void Main()
        {
            var collection = new List<string>();
            CountWindows.EnumDelegate filter = delegate (IntPtr hWnd, int lParam)
            {
                StringBuilder strbTitle = new StringBuilder(255);
                int nLength = CountWindows.GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1);
                string strTitle = strbTitle.ToString();

                if (CountWindows.IsWindowVisible(hWnd) && string.IsNullOrEmpty(strTitle) == false)
                {
                    collection.Add(strTitle);
                }
                return true;
            };

            if (CountWindows.EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero))
            {
                foreach (var item in collection)
                {
                    Console.WriteLine(item);
                }
                Console.WriteLine("Count : "+ collection.Count);
            }
            Console.Read();
        }
    }


}