示例#1
0
        public void Form_DpiChanged_Bounds(int newDpi)
        {
            // Run tests only on Windows 10 versions that support thread dpi awareness.
            if (!PlatformDetection.IsWindows10Version1803OrGreater)
            {
                return;
            }

            IntPtr originalAwarenessContext = User32.SetThreadDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2);

            try
            {
                using var form     = new Form();
                form.AutoScaleMode = AutoScaleMode.Dpi;
                form.Show();
                Drawing.Rectangle initialBounds   = form.Bounds;
                float             initialFontSize = form.Font.Size;
                DpiMessageHelper.TriggerDpiMessage(User32.WM.DPICHANGED, form, newDpi);
                var factor = newDpi / DpiHelper.LogicalDpi;

                // Lab machines giving strange values that I could not explain. for ex: on local machine,
                // I get 1050*1050 for factor 3.5. This is not same on lab machines ( ex, we get 1044). For now,
                // just verifying they are scaled.
                Assert.NotEqual(initialBounds.Width, form.Bounds.Width);
                Assert.NotEqual(initialBounds.Height, form.Bounds.Height);
                Assert.NotEqual(initialFontSize, form.Font.Size);
                form.Close();
            }
            finally
            {
                // Reset back to original awareness context.
                User32.SetThreadDpiAwarenessContext(originalAwarenessContext);
            }
        }
        public void ToolStripItems_FontScaling(int newDpi)
        {
            // Run tests only on Windows 10 versions that support thread dpi awareness.
            if (!PlatformDetection.IsWindows10Version1803OrGreater)
            {
                return;
            }

            // Set thread awareness context to PermonitorV2(PMv2).
            IntPtr originalAwarenessContext = User32.SetThreadDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2);

            try
            {
                int clientWidth = 800;
                using var form              = new Form();
                form.AutoScaleMode          = AutoScaleMode.Dpi;
                form.ClientSize             = new Size(clientWidth, 450);
                using var toolStrip         = new ToolStrip();
                using var toolStripItemOpen = new ToolStripButton("Open");

                toolStrip.GripStyle = ToolStripGripStyle.Hidden;
                toolStrip.Items.Add(toolStripItemOpen);
                toolStrip.Location     = new Point(0, 0);
                toolStrip.Name         = "toolStrip1";
                toolStrip.Text         = "toolStrip1";
                using Font initialFont = toolStrip.Font = new Font("Segoe UI", 9F, FontStyle.Bold, GraphicsUnit.Point);

                form.Show();

                DpiMessageHelper.TriggerDpiMessage(User32.WM.DPICHANGED_BEFOREPARENT, toolStrip, newDpi);
                var factor = newDpi / DpiHelper.LogicalDpi;

                Assert.Equal((float)initialFont.Size * factor, toolStrip.Font.Size);
                form.Close();
            }
            finally
            {
                // Reset back to original awareness context.
                User32.SetThreadDpiAwarenessContext(originalAwarenessContext);
            }
        }