public static bool IsOwned( IntPtr owner, IntPtr hwnd, ref int level) { while (true) { var ownerWindow = WindowNative .GetWindow(hwnd, ModalWindow.GwOwner); if (ownerWindow == IntPtr.Zero) { return(false); } if (ownerWindow == owner) { return(true); } ++level; hwnd = ownerWindow; } }