/**/ /// <summary> /// Find the child window, if found m_classname will be assigned /// </summary> /// <param name="hwndParent">parent's handle</param> /// <param name="lParam">the application value, nonuse</param> /// <returns>found or not found</returns> //The C++ code is that lParam is the instance of FindWindow class , if found assign the instance's m_hWnd private bool FindChildClassHwnd(IntPtr hwndParent, IntPtr lParam) { EnumWindowProc childProc = new EnumWindowProc(FindChildClassHwnd); IntPtr hwnd = FindWindowEx(hwndParent, IntPtr.Zero, this.m_classname, string.Empty); if (hwnd != IntPtr.Zero) { this.m_hWnd = hwnd; // found: save it return false; // stop enumerating } EnumChildWindows(hwndParent, childProc, IntPtr.Zero); // recurse redo FindChildClassHwnd return true;// keep looking }
private bool FindChildClassHwnd(IntPtr hwndParent, IntPtr lParam) { EnumWindowProc callback = new EnumWindowProc(this.FindChildClassHwnd); IntPtr ptr = FindWindowEx(hwndParent, IntPtr.Zero, this.m_classname, string.Empty); if (ptr != IntPtr.Zero) { this.m_hWnd = ptr; callback = null; return false; } EnumChildWindows(hwndParent, callback, IntPtr.Zero); callback = null; return true; }
/// <summary> /// Chrome's message-loop Window isn't created synchronously, so this may not find it. /// If so, you need to wait and try again later. /// </summary> public static bool TryFindHandle(IntPtr browserHandle, out IntPtr chromeWidgetHostHandle) { var classDetails = new ClassDetails(); var gcHandle = GCHandle.Alloc(classDetails); var childProc = new EnumWindowProc(EnumWindow); EnumChildWindows(browserHandle, childProc, GCHandle.ToIntPtr(gcHandle)); chromeWidgetHostHandle = classDetails.DescendantFound; gcHandle.Free(); return classDetails.DescendantFound != IntPtr.Zero; }
/// <summary> /// Returns a list of child windows /// </summary> /// <param name="parent">Parent of the windows to return</param> /// <returns>List of child windows</returns> public static List<IntPtr> GetChildWindows(IntPtr parent) { List<IntPtr> result = new List<IntPtr>(); 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; }
// Bir List<IntPtr> listesi yarat ve istenilen uygulamaya ait ana window altındaki tüm çocuk // window'ların handle kimliklerini listeye doldur. public List<IntPtr> GetAllChildHandles() { List<IntPtr> childHandles = new List<IntPtr>(); GCHandle gcChildhandlesList = GCHandle.Alloc(childHandles); IntPtr pointerChildHandlesList = GCHandle.ToIntPtr(gcChildhandlesList); try { EnumWindowProc childProc = new EnumWindowProc(EnumWindow); EnumChildWindows(this.m_MainHandle, childProc, pointerChildHandlesList); } finally { gcChildhandlesList.Free(); } return childHandles; }
public void Test() { // ���TextWriter�йܶ��ᱻ���ݸ�Win32 API���� // �������һֱ���ڣ�ֱ�����ý��� TextWriter outputWriter = System.Console.Out; // ʹ��GCHandle����ֹTextWriter������������������ GCHandle gch = GCHandle.Alloc(outputWriter, GCHandleType.Normal); EnumWindowProc cb = new EnumWindowProc(DisplayWindowsTitleProc); // P/Invoke���ڵ��ý���ǰ���Զ���ֹ���� // ��������ί�н��л��� EnumDesktopWindows(IntPtr.Zero, cb, (IntPtr)gch); // ��������ҪGCHandleʱ���������Free���������ͷŵ� gch.Free(); }
/**/ /// <summary> /// Find the child window, if found m_classname will be assigned /// </summary> /// <param name="hwndParent">parent's handle</param> /// <param name="lParam">the application value, nonuse</param> /// <returns>found or not found</returns> //The C++ code is that lParam is the instance of FindWindow class , if found assign the instance's m_hWnd private bool FindChildClassHwnd(IntPtr hwndParent, IntPtr lParam) { EnumWindowProc childProc = new EnumWindowProc(FindChildClassHwnd); IntPtr hwnd = FindWindowEx(hwndParent, IntPtr.Zero, m_classname, m_caption); if (hwnd != IntPtr.Zero) { this.m_hWnd = hwnd; // found: save it m_IsTimeOut = false; return false; // stop enumerating } DateTime end = DateTime.Now; if (start.AddSeconds(m_timeout) < end) { m_IsTimeOut = true; return false; } EnumChildWindows(hwndParent, childProc, IntPtr.Zero); // recurse redo FindChildClassHwnd return true;// keep looking }
public static extern bool EnumThreadWindows(int dwThreadId, EnumWindowProc lpfn, IntPtr lParam);
public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr lParam);
static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowProc lpEnumFunc, IntPtr lParam);
// 子ハンドルのリスト更新 public void Update() { hWndList.Clear(); this.hWnd = GetWindowHandle(windowName); if (hWnd.Equals((IntPtr)0)) { this.hWnd = GetWindowHandle(windowName+"*"); if (hWnd.Equals((IntPtr)0)) { string str = "\"" + windowName + "\"は起動していません"; throw new ApplicationException(str); } this.windowName = windowName + "*"; } GCHandle listHandle = GCHandle.Alloc(hWndList); try { EnumWindowProc childProc = new EnumWindowProc(EnumWindow); EnumChildWindows(hWnd, childProc, GCHandle.ToIntPtr(listHandle)); } finally { if (listHandle.IsAllocated) { listHandle.Free(); } } }
/// <summary> /// Returns a list of child windows /// </summary> /// <param name="parent">Parent of the windows to return</param> /// <returns>List of child windows</returns> public static List<IntPtr> GetChildWindows(IntPtr parent, bool onlyDirect = false) { List<IntPtr> result = new List<IntPtr>(); GCHandle listHandle = GCHandle.Alloc(result); try { EnumWindowProc childProc = new EnumWindowProc(EnumWindow); EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle)); } finally { if (listHandle.IsAllocated) { listHandle.Free(); } } if (onlyDirect == true) { foreach (var h in result.ToArray()) { if (GetParent(h) != parent) { result.Remove(h); } } } return result; }
public static extern bool EnumChildWindows(IntPtr hWndParent, EnumWindowProc lpEnumFunc, IntPtr lParam);
public static extern bool EnumChildWindows(IntPtr hWnd, EnumWindowProc callback, IntPtr lParam);
public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, /*IntPtr i*/ int lParam);
/// <summary> /// 遍历目标句柄的子句柄 /// </summary> /// <param name="hWnd">句柄</param> /// <param name="lpfn">遍历方法,参数需和EnumWindowProc一致</param> /// <param name="lParam">为0即可</param> /// <returns></returns> [DllImport("user32.dll")] public static extern int EnumChildWindows(IntPtr hWnd, EnumWindowProc lpfn, int lParam); public delegate bool EnumWindowProc(int hwnd, int lParam);
public static extern bool EnumWindows(EnumWindowProc enumProc, IntPtr lParam);
private static extern bool EnumThreadWindows(uint threadId, EnumWindowProc enumProc, IntPtr lParam);
internal static extern bool EnumWindows(EnumWindowProc lpEnumFunc, IntPtr lParam);
public static int EnumWindows(EnumWindowProc lpEnumFunc, IntPtr lParam) { return -1; }
internal static extern bool EnumWindows(EnumWindowProc callback, IntPtr lParam);
public static extern bool EnumThreadWindows(int threadId, EnumWindowProc callback, IntPtr lParam);
public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);
public void Init() { EnumWindowProc callback = new EnumWindowProc(EnumProc); EnumWindows(callback, IntPtr.Zero); }
private static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr lParam);
static extern bool EnumDesktopWindows( IntPtr desktopHandle, EnumWindowProc cb, IntPtr param);
public static bool EnumDesktopWindows(EnumWindowProc callback) { return NativeMethods.EnumDesktopWindows(IntPtr.Zero, callback, IntPtr.Zero); }
private List<IntPtr> GetAllChildHandles() { if (null == LastStepResult) LastStepResult = new List<IntPtr>(); else LastStepResult.Clear(); LastStepResult.Add(_rootHandle); foreach (SearchCriteria search in SearchOrder) { Current = search; List<IntPtr> childHandles = new List<IntPtr>(); GCHandle gcChildhandlesList = GCHandle.Alloc(childHandles); IntPtr pointerChildHandlesList = GCHandle.ToIntPtr(gcChildhandlesList); foreach (IntPtr handle in LastStepResult) { EnumWindowProc childProc = new EnumWindowProc(EnumWindow); EnumChildWindows(handle, childProc, pointerChildHandlesList); } if (childHandles.Count == 0) break; LastStepResult.Clear(); foreach (IntPtr item in childHandles) LastStepResult.Add(item); childHandles.Clear(); gcChildhandlesList.Free(); } Result.Clear(); foreach (IntPtr item in LastStepResult) Result.Add(item); return Result; }
public static void EnumWindows(EnumWindowProc callback) { NativeMethods.EnumWindows(callback, 0); }
public static extern int EnumChildWindows(IntPtr hWndParent, EnumWindowProc Callback, IntPtr lpParam);
public static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumWindowProc lpfn, IntPtr lParam);
public static extern bool EnumChildWindows(IntPtr hWnd, EnumWindowProc callback, IntPtr userObject);
public static extern int EnumWindows(EnumWindowProc x, int y);
/// <summary> /// </summary> /// <param name = "parent"></param> /// <returns></returns> private static IEnumerable<IntPtr> GetChildWindows( IntPtr parent ) { var windowPointers = new List<IntPtr>(); GCHandle listHandle = GCHandle.Alloc( windowPointers ); try { var childProc = new EnumWindowProc( EnumWindow ); EnumChildWindows( parent, childProc, GCHandle.ToIntPtr( listHandle ) ); } finally { if ( listHandle.IsAllocated ) { listHandle.Free(); } } return windowPointers; }
public static extern int EnumWindows(EnumWindowProc callPtr, int lPar);
public static extern int EnumWindows(EnumWindowProc lpEnumFunc, IntPtr lParam);
public static bool EnumChildWindows(IntPtr hWndParent, EnumWindowProc lpEnumFunc, IntPtr lParam) { return false; }
public static extern int EnumWindows(EnumWindowProc hWnd, IntPtr lParam);
private IntPtr FindDescendantByClassName(string className) { descendantFound = IntPtr.Zero; seekClassName = className; EnumWindowProc childProc = new EnumWindowProc(EnumWindow); EnumChildWindows(this.mainHandle, childProc, IntPtr.Zero); return descendantFound; }
internal static extern bool EnumChildWindows(IntPtr parentHandle, EnumWindowProc callback, IntPtr lParam);
private static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);
public static extern int EnumChildWindows(IntPtr hWndParent, EnumWindowProc lpEnumFunc, IntPtr lParam);
internal static partial bool EnumWindows(EnumWindowProc callback, IntPtr lParam);