如何通过一个进程的多个窗口枚举c#

如何通过一个进程的多个窗口枚举c#,c#,process,C#,Process,我复制了以下代码: 但我是C#的新手,我不太懂代码。如何过滤,例如,仅打印Word窗口?(word的进程名为WINWORD)检查“getwindowtext”。。它应该包含单词!使用www.pinvoke.net。您可以从句柄获取进程名称。见: using System.Runtime.InteropServices; protected delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam); [DllImpo

我复制了以下代码:


但我是C#的新手,我不太懂代码。如何过滤,例如,仅打印Word窗口?(word的进程名为WINWORD)

检查“getwindowtext”。。它应该包含单词!使用www.pinvoke.net。您可以从句柄获取进程名称。见:
using System.Runtime.InteropServices;
protected delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam); 

        [DllImport("user32.dll", CharSet = CharSet.Unicode)] 
        protected static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount); 

        [DllImport("user32.dll", CharSet = CharSet.Unicode)] 
        protected static extern int GetWindowTextLength(IntPtr hWnd); 

        [DllImport("user32.dll")] 
        protected static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam); 

        [DllImport("user32.dll")] 
        protected static extern bool IsWindowVisible(IntPtr hWnd); 

        protected static bool EnumTheWindows(IntPtr hWnd, IntPtr lParam) 
        { 
            int size = GetWindowTextLength(hWnd); 
            if (size++ > 0 && IsWindowVisible(hWnd)) 
            { 
                StringBuilder sb = new StringBuilder(size); 
                GetWindowText(hWnd, sb, size); 
                Console.WriteLine(sb.ToString()); 
            } 
            return true; 
        }

        static void Main(string[] args) 
        { 
            EnumWindows(new EnumWindowsProc(EnumTheWindows), IntPtr.Zero);
        }