1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | /// <summary> /// Uses FindWindowEx() to recursively search for a child window with the given class and/or title. /// </summary> public static IntPtr FindChildWindow( IntPtr hwndParent, string lpszClass, string lpszTitle ) { return FindChildWindow( hwndParent, IntPtr.Zero, lpszClass, lpszTitle ); } [DllImport( "user32.dll" , SetLastError = true )] public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); /// <summary> /// Uses FindWindowEx() to recursively search for a child window with the given class and/or title. /// </summary> public static IntPtr FindChildWindow( IntPtr hwndParent, string lpszClass, string lpszTitle ) { return FindChildWindow( hwndParent, IntPtr.Zero, lpszClass, lpszTitle ); } /// <summary> /// Uses FindWindowEx() to recursively search for a child window with the given class and/or title, /// starting after a specified child window. /// If lpszClass is null, it will match any class name. It's not case-sensitive. /// If lpszTitle is null, it will match any window title. /// </summary> public static IntPtr FindChildWindow( IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszTitle ) { // Try to find a match. IntPtr hwnd = FindWindowEx( hwndParent, IntPtr.Zero, lpszClass, lpszTitle ); if ( hwnd == IntPtr.Zero ) { // Search inside the children. IntPtr hwndChild = FindWindowEx( hwndParent, IntPtr.Zero, null , null ); while ( hwndChild != IntPtr.Zero && hwnd == IntPtr.Zero ) { hwnd = FindChildWindow( hwndChild, IntPtr.Zero, lpszClass, lpszTitle ); if ( hwnd == IntPtr.Zero ) { // If we didn't find it yet, check the next child. hwndChild = FindWindowEx( hwndParent, hwndChild, null , null ); } } } return hwnd; } |
Saturday, November 14, 2009
Finding child windows in c#
If you have a IntPtr window handle (e.g. by using Process.MainWindow), the following helper functions lets you find any of its child windows by class name and/or window title, recursively:
Subscribe to:
Post Comments (Atom)
thank you!
ReplyDeleteThank You So much, Its working
ReplyDelete