2013-07-26 76 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; 
    } 

}