本文介绍了c# 获取进程窗口标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    proc.MainWindowTitle.Contains("e")

如何使用MainWindowTitle"获取所有当前包含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?

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

推荐答案

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

}

这篇关于c# 获取进程窗口标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 15:56
查看更多