///<summary>Snaps the child window to the top level owner window, relative to the hWndSnap window. As ///the top level owner window is moved, resized, minimized or maximized, the child window moves too.</summary> ///<param name="child">The control that is automtically moved, e.g. a ToolStripDropDown window.</param> ///<param name="hWndTopLevel">A handle to a top-level window, e.g. a Form control's Handle.</param> ///<param name="hWndParent">A handle to a control that child window is relatively positioned, e.g. a TextBox or ComoBox.</param> ///<param name="snapPoint">Parameters that specify the X and Y location.</param> public static void SnapWindow(this Control child, IntPtr hWndParent, IntPtr hWndTopLevel, SnapPoint snapPoint) { if (child.IsHandleCreated) SetOwner(child.Handle, hWndParent, hWndTopLevel, snapPoint); else { child.HandleCreated += delegate { SetOwner(child.Handle, hWndParent, hWndTopLevel, snapPoint); }; } }
public static void SetOwner(IntPtr hWndChild, IntPtr hWndParent, IntPtr hWndTopLevel, SnapPoint snapPoint) { Data d = (Data) htData[hWndChild]; if (d != null) { d.nwOwner.ReleaseHandle(); if (hWndTopLevel != IntPtr.Zero) d.nwOwner = new OwnerNW(hWndTopLevel, d); else { d.nwChild.ReleaseHandle(); htData.Remove(hWndChild); } } else { if (hWndTopLevel == IntPtr.Zero) return; d = new Data(snapPoint, hWndChild, hWndTopLevel, hWndParent); htData[hWndChild] = d; } }
public Data(SnapPoint snapPoint, IntPtr hWndChild, IntPtr hWndOwner, IntPtr hWndSnap) { this.snapPoint = snapPoint; SnapHandle = hWndSnap; nwChild = new ChildNW(hWndChild, this); nwOwner = new OwnerNW(hWndOwner, this); }