Une fois que tu possèdes le handle d'un contrôle enfant, tu peux utiliser toutes les fonctions Win32 disponibles avec ce handle, comme par exemple GetClassName, pour connaitre son "type" :
public partial class Form1 : Form
{
[ DllImport( "user32.dll", CharSet = CharSet.Auto, SetLastError = true ) ]
private static extern int GetClassName
( IntPtr hWnd, StringBuilder lpClassName, int nMaxCount );
//[ DllImport( "user32.dll", CharSet = CharSet.Auto, SetLastError = true ) ]
//private static extern uint RealGetWindowClass
// ( IntPtr hwnd, StringBuilder pszType, uint cchType );
[ DllImport( "user32.dll" ) ]
private static extern bool EnumChildWindows
( IntPtr hWndParent, EnumChildProc lpEnumFunc, IntPtr lParam );
private delegate bool EnumChildProc( IntPtr hwnd, IntPtr lParam );
public Form1( )
{
InitializeComponent( );
this.Show( );
Button b = new Button( );
b.Parent = this;
b.Text = "Enum";
b.Click += delegate // Méthode anonyme
{
try
{
EnumChildWindows
(
this.Handle,
delegate ( IntPtr hwnd, IntPtr lParam ) // Méthode anonyme
{
StringBuilder className = new StringBuilder( 256 );
int res = GetClassName( hwnd, className, className.Capacity );
if ( res == 0 )
throw new Win32Exception( Marshal.GetLastWin32Error( ) );
MessageBox.Show( className.ToString( ) );
return true;
},
IntPtr.Zero
);
}
catch ( Exception ex )
{
MessageBox.Show( ex.ToString( ) );
}
};
}
}