public StrongHWNDSubclass(StrongHWND strongHwnd) : base(new HWND(strongHwnd.DangerousGetHandle())) { // Note that we passed a new "weak" HWND handle to the base class. // This is because we don't want the StrongHWNDSubclass processing // a partially disposed handle in its own Dispose methods. _strongHwnd = strongHwnd; }
/// <summary> /// Creates the hosted child window. /// </summary> /// <remarks> /// Derived types override the BuildWindowCore virtual method to /// create the child window. The child window is parented to /// a seperate top-level window for the purposes of redirection. /// The SafeWindowHandle type controls the lifetime of the /// child window. It will be disposed when the RedirectedHwndHost /// is disposed, or when the SafeWindowHandle is finalized. Set /// the SafeWindowHandle.DestroyWindowOnRelease property to true /// if you want the window destroyed automatically. /// </remarks> protected virtual HWND BuildWindowCore(HWND hwndParent) { StrongHWND hwndChild = StrongHWND.CreateWindowEx( 0, "STATIC", "Default RedirectedHwndHost Window", WS.CHILD | WS.CLIPSIBLINGS | WS.CLIPCHILDREN, 0, 0, 0, 0, hwndParent, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); return(hwndChild); }
protected virtual void Dispose(bool disposing) { if (disposing) { StrongHWND strongHwnd = _hwnd as StrongHWND; if (strongHwnd != null) { // Replace the StrongHWND reference with a regular "weak" // HWND reference so that messages processed during // disposing do not have to deal with a partially disposed // SafeHandle. _hwnd = new HWND(strongHwnd.DangerousGetHandle()); strongHwnd.Dispose(); // All done, replace the "weak" HWND reference with null. _hwnd = null; } } }
public TWindow CreateWindow(WindowParameters windowParams) { GCHandle gcHandle = new GCHandle(); IntPtr lpCreateParam = IntPtr.Zero; if (windowParams.Tag != null) { gcHandle = GCHandle.Alloc(windowParams.Tag); lpCreateParam = GCHandle.ToIntPtr(gcHandle); } StrongHWND hwnd = StrongHWND.CreateWindowEx( windowParams.ExtendedStyle, Name, windowParams.Name, windowParams.Style, windowParams.WindowRect.X, windowParams.WindowRect.Y, windowParams.WindowRect.Width, windowParams.WindowRect.Height, windowParams.Parent, IntPtr.Zero, Marshal.GetHINSTANCE(typeof(TWindow).Module), lpCreateParam); TWindow createdWindow = null; if (!hwnd.IsInvalid) { Debug.Assert(_createdWindow != null); _createdWindow.TransferHandleOwnership(hwnd); createdWindow = _createdWindow; } _createdWindow = null; return(createdWindow); }
internal void TransferHandleOwnership(StrongHWND hwnd) { Debug.Assert(hwnd == _hwnd); // equivalency, not reference equals _hwnd = hwnd; }