C#获取进程窗口标题

4
    proc.MainWindowTitle.Contains("e")

如何获取所有当前打开的包含“e”窗口的窗口标题,而不仅仅是主窗口,使用“MainWindowTitle”并将它们存储到字符串数组中?
     string[] toClose = {proc.MainWindowTitle};
                for (int i = 0; i < toClose.Length; i++)
                {
                    string s = toClose[i];
                    int hwnd = 0;

                    hwnd = FindWindow(null, s);

                    //send WM_CLOSE system message
                    if (hwnd != 0)
                        SendMessage(hwnd, WM_CLOSE, 0, IntPtr.Zero);

请阅读此相关问题(以及已被接受的答案)。https://dev59.com/-Gw05IYBdhLWcg3wZA4C。我认为这会对你有所帮助。 - HuorSwords
如果您想检索该进程拥有的所有窗口,则需要查看EnumChildWindows,并对它们调用SendMessage以使用WM_GETTEXT消息。 - KappaG3
3个回答

5

代码片段

string[] result = new string[50];
int count = 0;
Process[] processes = Process.GetProcesses();
foreach(var process in processes)
{
    if (process.MainWindowTitle
               .IndexOf("e", StringComparison.InvariantCulture) > -1)
    {
        result[count] = process.MainWindowTitle;
        count++;
    }
}

请查看这个问题(以及被接受的答案),它将指导我的回答。 编辑: 如果我理解正确,您想要检索进程主窗口标题的列表。
分析下面的代码,toClose变量始终存储一个标题: proc.MainWindowTitle 的值。
string[] toClose = { proc.MainWindowTitle };

你需要使用原始答案的foreach语句来检索每个窗口标题。然后,在你的代码中,你必须使用建议解决方案中的result变量而不是toClose变量。


已编辑 OP,附上当前的代码,但似乎无法正常工作。 - First Second

5
public static class MyEnumWindows
{
    private delegate bool EnumWindowsProc(IntPtr windowHandle, IntPtr lParam);

    [DllImport("user32")]
    private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);

    [DllImport("user32.dll")]
    private static extern bool EnumChildWindows(IntPtr hWndStart, EnumWindowsProc callback, IntPtr lParam);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam, uint fuFlags, uint uTimeout, out IntPtr lpdwResult);

    private static List<string> windowTitles = new List<string>();

    public static List<string> GetWindowTitles(bool includeChildren)
    {
        EnumWindows(MyEnumWindows.EnumWindowsCallback, includeChildren ? (IntPtr)1 : IntPtr.Zero);
        return MyEnumWindows.windowTitles;
    }

    private static bool EnumWindowsCallback(IntPtr testWindowHandle, IntPtr includeChildren)
    {
        string title = MyEnumWindows.GetWindowTitle(testWindowHandle);
        if (MyEnumWindows.TitleMatches(title))
        {
            MyEnumWindows.windowTitles.Add(title);
        }
        if (includeChildren.Equals(IntPtr.Zero) == false)
        {
            MyEnumWindows.EnumChildWindows(testWindowHandle, MyEnumWindows.EnumWindowsCallback, IntPtr.Zero);
        }
        return true;
    }

    private static string GetWindowTitle(IntPtr windowHandle)
    {
        uint SMTO_ABORTIFHUNG = 0x0002;
        uint WM_GETTEXT = 0xD;
        int MAX_STRING_SIZE = 32768;
        IntPtr result;
        string title = string.Empty;
        IntPtr memoryHandle = Marshal.AllocCoTaskMem(MAX_STRING_SIZE);
        Marshal.Copy(title.ToCharArray(), 0, memoryHandle, title.Length);
        MyEnumWindows.SendMessageTimeout(windowHandle, WM_GETTEXT, (IntPtr)MAX_STRING_SIZE, memoryHandle, SMTO_ABORTIFHUNG, (uint)1000, out result);
        title = Marshal.PtrToStringAuto(memoryHandle);
        Marshal.FreeCoTaskMem(memoryHandle);
        return title;
    }

    private static bool TitleMatches(string title)
    {
        bool match = title.Contains("e");
        return match;
    }

}

3
你需要遍历进程列表(可以使用 Process[] processlist = Process.GetProcesses(); 创建), 使用 foreach( Process in processlist ), 并将 Process.MainWindowTitle 写入数组中。 试试看吧 :)

你是如何将它添加到数组中的? - SlimPDX
编辑了原帖,这就是我的做法,但一次只能获取一个窗口。@MaxMarchuk - First Second
当您通过 foreach 循环迭代过程列表时,您是否将 Process.MainWindowTitle 添加到 array[array.count+1] 中?将其添加到 array[array.count+1] 中将会将其添加到数组的末尾而不是覆盖其中已有的内容。 - SlimPDX
抱歉,这可能听起来很愚蠢,但我该如何每次将1添加到数组中? - First Second
你需要使用以下代码声明一个数组:string[] ProcessArray = new string[*任意大小*];然后,使用以下代码将标题插入该数组中:ProcessArray[ProcessArray.Count+1] = Process.MainWindowTitle;试试看吧 :) - SlimPDX
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接