public void ParkingWindow_SystemAware() { // run tests only on Windows 10 versions that support thread dpi awareness. if (!PlatformDetection.IsWindows10Version1803OrGreater) { return; } // set thread awareness context to PermonitorV2(PMv2). // if process/thread is not in PMv2, calling 'EnterDpiAwarenessScope' is a no-op and that is by design. // In this case, we will be setting thread to PMv2 mode and then scope to UNAWARE IntPtr originalAwarenessContext = User32.SetThreadDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2); try { using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE)) { using var control = new Control(); ThreadContext ctx = GetContextForHandle(new HandleRef(this, control.Handle)); Assert.NotNull(ctx); ParkingWindow parkingWindow = ctx.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE); Assert.NotNull(parkingWindow); IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle); Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE, dpiContext)); } } finally { // reset back to original awareness context. User32.SetThreadDpiAwarenessContext(originalAwarenessContext); } }
/// <include file='doc\Application.uex' path='docs/doc[@for="Application.ThreadContext.DisposeParkingWindow"]/*' /> /// <devdoc> /// Disposes of this thread's parking form. /// </devdoc> /// <internalonly/> private void DisposeParkingWindow() { if (parkingWindow != null && parkingWindow.IsHandleCreated) { // We take two paths here. If we are on the same thread as // the parking window, we can destroy its handle. If not, // we just null it and let it GC. When it finalizes it // will disconnect its handle and post a WM_CLOSE. // // It is important that we just call DestroyHandle here // and do not call Dispose. Otherwise we would destroy // controls that are living on the parking window. // int pid; int hwndThread = SafeNativeMethods.GetWindowThreadProcessId(new HandleRef(parkingWindow, parkingWindow.Handle), out pid); int currentThread = SafeNativeMethods.GetCurrentThreadId(); if (hwndThread == currentThread) { parkingWindow.Destroy(); } else { parkingWindow = null; } } }