繁体   English   中英

在 C# windows 表单中的某些 forms 表单上的某些控件未调整大小

[英]Some controls on form not resizing on some forms in C# windows form

我有代码来调整我的 forms 上的控件大小,以便它们可以在任何屏幕分辨率上“响应”显示,此代码在我的某些 forms 上完美运行,但不幸的是不适用于我的其他 forms。

我怀疑可能是因为 Group-boxes 和面板中的一些控件,所以也许它们没有被“到达”,但代码已经证明这不是答案。

这是一些代码

#region Scale Controls To Screen Size

        Responsive ResponsiveObj;

        private void ScaleControls(Control.ControlCollection controls)
        {
            foreach (Control ctl in controls)
            {
                Scale(ctl);
                if (ctl is GroupBox || ctl is StatusStrip || ctl is DevExpress.XtraEditors.BarCodeControl || ctl is Panel || ctl is LabelControl || ctl is DevExpress.XtraBars.Navigation.TabPane || ctl is DevExpress.XtraBars.Navigation.TabNavigationPage)   // Recursive call if it is a Groupbox (which has more controls inside it).
                    ScaleControls(ctl.Controls);
            }
        }

        private void Scale(Control ctl)
        {
            ctl.Font = new Font(ctl.Font.Name, ResponsiveObj.GetMetrics((int)ctl.Font.Size), ctl.Font.Style);
            if (ctl is LookUpEdit)
            {
                (ctl as LookUpEdit).Properties.Appearance.Font = ctl.Font;
                (ctl as LookUpEdit).Properties.AppearanceDisabled.Font = ctl.Font;
                (ctl as LookUpEdit).Properties.AppearanceDropDown.Font = ctl.Font;
                (ctl as LookUpEdit).Properties.AppearanceDropDownHeader.Font = ctl.Font;
                (ctl as LookUpEdit).Properties.AppearanceFocused.Font = ctl.Font;
                (ctl as LookUpEdit).Properties.AppearanceReadOnly.Font = ctl.Font;
            }
            ctl.Width = ResponsiveObj.GetMetrics(ctl.Width, "Width");
            ctl.Height = ResponsiveObj.GetMetrics(ctl.Height, "Height");
            ctl.Top = ResponsiveObj.GetMetrics(ctl.Top, "Top");
            ctl.Left = ResponsiveObj.GetMetrics(ctl.Left, "Left");
        }

        #endregion

在构造函数中

ResponsiveObj = new Responsive(Screen.PrimaryScreen.Bounds);
ResponsiveObj.SetMultiplicationFactor();

在表单加载事件中

Width = ResponsiveObj.GetMetrics(Width, "Width");    // Form width and height set up.
Height = ResponsiveObj.GetMetrics(Height, "Height");
Left = Screen.GetBounds(this).Width / 2 - Width / 2;  // Form centering.
Top = Screen.GetBounds(this).Height / 2 - Height / 2 - 30;  // 30 is a calibration factor.

ScaleControls(Controls);

我想也许 ScaleControls 程序没有访问所有控件,所以我更改了它但仍然得到相同的结果

private void ScaleControls(Control.ControlCollection controls)
{
            foreach (Control ctl in controls)
            {
                Scale(ctl);
                if (ctl.HasChildren)   // Recursive call if it is a Groupbox (which has more controls inside it).
                    ScaleControls(ctl.Controls);
            }
}

图像在底部显示了一个调整大小的 TextEdit,在它的顶部是两个未调整大小的类似 TextEdit 控件

我意识到我的一些控件可能不包含在控件集合中,例如 TextEdit 控件 - 我正在尝试找到一种方法来创建所有控件的列表,包括 TextEdit 控件以及其他 DevExpress 控件,以便它们可以调整大小

我刚刚在 Microsoft Onedrive上上传了一个示例项目。 该项目是在 1920 * 1080 显示器上开发的,但我们希望 forms 即使在其他较小的显示器(例如 1600 * 900)上也能很好地调整和显示。 第一种形式 In Big 在其他较小的显示器上反应良好,字体大小正确缩放,控件正确放置在 1600 * 900 显示器上。 但是第二种形式的出站未正确调整大小。 Full Weight 和 Empty Weight TextEdit 未调整大小且字体不正确 - 但该表单是 In Big 表单的副本并进行了一些调整。我希望控件和 fonts 即使在出站表单中也能正确调整大小就像它以大形式做的那样

我发现在“Big In”表单中,tabPane DevExpress 控件已停靠,但在其他 forms 中却没有。 这种细微的差异是导致问题的原因

从 .NET 框架 4.7 开始,Windows Forms 包括针对常见高 DPI 和动态 DPI 场景的增强功能。 这些包括:

改进了许多 Windows Forms 控件的缩放和布局,例如 MonthCalendar 控件和 CheckedListBox 控件。

单程缩放。 在 .NET 框架 4.6 及更早版本中,缩放是通过多次传递执行的,这导致某些控件的缩放超出了必要的范围。

支持在 Windows Forms 应用程序启动后用户更改 DPI 或比例因子的动态 DPI 场景。

在从 .NET 框架 4.7 开始的 .NET 框架版本中,增强的高 DPI 支持是一项可选功能。 您必须配置您的应用程序以利用它。

配置 Windows Forms 应用程序以获得高 DPI 支持

The new Windows Forms features that support high DPI awareness are available only in applications that target the .NET Framework 4.7 and are running on Windows operating systems starting with the Windows 10 Creators Update.

此外,要在 Windows Forms 应用程序中配置高 DPI 支持,您必须执行以下操作:

声明与 Windows 10 的兼容性。

为此,请将以下内容添加到清单文件中:

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
  <application>
    <!-- Windows 10 compatibility -->
    <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
  </application>
</compatibility>

在 app.config 文件中启用每个显示器的 DPI 感知。

Windows Forms 引入了一个新元素,以支持从 .NET 框架 4.7 开始添加的新功能和自定义。 要利用支持高 DPI 的新功能,请将以下内容添加到您的应用程序配置文件中。

<System.Windows.Forms.ApplicationConfigurationSection>
  <add key="DpiAwareness" value="PerMonitorV2" />
</System.Windows.Forms.ApplicationConfigurationSection>

调用 static EnableVisualStyles 方法。

这应该是应用程序入口点中的第一个方法调用。 例如:

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form2());
}

退出个别高 DPI 功能将 DpiAwareness 值设置为 PerMonitorV2 可启用从 .NET 框架 4.7 开始的 .NET 框架版本支持的所有高 DPI 感知功能。 通常,这对于大多数 Windows Forms 应用程序来说已经足够了。 但是,您可能希望选择退出一项或多项单独的功能。 这样做的最重要原因是您现有的应用程序代码已经处理了该功能。 例如,如果您的应用程序处理自动缩放,您可能希望禁用自动调整大小功能,如下所示:

<System.Windows.Forms.ApplicationConfigurationSection>
  <add key="DpiAwareness" value="PerMonitorV2" />
  <add key="EnableWindowsFormsHighDpiAutoResizing" value="false" />
</System.Windows.Forms.ApplicationConfigurationSection>

有关更多配置相关帮助:: 阅读:: https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/winforms/windows-forms-add-configuration-element

希望这将使您的应用程序看起来不错!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM