internal override void RecreateHandleCore() { //Debug.Assert( CanRecreateHandle(), "Recreating handle when form is not ready yet." ); NativeMethods.WINDOWPLACEMENT wp = new NativeMethods.WINDOWPLACEMENT(); FormStartPosition oldStartPosition = FormStartPosition.Manual; if (!IsMdiChild && (WindowState == FormWindowState.Minimized || WindowState == FormWindowState.Maximized)) { wp.length = Marshal.SizeOf(typeof(NativeMethods.WINDOWPLACEMENT)); UnsafeNativeMethods.GetWindowPlacement(new HandleRef(this, Handle), ref wp); } if (StartPosition != FormStartPosition.Manual) { oldStartPosition = StartPosition; // Set the startup postion to manual, to stop the form from // changing position each time RecreateHandle() is called. StartPosition = FormStartPosition.Manual; } EnumThreadWindowsCallback etwcb = null; SafeNativeMethods.EnumThreadWindowsCallback callback = null; if (IsHandleCreated) { // First put all the owned windows into a list etwcb = new EnumThreadWindowsCallback(); if (etwcb != null) { callback = new SafeNativeMethods.EnumThreadWindowsCallback(etwcb.Callback); UnsafeNativeMethods.EnumThreadWindows(SafeNativeMethods.GetCurrentThreadId(), new NativeMethods.EnumThreadWindowsCallback(callback), new HandleRef(this, this.Handle)); // Reset the owner of the windows in the list etwcb.ResetOwners(); } } base.RecreateHandleCore(); if (etwcb != null) { // Set the owner of the windows in the list back to the new Form's handle etwcb.SetOwners(new HandleRef(this, this.Handle)); } if (oldStartPosition != FormStartPosition.Manual) { StartPosition = oldStartPosition; } if (wp.length > 0) { UnsafeNativeMethods.SetWindowPlacement(new HandleRef(this, Handle), ref wp); } if (callback != null) { GC.KeepAlive(callback); } }
internal override void RecreateHandleCore() { System.Windows.Forms.NativeMethods.WINDOWPLACEMENT placement = new System.Windows.Forms.NativeMethods.WINDOWPLACEMENT(); FormStartPosition manual = FormStartPosition.Manual; if (!this.IsMdiChild && ((this.WindowState == FormWindowState.Minimized) || (this.WindowState == FormWindowState.Maximized))) { placement.length = Marshal.SizeOf(typeof(System.Windows.Forms.NativeMethods.WINDOWPLACEMENT)); System.Windows.Forms.UnsafeNativeMethods.GetWindowPlacement(new HandleRef(this, base.Handle), ref placement); } if (this.StartPosition != FormStartPosition.Manual) { manual = this.StartPosition; this.StartPosition = FormStartPosition.Manual; } EnumThreadWindowsCallback callback = null; System.Windows.Forms.SafeNativeMethods.EnumThreadWindowsCallback callback2 = null; if (base.IsHandleCreated) { callback = new EnumThreadWindowsCallback(); if (callback != null) { callback2 = new System.Windows.Forms.SafeNativeMethods.EnumThreadWindowsCallback(callback.Callback); System.Windows.Forms.UnsafeNativeMethods.EnumThreadWindows(System.Windows.Forms.SafeNativeMethods.GetCurrentThreadId(), new System.Windows.Forms.NativeMethods.EnumThreadWindowsCallback(callback2.Invoke), new HandleRef(this, base.Handle)); callback.ResetOwners(); } } base.RecreateHandleCore(); if (callback != null) { callback.SetOwners(new HandleRef(this, base.Handle)); } if (manual != FormStartPosition.Manual) { this.StartPosition = manual; } if (placement.length > 0) { System.Windows.Forms.UnsafeNativeMethods.SetWindowPlacement(new HandleRef(this, base.Handle), ref placement); } if (callback2 != null) { GC.KeepAlive(callback2); } }