views:

66

answers:

1

How can I check if a Win32 Window pointer is a valid .Net Control?

+2  A: 

I'm going to assume that, by "Win32 Window pointer", you mean an hWnd.

You can call Control.FromChildHandle() supplying your hWnd as a parameter. If the hWnd is associated with a .NET Control, then you will receive, as a return value, a reference to the .NET Control representing the control. If the hWnd is not associated with a .NET Control, then you will receive, as a return value, a value of null.

Pseudocode is as follows:

Control AssociatedDotNetControl = Control.FromChildHandle(Win32WindowPointerAshWnd);
if(AssociatedDotNetControl != null)
{
    // this is a .NET control
}
else
{
    // this is not a .NET control
}
JeffFerguson
short and smart!
Thomas Maierhofer
Does this work if the hWnd is from another program?
Matthew Scharley
Matt, I haven't tried that specifically, but I am guessing that it would indeed work.
JeffFerguson