110,499
社区成员
发帖
与我相关
我的任务
分享
public const long WS_CHILD = 0x40000000L, WS_POPUP = 0x80000000L;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll")]
public static extern long GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
public static extern long SetWindowLong(IntPtr hWnd, int nIndex, long dwNewLong);
public static void RemoveWindowStyle(IntPtr hwnd, long styles)
{
styles = GetWindowLong(hwnd, GWL_STYLE) & ~styles;
SetWindowLong(hwnd, GWL_STYLE, styles);
}
public static void AddWindowStyle(IntPtr hwnd, long styles)
{
styles = GetWindowLong(hwnd, GWL_STYLE) | styles;
SetWindowLong(hwnd, GWL_STYLE, styles);
}
public static void SafeSetParent(IntPtr hChildWnd, IntPtr hNewParent)
{
//移除父窗口
if (hNewParent == IntPtr.Zero)
{
SetParent(hChildWnd, hNewParent);
RemoveWindowStyle(hChildWnd, WS_CHILD);
AddWindowStyle(hChildWnd, WS_POPUP);
}
//设置父窗口
else
{
RemoveWindowStyle(hChildWnd, WS_POPUP);
AddWindowStyle(hChildWnd, WS_CHILD);
SetParent(hChildWnd, hNewParent);
}
}