1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| public static class FormEmbedder { [DllImport("user32.dll", SetLastError = true)] private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll", SetLastError = true)] private static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", SetLastError = true)] private static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[DllImport("user32.dll", SetLastError = true)] private static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
private const int GWL_STYLE = -16; private const int WS_BORDER = 0x00800000; private const int WS_CAPTION = 0x00C00000;
public static void EmbedWindow(IntPtr childPtr, Control parent) { IntPtr style = GetWindowLong(childPtr, GWL_STYLE); style = (IntPtr)((int)style & ~WS_BORDER & ~WS_CAPTION); SetWindowLong(childPtr, GWL_STYLE, style);
SetParent(childPtr, parent.Handle);
MoveWindow(childPtr, 0, 0, parent.Width, parent.Height, true);
parent.SizeChanged += (s, e) => { MoveWindow(childPtr, 0, 0, parent.Width, parent.Height, true); }; } }
|