首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >创建支持DPI的应用程序

创建支持DPI的应用程序
EN

Stack Overflow用户
提问于 2010-11-02 15:56:27
回答 9查看 103.2K关注 0票数 74

我有一个C#格式的表单应用程序。当我改变显示器的DPI时,所有的控件都会移动。我使用了代码this.AutoScaleMode = AutoScaleMode.Dpi,但它并没有避免这个问题。

有谁有主意吗?

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2010-11-02 17:14:28

编辑:从.NET 4.7开始,windows forms改进了对High DPI的支持。Read more about it on docs.microsoft.com它只适用于Win 10创建者更新和更高版本,所以它可能还不可行,这取决于你的用户基础。

很难,但也不是不可能。当然,您最好的选择是迁移到WPF,但这可能并不可行。

我花了很多时间来解决这个问题。以下是一些规则/指南,可以让它在没有FlowLayoutPanel或TableLayoutPanel的情况下正常工作:

  • 始终以默认96 DPI (100%)编辑/设计您的应用程序。如果你用120DPI (125%DPI)进行设计,当你稍后回到96DPI来处理它时,它会变得非常糟糕。
  • 我已经成功地使用了AutoScaleMode.Font,但我没有尝试太多AutoScaleMode.DPI。
  • 确保你在所有的容器(表单,面板,标签页,用户控件等)上都使用默认的字体大小。8,25像素最好不要在.Designer.cs文件中为所有容器设置AutoScaleMode
  • Make,这样它就会使用container类中的默认字体。
  • all containers 必须使用相同的字体确保所有容器都在Designer.cs文件中设置了以下行:

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); // for design in 96 DPI

  • 如果您需要在标签/文本框等上设置不同的字体大小,请按控件设置它们,而不是在container类上设置字体,因为winforms使用containers字体设置来缩放它的内容,并且让f.ex创建一个与它所包含的表单不同的字体大小的面板肯定会出现问题。如果窗体和窗体上的所有容器使用相同的字体大小,它可能会起作用,但我还没有尝试过。
  • 使用另一台机器或具有更高DPI设置的虚拟windows安装(VMware、虚拟PC、VirtualBox)来立即测试您的设计。只需从DEV机器上的/bin/ .exe文件夹运行编译后的调试文件。

我向您保证,如果您遵循这些指导原则,即使您放置了具有特定锚点的控件并且不使用流面板,您也不会有问题。我们有一个以这种方式构建的应用程序部署在数百台具有不同DPI设置的机器上,我们不再有任何抱怨。所有表单/容器/网格/按钮/文本字段等大小都像字体一样被正确缩放。图像也可以,但它们在高DPI时往往会变得有点像素化。

编辑:这个链接有很多好的信息,特别是如果你选择使用AutoScaleMode.DPI:link to related stackoverflow question

票数 116
EN

Stack Overflow用户

发布于 2018-08-29 17:57:40

注意:当dpi发生变化时,这将不会修复控件的移动。这将只修复模糊的文本!!。

如何在高dpi设置中修复模糊的Windows窗体:

  1. 转到窗体设计器,然后选择您的窗体(通过单击窗体的标题栏)
  2. 按F4打开属性窗口,
  3. 然后找到AutoScaleMode将其从字体(默认)转到Dpi.

现在,转到Program.cs (或Main方法所在的文件)并将其更改为如下所示:

代码语言:javascript
复制
namespace myApplication
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            // ***this line is added***
            if (Environment.OSVersion.Version.Major >= 6)
                SetProcessDPIAware();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }

        // ***also dllimport of that function***
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern bool SetProcessDPIAware();
    }
}

保存并编译。现在,您的表单应该又看起来很清爽了。

来源:http://crsouza.com/2015/04/13/how-to-fix-blurry-windows-forms-windows-in-high-dpi-settings/

票数 30
EN

Stack Overflow用户

发布于 2012-04-13 06:59:44

我终于找到了屏幕方向和DPI处理问题的解决方案。

微软已经提供了一个文档解释它,但有一个小缺陷,将完全杀死DPI处理。只需按照“为每个方向创建单独的布局代码”http://msdn.microsoft.com/en-us/library/ms838174.aspx下的文档中提供的解决方案操作即可

那就是重要的部分!在Landscape()和Portrait()方法的代码中,在每个方法的末尾添加以下行:

代码语言:javascript
复制
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;

因此,这两个方法的代码如下所示:

