且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用C#通过其进程ID获取EXE的标题栏文本

更新时间:2021-10-19 01:20:55

这不是那么简单:一个进程可以有多个线程,每个线程可以有多个窗口......

但是,这应该让你开始:

It's not quite that simple: a process can have a number of threads, which can have a number of windows each...
But, this should get you started:
[DllImport("user32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private extern static bool EnumThreadWindows(int threadId, EnumWindowsProc callback, IntPtr lParam);
[DllImport("user32", SetLastError = true, CharSet = CharSet.Auto)]
private extern static int GetWindowText(IntPtr hWnd, StringBuilder text, int maxCount);
private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);


public IEnumerable<string> GetWindowText(Process p)
    {
    List<string> titles = new List<string>();
    foreach (ProcessThread t in p.Threads)
        {
        EnumThreadWindows(t.Id, (hWnd, lParam) =>
        {
            StringBuilder text = new StringBuilder(200);
            GetWindowText(hWnd, text, 200);
            titles.Add(text.ToString());
            return true;
        }, IntPtr.Zero);
        }
    return titles;
    }


http://***.com/questions/2531828/how-to-enumerate-all-windows-belonging-to-a-particular-process -using-net [ ^ ]