2013-07-26 78 views
1
proc.MainWindowTitle.Contains("e") 

如何使用“MainWindowTitle”获取所有包含“e”的当前窗口的窗口标题而不是主窗口并将它们存储到字符串数组中?c#获取进程窗口标题

编辑:

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

请阅读这个相关的问题(和接受的答案)。 http://stackoverflow.com/questions/7268302/get-the-names-of-all-open-windows-not-process-name。我认为这会帮助你。 – HuorSwords

+0

如果您的意思是检索该进程拥有的所有窗口,则需要查看[EnumChildWindows](http://msdn.microsoft.com/en-us/library/windows/desktop/ms633494(v = vs.85) ).aspx),并使用WM_GETTEXT Msg对它们调用[SendMessage](http://msdn.microsoft.com/en-us/library/windows/desktop/ms644950(v = vs.85).aspx)。 – KappaG3

回答

2

您将需要通过进程列表(可以用

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

迭代与foreach(Process in processlist)

和写入Process.MainWindowTitle到阵列。 试一下:)

+0

试过这个,但它一次只返回一个而不是全部 –

+0

你如何将它添加到数组? – MaxMarchuk

+0

已编辑的OP,即我如何做,但它一次只获得一个窗口@MaxMarchuk –

2

代码片断

​​

请仔细阅读本question(和接受的答案),指导我的答案。

编辑:

如果我理解正确的,你想要检索的过程中主窗口标题德名单。

分析下面的代码,toClose变量总是存储一个标题:proc.MainWindowTitle值。

string[] toClose = { proc.MainWindowTitle }; 

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

+0

编辑OP与当前代码似乎并不似乎正在工作 –

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

}