代码语言:javascript
复制
protected void Portrait()
{
   this.SuspendLayout();
   this.crawlTime.Location = new System.Drawing.Point(88, 216);
   this.crawlTime.Size = new System.Drawing.Size(136, 16);
   this.crawlTimeLabel.Location = new System.Drawing.Point(10, 216);
   this.crawlTimeLabel.Size = new System.Drawing.Size(64, 16);
   this.crawlStartTime.Location = new System.Drawing.Point(88, 200);
   this.crawlStartTime.Size = new System.Drawing.Size(136, 16);
   this.crawlStartedLabel.Location = new System.Drawing.Point(10, 200);
   this.crawlStartedLabel.Size = new System.Drawing.Size(64, 16);
   this.light1.Location = new System.Drawing.Point(208, 66);
   this.light1.Size = new System.Drawing.Size(16, 16);
   this.light0.Location = new System.Drawing.Point(192, 66);
   this.light0.Size = new System.Drawing.Size(16, 16);
   this.linkCount.Location = new System.Drawing.Point(88, 182);
   this.linkCount.Size = new System.Drawing.Size(136, 16);
   this.linkCountLabel.Location = new System.Drawing.Point(10, 182);
   this.linkCountLabel.Size = new System.Drawing.Size(64, 16);
   this.currentPageBox.Location = new System.Drawing.Point(10, 84);
   this.currentPageBox.Size = new System.Drawing.Size(214, 90);
   this.currentPageLabel.Location = new System.Drawing.Point(10, 68);
   this.currentPageLabel.Size = new System.Drawing.Size(100, 16);
   this.addressLabel.Location = new System.Drawing.Point(10, 4);
   this.addressLabel.Size = new System.Drawing.Size(214, 16);
   this.noProxyCheck.Location = new System.Drawing.Point(10, 48);
   this.noProxyCheck.Size = new System.Drawing.Size(214, 20);
   this.startButton.Location = new System.Drawing.Point(8, 240);
   this.startButton.Size = new System.Drawing.Size(216, 20);
   this.addressBox.Location = new System.Drawing.Point(10, 24);
   this.addressBox.Size = new System.Drawing.Size(214, 22);

   //note! USING JUST AUTOSCALEMODE WILL NOT SOLVE ISSUE. MUST USE BOTH!
   this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); //IMPORTANT
   this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;   //IMPORTANT
   this.ResumeLayout(false);
}

protected void Landscape()
{
   this.SuspendLayout();
   this.crawlTime.Location = new System.Drawing.Point(216, 136);
   this.crawlTime.Size = new System.Drawing.Size(96, 16);
   this.crawlTimeLabel.Location = new System.Drawing.Point(160, 136);
   this.crawlTimeLabel.Size = new System.Drawing.Size(48, 16);
   this.crawlStartTime.Location = new System.Drawing.Point(64, 120);
   this.crawlStartTime.Size = new System.Drawing.Size(248, 16);
   this.crawlStartedLabel.Location = new System.Drawing.Point(8, 120);
   this.crawlStartedLabel.Size = new System.Drawing.Size(48, 16);
   this.light1.Location = new System.Drawing.Point(296, 48);
   this.light1.Size = new System.Drawing.Size(16, 16);
   this.light0.Location = new System.Drawing.Point(280, 48);
   this.light0.Size = new System.Drawing.Size(16, 16);
   this.linkCount.Location = new System.Drawing.Point(80, 136);
   this.linkCount.Size = new System.Drawing.Size(72, 16);
   this.linkCountLabel.Location = new System.Drawing.Point(8, 136);
   this.linkCountLabel.Size = new System.Drawing.Size(64, 16);
   this.currentPageBox.Location = new System.Drawing.Point(10, 64);
   this.currentPageBox.Size = new System.Drawing.Size(302, 48);
   this.currentPageLabel.Location = new System.Drawing.Point(10, 48);
   this.currentPageLabel.Size = new System.Drawing.Size(100, 16);
   this.addressLabel.Location = new System.Drawing.Point(10, 4);
   this.addressLabel.Size = new System.Drawing.Size(50, 16);
   this.noProxyCheck.Location = new System.Drawing.Point(168, 16);
   this.noProxyCheck.Size = new System.Drawing.Size(152, 24);
   this.startButton.Location = new System.Drawing.Point(8, 160);
   this.startButton.Size = new System.Drawing.Size(304, 20);
   this.addressBox.Location = new System.Drawing.Point(10, 20);
   this.addressBox.Size = new System.Drawing.Size(150, 22);

   //note! USING JUST AUTOSCALEMODE WILL NOT SOLVE ISSUE. MUST USE BOTH!
   this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); //IMPORTANT
   this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;   //IMPORTANT
   this.ResumeLayout(false);
}

对我来说很有魅力。

票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4075802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档