I'm trying to write a helper class to let me set the parent of a specific window, and then release the parent at a specific point or when the class is disposed.
My problem is that after I call SetParent function, GetParent always returns null. This is a problem because for this reason I can't do a safety check to determine whether the window is parented before trying to release it from its current parent and set the window (window style and position) back to its normal state.
I also tried IsChild function and it also returns null.
I'm using a custom SafeWindowHandle
class that inherits from SafeHandleZeroOrMinusOneIsInvalid class to ensure that the parented window handle does not turn zero. Also my P/Invoke definitions for SetParent
/ GetParent
/ Ischild
functions takes a SafeHandle
object.
What could be the problem with GetParent
and IsChild
?.
This is a sample code that in my case reproduces the problem:
Dim pr As Process = Process.GetProcessesByName("name").Single()
Dim hwnd As New SafeWindowHandle(pr.MainWindowHandle)
NativeMethods.SetParent(hwnd, Me.Panel1.Handle)
Dim hasParent As Boolean = NativeMethods.GetParent(hwnd)
Console.WriteLine(hasParent.ToString())