3
    proc.MainWindowTitle.Contains("e")

如何使用“MainWindowTitle”打开包含“e”的所有当前窗口的窗口标题,而不是仅打开主窗口,并将它们存储到字符串数组中?

编辑:

     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);
4

3 回答 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在您的代码段中使用变量。

于 2013-07-26T17:24:20.883 回答
4
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;
    }

}
于 2013-07-26T20:20:05.550 回答
3

您将需要遍历流程列表(可以使用

Process[] processlist = Process.GetProcesses();)

foreach( Process in processlist )

并将 写入Process.MainWindowTitle数组。试试看:)

于 2013-07-26T17:16:46.730 回答