如何使用C#查找所有窗口?

有Win32函数可以在系统上查找窗口:

[DllImport("user32.dll", SetLastError = true)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] public static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName); 

但是如何在系统上找到所有窗口句柄?

这是我用来查找所有窗口的代码:

以下是调用代码获取窗口信息的三种方法。

 // Returns a list of WindowInformation objects with Handle, Caption, Class List windowListBasic = WindowList.GetAllWindows(); // Returns a list of WindowInformation objects with Handle, Caption, Class, // Parent, Children, Siblings and process information List windowListExtended = WindowList.GetAllWindowsExtendedInfo(); // Returns a WindowInformation objects representing the Desktop window with all // of the information returned above. The objects also contains all of the other // windows in the ChildWindows property. This can be useful for building a tree // view representation of the windows. WindowInformation windowInformationTree = WindowList.GetAllWindowsTree(); 

您可以使用LINQ查询和lambda表达式来定位窗口。

 // Using a lambda expression WindowInformation wi = windowListExtended.Find( w => w.Caption.StartsWith("c# - How") ); // Using a query expression WindowInformation wi = (from w in windowListExtended.AsEnumerable() where w.Caption.StartsWith("c# - How") select w).First(); 

这是代码。

 using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Runtime.InteropServices; using System.Text; namespace Win32 { ///  /// Static class that lists all windows ///  public static class WindowList { #region Call these to return window lists ///  /// Gets all windows with basic information: caption, class and handle. /// The list starts with the desktop. ///  /// WindowInformation list public static List GetAllWindows() { IntPtr desktopWindow = GetDesktopWindow(); List winInfo = new List(); winInfo.Add(winInfoGet(desktopWindow)); List handles = getChildWindows(desktopWindow); foreach (IntPtr handle in handles) { try { winInfo.Add(winInfoGet(handle)); } catch (Exception ex) { } } return winInfo; } ///  /// Gets all windows with extended information: caption, class, handle /// parent, children, and siblings. The list starts with the desktop. ///  /// WindowInformationList public static List GetAllWindowsExtendedInfo() { return winInfoExtendedInfoProcess(GetAllWindowsTree()); } ///  /// Gets all windows in nested objects usefule for adding to a TreeView. /// Includes the extended information: caption, class, handle, parent, /// children, and siblings. The list starts with the desktop. ///  /// Desktop WindowInformation object with nested children public static WindowInformation GetAllWindowsTree() { WindowInformation desktopWindow = winInfoGet(GetDesktopWindow()); desktopWindow.ChildWindows = getChildWindowsInfo(desktopWindow); return desktopWindow; } #endregion #region Unmanaged Code References ///  /// Enumerates the child windows that belong to the specified parent /// window by passing the handle to each child window, in turn, to an /// application-defined callback function. EnumChildWindows continues /// until the last child window is enumerated or the callback function /// returns FALSE. ///  /// A handle to the parent window whose child windows are to be enumerated. If this parameter is NULL, this function is equivalent to EnumWindows. /// A pointer to an application-defined callback function. /// An application-defined value to be passed to the callback function. /// Return FALSE [DllImport("user32")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i); ///  /// Retrieves a handle to a window whose class name and window name match the specified strings. /// The function searches child windows, beginning with the one following the specified child window. /// This function does not perform a case-sensitive search. ///  /// A handle to the parent window whose child windows are to be searched. If hwndParent is NULL, the function uses the desktop window as the parent window. The function searches among windows that are child windows of the desktop. If hwndParent is HWND_MESSAGE, the function searches all message-only windows. /// A handle to a child window. The search begins with the next child window in the Z order. The child window must be a direct child window of hwndParent, not just a descendant window. If hwndChildAfter is NULL, the search begins with the first child window of hwndParent. Note that if both hwndParent and hwndChildAfter are NULL, the function searches all top-level and message-only windows. /// Optional: The class name or a class atom created by a previous call to the RegisterClass or RegisterClassEx function. The atom must be placed in the low-order word of lpszClass; the high-order word must be zero. If lpszClass is a string, it specifies the window class name. The class name can be any name registered with RegisterClass or RegisterClassEx, or any of the predefined control-class names, or it can be MAKEINTATOM(0x8000). In this latter case, 0x8000 is the atom for a menu class. For more information, see the Remarks section of this topic. /// Optional: The window name (the window's title). If this parameter is NULL, all window names match. ///  [DllImport("user32.dll", EntryPoint = "FindWindowEx", CharSet = CharSet.Auto)] private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); ///  /// Retrieves the name of the class to which the specified window belongs. ///  /// A handle to the window and, indirectly, the class to which the window belongs. /// The class name string. /// The length of the lpClassName buffer, in characters. The buffer must be large enough to include the terminating null character; otherwise, the class name string is truncated to nMaxCount-1 characters. /// If the function succeeds, the return value is the number of characters copied to the buffer, not including the terminating null character. If the function fails, the return value is zero. To get extended error information, call GetLastError. [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); ///  /// Retrieves a handle to the desktop window. The desktop window covers the entire screen. The /// desktop window is the area on top of which other windows are painted. ///  /// The return value is a handle to the desktop window [DllImport("user32.dll", SetLastError = false)] private static extern IntPtr GetDesktopWindow(); ///  /// Retrieves a handle to a window that has the specified relationship (Z-Order or owner) to /// the specified window. ///  /// A handle to a window. The window handle retrieved is relative to this window, based on the value of the uCmd parameter. /// The relationship between the specified window and the window whose handle is to be retrieved. This parameter can be one of the following values. /// If the function succeeds, the return value is a window handle. If no window exists with the specified relationship to the specified window, the return value is NULL. To get extended error information, call GetLastError. [DllImport("user32.dll", SetLastError = true)] private static extern IntPtr GetWindow(IntPtr hWnd, GetWindow_Cmd uCmd); ///  /// Copies the text of the specified window's title bar (if it has one) into a buffer. If the /// specified window is a control, the text of the control is copied. However, GetWindowText /// cannot retrieve the text of a control in another application. ///  /// A handle to the window or control containing the text. /// The buffer that will receive the text. If the string is as long or longer than the buffer, the string is truncated and terminated with a null character. /// The maximum number of characters to copy to the buffer, including the null character. If the text exceeds this limit, it is truncated. /// If the function succeeds, the return value is the length, in characters, of the copied string, not including the terminating null character. If the window has no title bar or text, if the title bar is empty, or if the window or control handle is invalid, the return value is zero. To get extended error information, call GetLastError. [DllImport("user32.dll")] private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count); ///  /// Sends the specified message to a window or windows. The SendMessage function calls the /// window procedure for the specified window and does not return until the window procedure /// has processed the message. ///  /// A handle to the window whose window procedure will receive the message. If this parameter is HWND_BROADCAST ((HWND)0xffff), the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows. /// Messages defined in wmConstants enum. See pinvoke.net for more details. /// StringBuilder capacity. /// StringBuilder object. ///  [DllImport("User32.dll")] private static extern int SendMessage(IntPtr hwnd, WMConstants wmConstant, int wParam, StringBuilder sb); ///  /// Sends the specified message to a window or windows. The SendMessage function calls the /// window procedure for the specified window and does not return until the window procedure /// has processed the message. ///  /// A handle to the window whose window procedure will receive the message. If this parameter is HWND_BROADCAST ((HWND)0xffff), the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows. /// Messages defined in wmConstants enum. See pinvoke.net for more details. /// IntPtr.Zero /// IntPtr.Zero ///  [DllImport("User32.dll")] private static extern int SendMessage(IntPtr hwnd, WMConstants wmConstant, IntPtr wParam, IntPtr lParam); #endregion #region Defnitions private delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter); private enum GetWindow_Cmd : uint { GW_HWNDFIRST = 0, GW_HWNDLAST = 1, GW_HWNDNEXT = 2, GW_HWNDPREV = 3, GW_OWNER = 4, GW_CHILD = 5, GW_ENABLEDPOPUP = 6 } private enum WMConstants { WM_GETTEXT = 0x000D, WM_GETTEXTLENGTH = 0x000E } #endregion #region Static Methods ///  /// The delegate functions that is called by EnumChildWindows ///  /// Window handle /// Pointer to the IntPtr list. /// Boolean private static bool enumWindow(IntPtr handle, IntPtr pointer) { GCHandle gch = GCHandle.FromIntPtr(pointer); List list = gch.Target as List; if (list == null) { throw new InvalidCastException("GCHandle Target could not be cast as List"); } list.Add(handle); return true; } ///  /// Called by GetAllWindows. ///  /// The handle of the desktop window. /// List of window handles. private static List getChildWindows(IntPtr parent) { List result = new List(); GCHandle listHandle = GCHandle.Alloc(result); try { EnumWindowProc childProc = new EnumWindowProc(enumWindow); EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle)); } finally { if (listHandle.IsAllocated) listHandle.Free(); } return result; } ///  /// Called by GeAlltWindowsTree and builds the nested WindowInformation /// object with extended window information. Recursive function. ///  /// The parent window. /// List of WindowInformation objects. private static List getChildWindowsInfo(WindowInformation parent) { List result = new List(); IntPtr childHwnd = GetWindow(parent.Handle, GetWindow_Cmd.GW_CHILD); while (childHwnd != IntPtr.Zero) { WindowInformation child = winInfoGet(childHwnd); child.Parent = parent; child.ChildWindows = getChildWindowsInfo(child); result.Add(child); childHwnd = FindWindowEx(parent.Handle, childHwnd, null, null); } foreach (WindowInformation child in result) { child.SiblingWindows.AddRange(result); child.SiblingWindows.Remove(child); } return result; } ///  /// Called by GetAllWindowsExtededInfo. Flattens the nested WindowInformation /// object built by GetAllWindowsTree. ///  /// The nested WindowInformation object created by GetAllWindowsTree. /// Flattened list of WindowInformation objects with extended information. private static List winInfoExtendedInfoProcess(WindowInformation winInfo) { List winInfoList = new List(); winInfoList.Add(winInfo); foreach (WindowInformation child in winInfo.ChildWindows) { winInfoList.AddRange(winInfoExtendedInfoProcess(child)); } return winInfoList; } ///  /// Gets the basic window information from a handle. ///  /// Window handle. /// WindowInformation object with basic information. private static WindowInformation winInfoGet(IntPtr hWnd) { StringBuilder caption = new StringBuilder(1024); StringBuilder className = new StringBuilder(1024); GetWindowText(hWnd, caption, caption.Capacity); GetClassName(hWnd, className, className.Capacity); WindowInformation wi = new WindowInformation(); wi.Handle = hWnd; wi.Class = className.ToString(); if (caption.ToString() != string.Empty) { wi.Caption = caption.ToString(); } else { caption = new StringBuilder(Convert.ToInt32(SendMessage(wi.Handle, WMConstants.WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero)) + 1); SendMessage(wi.Handle, WMConstants.WM_GETTEXT, caption.Capacity, caption); wi.Caption = caption.ToString(); } return wi; } #endregion } ///  /// Object that holds window specific information. ///  public class WindowInformation { #region Constructor ///  /// Initialize the class. ///  public WindowInformation() { } #endregion #region Properties ///  /// The window caption. ///  public string Caption = string.Empty; ///  /// The window class. ///  public string Class = string.Empty; ///  /// Children of the window. ///  public List ChildWindows = new List(); ///  /// Unmanaged code to get the process and thres IDs of the window. ///  ///  ///  ///  [DllImport("user32")] private static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId); ///  /// The string representation of the window. ///  ///  public override string ToString() { return "Window " + this.Handle.ToString() + " \"" + this.Caption + "\" " + this.Class; } ///  /// The handles of the child windows. ///  public List ChildWindowHandles { get { try { var handles = from c in this.ChildWindows.AsEnumerable() select c.Handle; return handles.ToList(); } catch (Exception ex) { return null; } } } ///  /// The window handle. ///  public IntPtr Handle; ///  /// The parent window. ///  public WindowInformation Parent { get; set; } ///  /// The handle of the parent of the window. ///  public IntPtr ParentHandle { get { if (this.Parent != null) return this.Parent.Handle; else return IntPtr.Zero; } } ///  /// The corresponding process. ///  public Process Process { get { try { int processID = 0; GetWindowThreadProcessId(this.Handle, out processID); return Process.GetProcessById(processID); } catch (Exception ex) { return null; } } } ///  /// Sibling window information. ///  public List SiblingWindows = new List(); ///  /// The handles of the sibling windows. ///  public List SiblingWindowHandles { get { try { var handles = from s in this.SiblingWindows.AsEnumerable() select s.Handle; return handles.ToList(); } catch (Exception ex) { return null; } } } ///  /// The thread ID of the window. Returns -1 on exception. ///  public int ThreadID { get { try { int dummy = 0; return GetWindowThreadProcessId(this.Handle, out dummy); } catch (Exception ex) { return -1; } } } #endregion } } 

参考文献:

PInvoke.Net

MSDN Windowsfunction

将EnumWindows与EnumChildWindows一起使用; 有关示例,请参阅底部的文档注释。

我相信这个问题之前已被问过这个问题一百万次了。 但是,您只需要使用EnumWindows来获取顶级窗口,然后使用EnumChildWindows来获取所有孩子。

您可能正在寻找EnumWindowsfunction

您想使用EnumWindows