c# Get process window titles

杀马特。学长 韩版系。学妹 提交于 2019-12-02 08:20:46

问题


    proc.MainWindowTitle.Contains("e")

How does one get the window titles of all the current windows containing "e" open instead of just the Main Window using the "MainWindowTitle" and store them into a string array?

EDIT:

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

回答1:


Code snippet

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++;
    }
}

Please review this question (and the accepted answer) that guides my answer.

Edit:

If I understand correctly, you want to retrieve de list of processes main window titles.

Analysing your code below, the toClose variable always store one title: the proc.MainWindowTitle value.

string[] toClose = { proc.MainWindowTitle };

You must retrieve each window title using the foreach statement of my original answer. Then, you must use the result variable in the proposed solution instead of toClose variable on your piece of code.




回答2:


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:


You will need to iterate through the process list (can be made with

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

with foreach( Process in processlist )

and write the Process.MainWindowTitle to the array. Try that :)



来源:https://stackoverflow.com/questions/17887211/c-sharp-get-process-window-titles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!