Creating an Existing .NET Windows Application for DPI Support

We have an existing Windows.net application in which it uses all the custom controls.

For many days, everything was in order, as we simply supported the default resolution with a resolution of 96 dpi.

Now that we need our application to be in dpi format. What happens when we move on to the next

higher resolution 120 or 144, etc.

Most common problems

  • Problems with scaling a bitmap and

  • Text off

After going through these MSDN docs and existing SO issues I tried

enable such a fix in vain inside my application (as all controls used

configured and not supported in dpi format).

Things I tried after changing my application.manifest to enable the dpi transparency flag and set AutoScaleMode to AutoScaleMode.Dpi in the main form and in other forms, use AutoScaleMode as Initit

  • Changed the control font inside the OnLoad event

Graphics g = this.CreateGraphics (); int dpi = int.Parse (g.DpiX.ToString ());

switch (dpi) { case 125: this.Font = new Font(this.Font.FontFamily, this.Font.Size * 125.0f / (float)(g.DpiX)); Debug.WriteLine("<<---- Selected DPI Resolution :" + " 125 DPI ---->"); break; ... so on 
  1. Tried to override ScaleControl method to use different DPI-based scaling factor

But all this never works in my case.

Does anyone know / suggest me a better approach to solving this problem.

thanks

VATSAG

+6
source share
1 answer

So, I had the same problem in one of my applications. I managed to get around this in a two-step process, which, unfortunately, requires LOT refactoring, but after doing this work, I was able to automatically configure the application on a scale at different DPIs. Here's how to do it:

  • All your forms must be AutoScaleMode = AutoScaleMode.Font using AutoScaleMode = AutoScaleMode.Font . When I did something, I found out that AutoScaleMode.Dpi does not work as you would expect. You should also choose what your standard DPI will be. Say it's 96, because the fact that your application was originally designed in any case, you must set all your forms to use AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F) . You can set these configurations from the constructor view. In addition, if you created any custom controls, they should all be configured to use AutoScaleMode = AsutoScaleMode.Inherit . Using these two things, you can be sure that .NET will do all the scaling for all of your static components.

  • Now that is getting complicated. The fact is that the structure will support all your controls with the correct size, if they are not dynamically created and are not placed inside the form.

So, if you do something like:

 var myTextBox = new TextBox(); myTextBox.Location = new System.Drawing.Point(20, 20); myForm.Controls.Add(myTextBox); 

then this text box will be placed at the location (20, 20) no matter what your DPI is, therefore, making it look out of place on high DPI monitors. Thus, the solution should NEVER use hard coded pixel values, instead use values ​​that scale dynamically, depending on your current DPI configuration. So instead, you can write something like:

 var graphics = Graphics.FromHwnd(IntPtr.Zero); // This gets the graphics configuration of the current screen var scaleX = graphics.DpiX / 96; // 96 was our standard design DPI, remember? var scaleY = graphics.DpiY / 96; var myTextBox = new TextBox(); myTextBox.Location = new System.Drawing.Point((int)Math.Round(20 * scaleX), (int)Math.Round(20 * scaleY)); myForm.Controls.Add(myTextBox); 

Thus, the location you draw the control on the screen will depend on the actual DPI. And, if I can assume, I would actually extract the scaling functionality in some kind of helper class, where you define the scaling methods for the values ​​by x and y, so you will have a standard scaling mechanism. Note that it is also important that you independently scale along x and y, as some screens have different pixel densities on each axis.

In any case, if you follow these two conditions, your application should look just fine, regardless of the screen displayed on it.

+6
source

Source: https://habr.com/ru/post/958830/