I have a form application in C#. When I change the monitor's DPI, all the controls move. I used the code this.AutoScaleMode = AutoScaleMode.Dpi
, but it didn't avoid the problem.
Does anyone have an idea?
System: When an app is system DPI aware, it'll obtain the scaling settings from the primary monitor allowing the app to scale and render correctly, no matter the scaling settings.
To make your application dpi-aware, you must cancel automatic dpi scaling, and then adjust user interface elements to scale appropriately to the system dpi.
Windows Presentation Foundation (WPF) applications are by default system DPI-aware.
Select Display > Change the size of text, apps, and other items, and then adjust the slider for each monitor. Right-click the application, select Properties, select the Compatibility tab, and then select the Disable display scaling on high DPI settings check box.
EDIT: As of .NET 4.7, windows forms has improved support for High DPI. Read more about it on docs.microsoft.com It only works for Win 10 Creators Update and higher though, so it might not be feasible to use this yet depending on your user base.
Difficult, but not impossible. Your best option is to move to WPF of course, but that might not be feasible.
I've spent A LOT of time with this problem. Here are some rules/guidelines to make it work correctly without a FlowLayoutPanel or TableLayoutPanel:
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); // for design in 96 DPI
I guarantee that if you follow these guidelines you will be ok, even when you have placed controls with specific anchors and don't use a flowpanel. We have an app built this way deployed on hundreds of machines with different DPI setups and we no longer have any complaints. All forms/containers/grids/buttons/textfield etc sizes are scaled correctly as is the font. Images work too, but they tend to get a little pixellated at high DPI.
EDIT: This link has a lot of good info, especially if you choose to use AutoScaleMode.DPI: link to related stackoverflow question
note: this will not fix the controls moving , when dpi change. this will only fix blurry text!!.
How to fix blurry Windows Forms in high-dpi settings:
Now, go to Program.cs (or the file where your Main method is located) and change it to look like:
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(); } }
Save and compile. Now your form should look crispy again.
source: http://crsouza.com/2015/04/13/how-to-fix-blurry-windows-forms-windows-in-high-dpi-settings/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With