Sign in Join
Wiki copytemporary

High DPI: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: High DPI

OldNewLine
1+## Introduction
2+
3+DPI (Dots Per Inch) is the relation between size in pixels and the actual display size. Here dot is an equivalent for pixel in printing terminology. Applications can either use pixel sizes, or take into account the actual display size. In this second case, sizes are given in points.
4+
5+Most of today operating systems use default DPI set to 96 and allow to change it to higher value manually. The physical DPI can be determined from display through [EDID](http://en.wikipedia.org/wiki/Extended_display_identification_data) protocol from physical size data and actual resolution. But the physical DPI is not used automatically by system so if you connect video output to monitor with different size then sceen resolution and visual size of controls are not automatically changed.
6+
7+Usually DPI is presented as one value but it can be different for horizontal and vertical axes if pixel is not square.
8+
9+In addition to basic application DPI awareness you can add own DPI options to your application to allow users to set custom per application DPI to overcome wrong system DPI setting.
10+
11+### Lazarus DPI related properties
12+
13+- Graphics.ScreenInfo.PixelsPerInchX
14+- Graphics.ScreenInfo.PixelsPerInchY
15+- Forms.Screen.PixelsPerInch
16+- Forms.TForm.DesignTimePPI (See: [DPI auto-adjustment and absolute layout auto-adjustment](http://wiki.lazarus.freepascal.org/Autosize_/_Layout#DPI_auto-adjustment_and_absolute_layout_auto-adjustment))
17+
18+### Pixels and points
19+
20+For example **300 DPI** means that there are 300 pixels (or dots) per inch. There are 72 points per inch, so :
21+
22+300 pixels ↔ 1 inch
23+
24+300/72 pixels ↔ 1 point
25+
26+4.16 pixels ↔ 1 point
27+
28+**Now with 96 DPI :**
29+
30+96 pixels ↔ 1 inch
31+
32+1.33 pixel ↔ 1 point
33+
34+**Now with 144 DPI :**
35+
36+144 pixels ↔ 1 inch
37+
38+2 pixels ↔ 1 point
39+
40+## Setting High DPI
41+
42+### Windows
43+
44+On Windows 95 and later, it is possible to change the DPI ratio to make elements bigger. High DPI means any custom DPI setting with more than 96 DPI (the default setting) \[<http://msdn.microsoft.com/en-us/library/ee318406(VS.85>).aspx *\].
45+
46+High DPI awareness means that an application takes this DPI setting into account.
47+
48+#### Windows Vista and Windows 7
49+
50+In Windows 7 go to "Control Panel \> Appearance and Personalization \> Display" (or just Control Panel \> Display in recent updates).
51+
52+Select Smaller 100% (default), Medium 125% or Larger 150%. If you select 100% (96 DPI) this is the default Windows DPI setting, (High DPI is not the default).
53+
54+If you select 125% (120 DPI) the option "Use Windows XP style DPI scaling" is enabled. Applications you run under this setting are scaled as if running under Windows XP.
55+
56+If you select 150% (144 DPI) the option "Use Windows XP style DPI scaling" is disabled (DPI Virtualization is enabled), and applications you run under this setting must be High DPI Awareness to prevent system scaling which will produce a blurred image.
57+
58+You can also set your custom DPI setting via the option "Set custom text size (DPI)" and enable/disable the DPI Virtualization.
59+
60+#### Windows 8 Metro Applications
61+
62+For Windows 8 Metro Applications read this <http://blogs.msdn.com/b/b8/archive/2012/03/21/scaling-to-different-screens.aspx>
63+
64+#### Windows 10
65+
66+Windows 10 "Control Panel \> Appearance and Personalization \> Display" have more options. You can have different font sizes for each element: Title bar, Menu, Dialog box and so on. Ensure you test twice in order to check if everything works under different sizes.
67+
68+Now is based on Font Size, not DPI. The DPI option is not recommended, but still there. So, instead of changing the size of all elements in desktop, this will change just the font size (And of course everything else is changed to fit).
69+
70+Remember that under Windows 10 there are Universal Applications (WinRT) and the classic desktop applications (Win32). We're talking here about desktop applications.
71+
72+### Linux
73+
74+On Linux DPI setting is more complicated and depends on used software and their version.
75+
76+You can discover your current monitor DPI by command:
77+
78+``` bash
79+xdpyinfo|grep dots
80+```
81+
82+You can change DPI to new value by command:
83+
84+``` bash
85+xrandr --dpi 144x144
86+```
87+
88+To preserve setting after reboot you need to add the command as script to /etc/X11/Xsession.d/77set_dpi.
89+
90+More information:
91+
92+- [How to find and change the screen DPI?](http://askubuntu.com/questions/197828/how-to-find-and-change-the-screen-dpi)
93+- [Xorg Display size and DPI](https://wiki.archlinux.org/index.php/xorg#Display_size_and_DPI)
94+- [Change fixed 96dpi on Ubuntu with high DPI LCD](https://help.ubuntu.com/community/AsusZenbook#LCD)
95+
96+## Examples
97+
98+### Fixed Font Sizes (not HighDPI)
99+
100+Here is a form with an undefined font size (set to zero, which is the default value). It has been designed at 96 DPI (100%), and it looks like this :
101+
102+<figure>
103+<img src="Testdpi100.png" title="Testdpi100.png" />
104+<figcaption>Testdpi100.png</figcaption>
105+</figure>
106+
107+Now, at 120 DPI (125%), it becomes :
108+
109+<figure>
110+<img src="Testdpi125.png" title="Testdpi125.png" />
111+<figcaption>Testdpi125.png</figcaption>
112+</figure>
113+
114+As you can see, the font gets bigger and so the text is clipped. The window title gets bigger, but the client area of the window remains the same size. Note that these changes in size can occur by using an application with a different Windows theme, or with another operating system.
115+
116+To avoid this, you must set the font size to a non-zero value. Note that Font.Size is expressed in points and Font.Height is expressed in pixels. In fact, only the value of Font.Height is stored, and Font.Size changes according to current DPI value. So if we set the font size, it will be fixed to a certain size in pixels.
117+
118+If we try again with a fixed font size of 9 points, then at 96 DPI (100%), we get this :
119+
120+<figure>
121+<img src="Testdpi100fixedM12P9.png" title="Testdpi100fixedM12P9.png" />
122+<figcaption>Testdpi100fixedM12P9.png</figcaption>
123+</figure>
124+
125+Now if the same program is run at 120 DPI (125%), it becomes :
126+
127+<figure>
128+<img src="Testdpi125fixedM12P9.png" title="Testdpi125fixedM12P9.png" />
129+<figcaption>Testdpi125fixedM12P9.png</figcaption>
130+</figure>
131+
132+The result is the almost the same. The title bar is bigger, but the client area and the font size is the same. Note that in fact, the size in points of the font has changed.
133+
134+The conclusion from this is that it is possible to avoid inconsistency in the display by fixing font sizes. But we do not take into account that the graphical elements may be smaller according to actual DPI of the screen. With DPI awareness, it is possible to make an application behave as if it knew the real size of the pixels.
135+
136+### DPI Aware Application (For Vista +)
137+
138+[CPickSniff](http://sourceforge.net/projects/cpicksniff2/) is an application to capture screen colors. We will use it as an example to see how High DPI works in Windows.
139+
140+**Default DPI**
141+
142+This is the app running at 96 DPI (100%). It's the default mode, when scaling isn't necessary.
143+
144+<figure>
145+<img src="cpicksniff_defaultdpi.png" title="cpicksniff_defaultdpi.png" />
146+<figcaption>cpicksniff_defaultdpi.png</figcaption>
147+</figure>
148+
149+**Windows DPI Scaling**
150+
151+This is the same app running at 144 DPI (150%) without a manifest, so Windows scales it like a bitmap. The result is a blurred image.
152+
153+<figure>
154+<img src="cpicksniff_blured.png" title="cpicksniff_blured.png" />
155+<figcaption>cpicksniff_blured.png</figcaption>
156+</figure>
157+
158+**With Manifest**
159+
160+Running at 144 DPI (150%). This time the app includes a manifest but the application contains no code to handle scaling. Items aren't scaled whereas fonts are scaled (Windows does this automatically), so text is clipped.
161+
162+<figure>
163+<img src="cpicksniff_nohighdpi.png" title="cpicksniff_nohighdpi.png" />
164+<figcaption>cpicksniff_nohighdpi.png</figcaption>
165+</figure>
166+
167+**High DPI**
168+
169+Finally with both a manifest and a LCL scaling, the app is in High DPI.
170+
171+<figure>
172+<img src="cpicksniff_highdpi.png" title="cpicksniff_highdpi.png" />
173+<figcaption>cpicksniff_highdpi.png</figcaption>
174+</figure>
175+
176+## High DPI in Lazarus 1.8 and above
177+
178+To handle High DPI using new features in 1.8, follow these steps:
179+
180+- On Windows: enable DPI awareness in Project Options -\> Application. Decide if you want to support per monitor DPI awareness or not.
181+- Enable LCL scaling for your application DPI awareness in Project Options -\> Application -\> "Use LCL scaling (Hi-DPI).
182+- Set TForm.Scaled=True for all your forms (it is the default value). All WYSIWYG should work automatically. Also the designer scales the forms accordingly.
183+- Set Application.Scaled=True.
184+- If you create controls run-time, scale all coordinates, sizes etc that have to be DPI-aware with TControl.Scale96ToForm() or ScaleDesignToForm() (depending on your choice of default PPI) or prepare your container (e.g. panel with controls) as it was with 96 PPI and then call TControl.AutoAdjustLayout(lapAutoAdjustForDPI, 96, ParentFormOfTheContainer.PixelsPerInch, 0, 0);
185+- If some of your components don't scale their inner sizes, override DoAutoAdjustLayout and scale the sizes (see TToolBar) - it has to be done for all controls. If a LCL control misses DoAutoAdjustLayout please report to mantis and provide a patch if you can.
186+- Hint: Do *not* develop under HighDPI Mode, always develop under normal mode and only test under HighDPI. Otherwise Lazarus IDE will set DesignTimePPI of the different forms to different values.
187+
188+### Lazarus IDE high DPI
189+
190+The Lazarus IDE is DPI-aware itself.
191+
192+## High DPI in older Lazarus
193+
194+Is not as nice as 1.8 but it works, call on each form OnCreate event:
195+
196+Self.AutoAdjustLayout(lapAutoAdjustForDPI, 96, Screen.PixelsPerInch, Self.Width, ScaleX(Self.Width, 96));
197+
198+## See also
199+
200+- [[Autosize / Layout]]
201+- [[Anchor Sides]]
202+
203+## External Links
204+
205+- \[<http://msdn.microsoft.com/en-us/library/dd464646(v=VS.85>).aspx High DPI (Windows)\] MSDN article about High DPI
206+- [Guidelines for scaling](http://msdn.microsoft.com/en-us/library/windows/apps/hh465362) Windows Dev Center - User experience guidelines for scaling Metro style apps
207+- [[Windows Icon]] How to create icons that work with High DPI.
208+
209+---
210+*From the Free Pascal wiki, page "High DPI", as it stood on 2022-08-19 after a change by El Salvador, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history