コード例 #1
0
        public void ParkingWindow_Multiple()
        {
            // 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 var control = new Control();
                ThreadContext ctx = GetContextForHandle(new HandleRef(this, control.Handle));
                Assert.NotNull(ctx);
                ParkingWindow parkingWindow = ctx.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2);
                Assert.NotNull(parkingWindow);

                IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle);
                Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext));

                using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE))
                {
                    using var systemControl = new Control();
                    ctx = GetContextForHandle(new HandleRef(this, systemControl.Handle));
                    Assert.NotNull(ctx);
                    parkingWindow = ctx.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE);
                    Assert.NotNull(parkingWindow);

                    dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle);
                    Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE, dpiContext));

                    // check PMv2 parking window still available.
                    parkingWindow = ctx.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2);
                    Assert.NotNull(parkingWindow);

                    dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle);
                    Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext));
                }
            }
            finally
            {
                // reset back to original awareness context.
                User32.SetThreadDpiAwarenessContext(originalAwarenessContext);
            }
        }